Angular 2 / Ionic 2 TypeError: zxcvbn_1.default is not a function
Asked Answered
E

1

6

Testing with Jasmine and Karma, I am using the zxcvbn library to do some password strength calculations. This fails when I try to run a spec on it, by calling the updateScore() function below.

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

In the import, I am doing import zxcvbn from 'zxcvbn';

This works in the app, and even when I build and emulate, but in the spec, it gives me that error:

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)
Electrosurgery answered 19/1, 2017 at 19:38 Comment(0)
R
10

I had this problem when using

import linkifyStr from 'linkifyjs/string';

instead of

import * as linkifyStr from 'linkifyjs/string';

Revitalize answered 19/3, 2017 at 23:49 Comment(1)
Thanks! your answer solved my problem when I was testing angular 2 components that depends of moment-timezone. I had this error when ran ng test: moment_timezone_1.default is not a function After change the import It works like a charm.Meso

© 2022 - 2024 — McMap. All rights reserved.