Angular 2 / Ionic 2 TypeError:zxcvbn_1.default不是函数

时间:2017-01-19 19:38:37

标签: angular ionic-framework ionic2 angular2-testing

使用Jasmine和Karma进行测试, 我正在使用zxcvbn库来进行密码强度计算。 当我尝试通过调用下面的updateScore()函数来运行规范时,这会失败。

private updateScore(): void {
    const analysis = zxcvbn(this.password)
    this.score = analysis.score;
    this.comment = analysis.feedback.warning || analysis.feedback.suggestions[0];
  }

在导入中,我正在执行import zxcvbn from 'zxcvbn';

这适用于应用程序,即使我构建和模拟,但在规范中,它给了我这个错误:

FAILED TESTS:
  PasswordStrengthChecker
    ✖ should update the score when password changes
      Chrome 55.0.2883 (Mac OS X 10.11.6)
    TypeError: zxcvbn_1.default is not a function
        at PasswordStrengthChecker.updateScore (webpack:///src/components/password-strength-checker/password-strength-checker.ts:25:28 <- src/test.ts:93920:40)
        at PasswordStrengthChecker.ngOnChanges (webpack:///src/components/password-strength-checker/password-strength-checker.ts:20:9 <- src/test.ts:93916:14)
        at Object.<anonymous> (webpack:///src/components/password-strength-checker/password-strength-checker.spec.ts:27:13 <- src/test.ts:185997:18)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:232:0 <- src/test.ts:183822:26)
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- src/test.ts:130574:39)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:231:0 <- src/test.ts:183821:32)
        at Zone.run (webpack:///~/zone.js/dist/zone.js:114:0 <- src/test.ts:183704:43)
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:102:0 <- src/test.ts:130289:34)

1 个答案:

答案 0 :(得分:8)

使用

时出现此问题

import linkifyStr from 'linkifyjs/string';

而不是

import * as linkifyStr from 'linkifyjs/string';