nx: How to set transformIgnorePatterns to fix "Jest encountered an unexpected token"
First 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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 42
- Comments: 31 (10 by maintainers)
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:
Nevermind, obviously it’s
./jest.config.json… 🙄However, the problem is still there even when adding that config 🤔
the only thing that worked for me was to add this to the main
jest.config.jsmake sure you have
lodashin you devDependenciesI finally fixed the issue by adding
"allowJs": trueto thecompilerOptionsof each lib/app’s tsconfig.spec.json (or alternatively to the root tsconfig.json). Of course in addition to settingtransformIgnorePatternsas @llwt suggested. Without that option the TypeScript compiler just skipped the lodash files.for next release of nx(11.5.2) you can do this to fix the issue. Install babel-jest and @babel/preset-env
create babel.config,js in the root of the workspace and add this module.exports = { presets: [‘@babel/preset-env’] };
then in your jest,config.js add this transformIgnorePatterns: [
/node_modules/?!lodash-es], transform: { ‘^.+\.(ts|html)$’: ‘ts-jest’, ‘^.+\.js$’: ‘babel-jest’ }Solved this without
allowJs, the only 1 file which changed it was jest.config.js (local lib file, not in global jest.config.js) by adding transformIgnorePatterns lijke belowWhere ng2-tel-input was the problematic module, getting it for the following environment: Jest 23.6.0, @nrwl/builders 7.5.1, @nrwl/nx 7.5.1, @nrwl/schematics 7.5.1. jest-preset-angular 6.0.1.
I was not able to get this solution to work.
Instead I wrote it like this and it worked:
In case it helps anyone - in my case, using a monorepo with
babel-jest, I needed to add:without the
rootModeoption, babel-jest was giving syntax errors when importing other libraries in the monorepo.I am using deepdash in place of loadash added below in jest.preset.js
and created a new file babel.config.json and added
and inside individual lib jest.config.js added
in tsconfig.spec.json added “allowJs”: true,
nothing is working, still the issue persists, could you please help on this.
For anyone that is still running into this issue in the context of a monorepo (yarn workspace in my case), make sure that you are not hoisting the package that needs to be transpiled.
The solution that worked for me:
babel.config.jsin the package root (not the workspace root):Jest config:
Root
package.jsonAny one of those four things (nohoist, transform, transformIgnorePatterns, and babel config) missing & it won’t work.
Actually this one is better
Because mapping to
lodashwill resolve in LodashWrapper object instead of the function itselfSorry, but don’t remember now… For sure it was one of those which caused errors on Server-Side Rendering (SSR)/Angular Universal.
asztal solution works for me with Nx 15.x, Angular 15.x and Jest 28.x
@llwt Where can I find the
jest-preset-angular/preprocessor.js? It’s not available in jest-preset-angular@8.0.0Update: apologizes for the mistake, you have to include
allowJsas well. The reason why I got it working is I didn’t clean cache. Before testing remember to do so.