Jest-preset-angular: lodash functions cause: TypeError: get_1.default is not a function

Created on 3 Oct 2017  ·  10Comments  ·  Source: thymikee/jest-preset-angular

I have an Angular app generated with angular/cli with a dependency to "lodash": "^4.17.4". The application works well but it is not possible to test it with jest.

In my component I import the lodash function that I need with import get from 'lodash/get';, testing this component cause the error TypeError: get_1.default is not a function.

Any idea of how to fix this issue?

Most helpful comment

This is not a problem of Jest, it's your code. You should use lodash-es if you want to use ES modules. As a workaround, ESM provides this syntax, which will let you use non-ESM compliant code:

import * as isString from 'lodash/isString';

All 10 comments

I don't get that error in my examples (just used latest lodash and imported to component) but funny things happen to snapshot serializer when I name it get (other names are ok)

Ok so I have:

"lodash": "^4.17.4",
"@types/jest": "^21.1.2",
"jest": "^21.2.1",
"jest-preset-angular": "^4.0.0",
"jest": {
    "preset": "jest-preset-angular",
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts"
}

Nothing different than the default configuration in your readme and still TypeError: isString_1.isString is not a function in my component with the code import { isString } from 'lodash/isString';

Once you use default export, now it's named export, which is weird.
You can clone this repository and repro your case in the example dir.

I have the exact same issue using the example dir:

● SimpleComponent › snapshots

TypeError: isString_1.isString is not a function

This is not a problem of Jest, it's your code. You should use lodash-es if you want to use ES modules. As a workaround, ESM provides this syntax, which will let you use non-ESM compliant code:

import * as isString from 'lodash/isString';

@zyasir you bump an issue closed two years ago with a different error message, probably you do not have setup jest + typescript properly.

Please follow the configuration in README.md to solve your problem or create a new issue if it still persists.

Affirmative @wtho - deleted.

@zyasir no need to delete the comment, I also didn't want to come across too harsh, but your error message looked more like an issue with a wrong typescript+jest setup and just accidentally showing lodash in the error message.
If you follow the README, it should help you solving it.
Cheers!

Thanks @wtho I appreciate the gentleness!

If you're using typescript, setting
"esModuleInterop": true
will work like charm

Was this page helpful?
0 / 5 - 0 ratings