Repository navigation
Closed
Description
Describe the bug
I updated the Axios from "axios": "^0.27.2" to "axios": "^1.1.2", and Jest tests are not working anymore with the following error:
FAIL src/app/slices/app/App.slice.test.tsx
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/imrankhan/Development/roc/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import axios, { AxiosRequestConfig } from 'axios';
| ^
2 |
3 | class HttpClientService {
4 | /**
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/app/services/http-client/HttpClient.service.tsx:1:1)
To Reproduce
- Use React and Axios latest version
- Create Jest test
- Run the test to generate the error
Environment
- Axios Version
^v1.1.2 - Browser
Chrome - Browser Version
Version 105.0.5195.125 (Official Build) (x86_64) - Node.js Version
v18.4.0 - OS:
MAC OS 12.4 - React:
^18.2.0
Temporary Fix
The following fix works for now but I expect a standard fix for this issue.
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
or
"test": "react-app-rewired test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
Reacted by Ron Šmeral, artshepel, Jamie Telin, Anthony, bhoomiagrawal, Tomohiko Ozawa, Kai Dorschner, MihaiC, Alexander Sedlmayr, Wooulf and 65 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
lejahmie commentedon Oct 11, 2022
For context: Problem seem to be that Axios is now built as ES Module instead of CommonJs when not run in Node. Problem with Jest is that it runs code in Node, but application is built for web-clients.
This is why telling Jest to transform Axios works.
pzi commentedon Oct 12, 2022
See #5026
rathpc commentedon Oct 12, 2022
The change to
transformIgnorePatternsdoes not work in my case so I am hoping there is a proper fix provided from axios soon.hisuwh commentedon Oct 12, 2022
yh
transformIgnorePatterns"fix" is not working for merathpc commentedon Oct 12, 2022
I found that this did however work for me because it forces those imports to resolve with the CJS module instead:
rd-stefan-riehmer commentedon Oct 14, 2022
@rathpc - i tried your solution.
outcome:
jest test: mocking with jest.mock('axios') works again
the app (called from jest): the library axios is imported and it's value is
undefinedand therefore i get an error => e.g.Cannot read properties of undefined (reading 'get')zntemel commentedon Oct 22, 2022
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\""this worked, when I restart test command
otistruong commentedon Oct 24, 2022
Thank you!
This is work for me, not
transformIgnorePatternspzi commentedon Oct 26, 2022
Try upgrading jest to latest (>29.2.x), it worked for me without having to change jest config.
single9 commentedon Oct 28, 2022
You save my time!
nskazki commentedon Oct 29, 2022
Works for me:
Jest tests failed after upgrading axios to v1.1.2. Add axios to modul…
kurund commentedon Nov 4, 2022
Thank you. This worked for me
dhruvsakariya commentedon Nov 6, 2022
Thank You it worked
Any permanent solution Found ??
MatVD commentedon Nov 8, 2022
"test": "react-scripts test --transformIgnorePatterns "node_modules/(?!axios)/"",
Thanks... it works well for me
44 remaining items
hems commentedon Dec 2, 2023
nothing suggested here or on the readme works for me as well..
here are my versions:
probably worth mentioning i'm using
ts-node.. maybe has something to do with it?fix: Added jest transform ignore pattern for axios
UAR-1326 Updated version of the api-sdk-node library
Namnika commentedon Dec 14, 2023
In my case, I was also facing the same problem
So I got to solve with this:
but have one question
jestcan't work withVite? Even knowing Vitest is integrated with jest, So still vitest should used?btw, I'm also using Vite
SalahAdDin commentedon Dec 15, 2023
Forget Jest, embrace Vitest.
devamitranjan commentedon Dec 25, 2023
If you are using xhrAdapter then you have to use the module mapper configuration for the not found modules in your package.json file:
"jest": { "moduleNameMapper": { "axios/unsafe/adapters/xhr": "axios/lib/adapters/xhr.js", "axios/unsafe/core/settle": "axios/lib/core/settle.js" } }test: fixes jest errors after axios upgrade.
YuraZakaryan commentedon Mar 4, 2024
This is work for me
"jest": { "moduleNameMapper": { "^axios$": "axios/dist/node/axios.cjs" } },markcorrea commentedon Mar 13, 2024
Solved for me for version
1.6.7. Inside jest-config.json:"moduleNameMapper": { "^axios$": "<rootDir>/node_modules/axios/dist/node/axios.cjs" },gclark-eightfold commentedon May 14, 2024
To help anyone else in the future facing similar issues in other packages, I think the issue is that Jest 27 and below does not fully support package.json exports (see Jest 28 blog post)
As for why this issue happens with Axios and not many other packages, axios is declaring
"type": "module"in the package.json with"main": "index.js", such thatindex.jsis an ESM file. If you are running jest tests in a non-node environment (e.g.jsdom), this will cause issues in Jest 27 and below sincedist/node/axios.cjsis only referenced in theexportsfield.Solutions for Library Authors
mainfield in package.json references a CommonJS file and use themodulefield for the ESM file.exportsfield to remap how consumers import specific entry points (e.g.my-package/hello-world->my-package/dist/hello-world.cjs), not supporting Jest 27 and below may be the only option.Solutions for Application Authors
exportscompatibilitymoduleNameMapperconfig option to force the CJS file to be used. (See above examples for how this is done for axios specifically).added transformIgnorePattern for axios, there is a known issue with …
Snyk July Review (#7895)
Upgrade Axios
Upgrade misc dependencies (#133)
Russzh commentedon Apr 16, 2025
jest: { configure: { collectCoverage: true, collectCoverageFrom: [ "src/**/*.{js,jsx,ts,tsx}", "!src/**/*.types.ts", "!src/**/*.d.ts", "!src/**/index.{js,jsx,ts,tsx}", "!src/reportWebVitals.ts", "!src/stories/*.{ts,tsx}", ], moduleNameMapper: { "^axios$": require.resolve("axios"), }, }, },in craco.config.js works for me