axios: Jest tests failed after upgrading axios to v1.1.2
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)/\"",
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 73
- Comments: 51 (2 by maintainers)
Commits related to this issue
- Jest tests failed after upgrading axios to v1.1.2. Add axios to moduleNameMapper to workaround https://github.com/axios/axios/issues/5101\#issuecomment-1276572468 Signed-off-by: Gavin Reynolds <greyn... — committed to giranm/pd-live-react by gsreynolds 2 years ago
- test: add fix for axios import ref: https://github.com/axios/axios/issues/5101#issuecomment-1276572468 — committed to Tony133/testing-nestjs by jmcdo29 2 years ago
- test: add fix for axios import ref: https://github.com/axios/axios/issues/5101#issuecomment-1276572468 — committed to jmcdo29/testing-nestjs by jmcdo29 2 years ago
- Upgrade Axios https://github.com/axios/axios/issues/5101#issuecomment-1307878899 — committed to watsonbox/exportify by watsonbox 2 years ago
- Add Axios Package Add the Axios package to connect to the API so we can get data from the 'dev.to' API. Currently, the latest version of Axios, v1.1.2, does not work with Jest, so we are staying with... — committed to SpiritBreaker226/portfolio by SpiritBreaker226 2 years ago
- Add Axios Package Add the Axios package to connect to the API so we can get data from the 'dev.to' API. Currently, the latest version of Axios, v1.1.2, does not work with Jest, so we are staying with... — committed to SpiritBreaker226/portfolio by SpiritBreaker226 2 years ago
- Update axios to 1.0, update jest to handle axios >=1, following advice in https://github.com/axios/axios/issues/5101, then update pact to fix https://github.com/pact-foundation/pact-js/issues/606, and... — committed to holly-cummins/house-of-microservices-quarkus-contract-testing-sample by holly-cummins a year ago
- Add fix to run axios on CI See https://github.com/axios/axios/issues/5101 — committed to wger-project/react by rolandgeider a year ago
- build(deps): allow axios `>=0.25.0 || ^1.2.2` Versions of Axios 1.x are causing issues with Jest (https://github.com/axios/axios/issues/5101). Jest 28 and 29, where this issue is resolved, has other... — committed to swan-bitcoin/node-marketo by ramontayag a year ago
- update test:nowatch for axios support https://github.com/axios/axios/issues/5101 — committed to joseph-ortiz/bookstoreclient by deleted user a year ago
- Fix package.json テストが走らない テストコマンドを修正 参考 https://github.com/axios/axios/issues/5101 — committed to nagisano33/recoil-sample by nagisano33 10 months ago
- package: upgrade all dependencies Upgrade all dependencies. Enable transformation for axios in order to make tests execute correctly. See https://github.com/axios/axios/issues/5101. — committed to mdonadoni/reana-ui by mdonadoni 8 months ago
- package: upgrade all dependencies Upgrade all dependencies. Enable transformation for axios in order to make tests execute correctly. See https://github.com/axios/axios/issues/5101. — committed to mdonadoni/reana-ui by mdonadoni 8 months ago
- Add Jest config to support upgraded axios See https://github.com/axios/axios/issues/5101#issuecomment-1276572468 — committed to companieshouse/confirmation-statement-web by nevans4-ch 7 months ago
- fix: Added jest transform ignore pattern for axios Tests are failing because jest is failing to import axios. Fixing it by following https://github.com/axios/axios/issues/5101#issuecomment-1307878899 — committed to awslabs/visual-asset-management-system by ravij3 7 months ago
- UAR-1326 Updated version of the api-sdk-node library * Uses latest version in order to get 'is_remove' field sent to API * Added Jest config to support upgraded axios (see https://github.com/axios/ax... — committed to companieshouse/overseas-entities-web by mwestacott 7 months ago
- test: fixes jest errors after axios upgrade. See https://github.com/axios/axios/issues/5101 — committed to JaneliaSciComp/neuronbridge by neomorphic 5 months ago
I found that this did however work for me because it forces those imports to resolve with the CJS module instead:
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\""
this worked, when I restart test command
Works for me:
Try upgrading jest to latest (>29.2.x), it worked for me without having to change jest config.
Thanks, this works:
"test": "react-scripts test --transformIgnorePatterns "node_modules/(?!axios)/"",
You can also do it this way: add this jest config to your package.json
and you can keep the npm test script as it was before:
"test": "react-scripts test",
Hi 👋
Please try the latest pre-release by running the following:
Please provide feedback in either the pinned issue or the discussion thread 🧵
Thank you! This is work for me, not
transformIgnorePatterns
Could you not think of releasing two different libs, one for backend, one for frontend, with different build target or something? How many human life hours have been wasted on this nonsense?
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.
Thank You it worked
Any permanent solution Found ??
The change to
transformIgnorePatterns
does not work in my case so I am hoping there is a proper fix provided from axios soon.Checked on
"axios": "^1.3.2"
still not fixed (without--transformIgnorePatterns
) !!Just leaving this here. Here is a blurb from the JEST documentation why you have to include axios in transforming/transpiling. By default JEST ignores node_modules/*
Solved for me for version
1.6.7
. Inside jest-config.json:"moduleNameMapper": { "^axios$": "<rootDir>/node_modules/axios/dist/node/axios.cjs" },
This is work for me
"jest": { "moduleNameMapper": { "^axios$": "axios/dist/node/axios.cjs" } },
Forget Jest, embrace Vitest.
I am at axios 1.6.1 and jest is 29.5.8, with CRA and
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
is not working… any solutions ??Axios : “axios”: “^1.5.1”, Jest : “27.5.1”, Tried out all the solutions but none working for these versions…
“test”: “react-scripts test --transformIgnorePatterns "node_modules/(?!axios)/"” Thanks for the solution, saved a lot of time 🙂
No any of the solutions given here gave us a proper solution, we are working with the
1.2.5
version.Up from me 😉
Finally, the issue is resolved I updated the latest jest version —>npm install --save-dev jest@29.3.1
another way this also working “test”: “react-scripts test --transformIgnorePatterns "node_modules/(?!axios)/"”
this worked when I restarted the test command
@KonradLinkowski oh whoops 😅. Yeah we should probably have one of them opened
@KonradLinkowski #5203 is currently open for the same issue - looks like things won’t be solved until this issue is fixed on the jest side of things.
See https://github.com/axios/axios/issues/5026