How to set transformIgnorePatterns to fix "Jest encountered an unexpected token"
See original GitHub issueFirst of all, thanks for bringing Jest to Angular!
I previously had configured Jest in my Angular project by myself. In order to use lodash-es, I had to set transformIgnorePatterns to inlude the path to lodash-es:
"jest": {
"preset": "jest-preset-angular",
...
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!lodash-es/.*)"
],
Now, after migrating to the Jest config provided by Nx, I don’t know where I can set this option. My tests currently fail with this error:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
C:\Users\Marvin\Projekte\hypershop-ng\node_modules\lodash-es\lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token export
1 | import { ComponentFixture } from "@angular/core/testing";
> 2 | import { isUndefined } from "lodash-es";
| ^
3 |
4 | export function expectElementFromFixture<T>(fixture: ComponentFixture<T>, domQuery?: string): jasmine.Matchers<{} | null> {
5 | return expect(elementFromFixture(fixture, domQuery));
at ScriptTransformer._transformAndBuildScript (../../node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (../../libs/common/src/test/expect.ts:2:1)
at Object.<anonymous> (../../libs/common/src/test/index.ts:1:1)
Thanks for your help
Issue Analytics
- State:
- Created 5 years ago
- Reactions:41
- Comments:29 (10 by maintainers)
Top Results From Across the Web
Jest transformIgnorePatterns not working - Stack Overflow
My project has babel set up and working fine, is there something special I would need to do for Jest? I thought it...
Read more >Fix "Jest encountered an unexpected token" with "create-react ...
The easiest way to fix this one is of course also using the same option transformIgnorePatterns but we just simply put in the...
Read more >jest syntaxerror: unexpected token export - You.com | The AI ...
This is what you need to do: To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your...
Read more >Jest Unexpected Token a.k.a. won't transpile error - home/pierce
Jest encountered an unexpected token This usually means that you are ... you can specify a custom "transformIgnorePatterns" in your config.
Read more >Using Jest together with TypeScript in multi-module projects
Jest complained with the following error message: Jest encountered an unexpected token . Sometimes it happens (especially in React Native or ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had to remove the
<rootDir>from ours:One other option is to pull in
babel-jestand tell it to transpile those js files.From the jest-preset-angular docs:
We took that and tweaked it to only pass the js files needed through: