Closed dubzzz closed 3 years ago
By default, babel-jest
does not consider (or ignores) transformation of any module under node_modules
. One solution to fix your issue is to use the following as your test script:
react-scripts test --transformIgnorePatterns "node_modules/(?!@codemirror)/"
For further reference, please check: https://github.com/facebook/jest/issues/6229#issuecomment-403539460
Thanks a lot @ehfeng, it works perfectly well! Even the --experimental-vm-modules
does not seem to be required anymore.
I got the same error. After many days of struggling, trying all sorts of different configs, setting transformIgnorePatterns
here and there without any luck, I managed to figure out the culprit: my IDE, WebStorm. If I run npm test
with the new transformIgnorePatterns
flag changes in my package.json > scripts > test
, then all my test runs properly. However, if I try to run a test own its own via the IDE, I get the error above. If I edit the test (Run Configuration) in WebStorm by adding the flag, it runs perfectly, but I'm not going to add it manually to every individual test that I click with the green arrow.
It seems as though the transformIgnorePatterns
doesn't work when set in the jest.config.js
file, wish it did though, with WebStorm.
Edit:
I think I managed to solve it. The jest.config.js
file doesn't even get read by WebStorm. So I just went into my package.json
and put a jest
field as follows:
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"transformIgnorePatterns": ["node_modules/(?!@shotgunjed)/"]
},
My external module that's in node_modules
as @shotgunjed/my-custom-library
which was causing the problem, no longer does. Now Jest can interpret the code and no longer act like it's choking when it encounters the import
keyword.
npm i babel-jest
And I changed the test
in my package.json
to "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!@codemirror)/\"",
I got the same error. After many days of struggling, trying all sorts of different configs, setting
transformIgnorePatterns
here and there without any luck, I managed to figure out the culprit: my IDE, WebStorm. If I runnpm test
with the newtransformIgnorePatterns
flag changes in mypackage.json > scripts > test
, then all my test runs properly. However, if I try to run a test own its own via the IDE, I get the error above. If I edit the test (Run Configuration) in WebStorm by adding the flag, it runs perfectly, but I'm not going to add it manually to every individual test that I click with the green arrow.It seems as though the
transformIgnorePatterns
doesn't work when set in thejest.config.js
file, wish it did though, with WebStorm.Edit: I think I managed to solve it. The
jest.config.js
file doesn't even get read by WebStorm. So I just went into mypackage.json
and put ajest
field as follows:"jest": { "transform": { "^.+\\.[t|j]sx?$": "babel-jest" }, "transformIgnorePatterns": ["node_modules/(?!@shotgunjed)/"] },
My external module that's in
node_modules
as@shotgunjed/my-custom-library
which was causing the problem, no longer does. Now Jest can interpret the code and no longer act like it's choking when it encounters theimport
keyword.
You are my savior. This problem drove me mad two days and only your solution fixed it
By default,
babel-jest
does not consider (or ignores) transformation of any module undernode_modules
. One solution to fix your issue is to use the following as your test script:
react-scripts test --transformIgnorePatterns "node_modules/(?!@codemirror)/"
For further reference, please check: facebook/jest#6229 (comment)
Thanks man
I got the same error. After many days of struggling, trying all sorts of different configs, setting
transformIgnorePatterns
here and there without any luck, I managed to figure out the culprit: my IDE, WebStorm. If I runnpm test
with the newtransformIgnorePatterns
flag changes in mypackage.json > scripts > test
, then all my test runs properly. However, if I try to run a test own its own via the IDE, I get the error above. If I edit the test (Run Configuration) in WebStorm by adding the flag, it runs perfectly, but I'm not going to add it manually to every individual test that I click with the green arrow.It seems as though the
transformIgnorePatterns
doesn't work when set in thejest.config.js
file, wish it did though, with WebStorm.Edit: I think I managed to solve it. The
jest.config.js
file doesn't even get read by WebStorm. So I just went into mypackage.json
and put ajest
field as follows:"jest": { "transform": { "^.+\\.[t|j]sx?$": "babel-jest" }, "transformIgnorePatterns": ["node_modules/(?!@shotgunjed)/"] },
My external module that's in
node_modules
as@shotgunjed/my-custom-library
which was causing the problem, no longer does. Now Jest can interpret the code and no longer act like it's choking when it encounters theimport
keyword.
Thank you so much. So annoying to see last test failing because of axios import module.
Perfect, This worked for me. Cheers mate
Hello 👋
Describe the bug
I want to run unit tests against my React application.
Unfortunately one of the dependencies I am using is only packaged for ES Modules* and it looks as if the default configuration coming with CRA 4.x is not able to deal with it out-of-the-box. * "type": "module"
When calling
yarn test
I get the following error (which looks as if I was mixing import with require):While facing this issue I tried to shrink the issue to its simplest case so I also tried without CRA just by using only Jest and it worked as charm (on normal js files without all the babel part).
Did you try recovering your dependencies?
Which terms did you search for in User Guide?
ES Modules, mjs
Environment
Steps to reproduce
In order to reproduce you can have a look to this minimal reproduction repository: https://github.com/dubzzz/codemirror-next-repro-cra.
cd test-in-cra
yarn
yarn test
In the repository I also tried to run a test on a file using this package using jest only (see test-in-jest-esm). It required some changes like passing the option
--experimental-vm-module
to node.But I have not succeeded to do the same kind of trick when running Jest through CRA. Passing the flag does not seem to be enough as babel keeps transpiling the import to require when running the test.
Expected behavior
It should run fine when I try to run a test against a file importing a package only designed for ES Modules.
Actual behavior
It crashes.
Reproducible demo
Repro: https://github.com/dubzzz/codemirror-next-repro-cra
See Steps to reproduce