You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Updating Axios from 0.27.2 to 1.0.0 breaks Jest tests in a create-react-app app #5026
I have several React applications created with a Typescript based create-react-app application (and using [email protected], which depends on [email protected]). Attempting to upgrade Axios from 0.27.2 to 1.0.0 causes Jest tests to fail with many errors like the one below. It appears that [email protected] now emits only an ESM module, and no longer a CJS module, which causes known errors with Jest.
Test Runtime Error
/home/runner/work/library/library/client/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
6 | // External Modules ----------------------------------------------------------
7 |
> 8 | import axios, {AxiosInstance} from "axios";
| ^
9 |
10 | // Internal Modules ----------------------------------------------------------
11 |
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/clients/Api.ts:8:1)
Environment
Axios Version [1.0.0]
React Version [18.2.0]
React-Scripts Version [5.0.1]
Node.js Version [16.11.1]
OS: [OSX 12.6]
Reacted by pipopotamasu, Victor Orlyk, Bart Schriever, PhilippeGingras, CSWITH89, Chad Robinson, H. Peng, Damien White, Udgar Bhasu, Lawand and 67 more
Reacted by Aqeel Bhat
Reacted by Eduardo Pacheco, Victor Orlyk, João Portela, Jenny Tran, CSWITH89, Udgar Bhasu, vitalyster, Vatsal Patel , Niklas Eicker, Josie and 2 more
Reacted by João Portela, Erazihel, Erik Renberg, David DOLCIMASCOLO, Grant Iverson, Dercilio Fontes, Yuta Takahashi, Haneen, Chase-Dickerson, Žilvinas and 10 more
Reacted by AbelardoLG, arash-bizcover, Brieuc de La Fouchardiere, Darren Cooney, Tyler Goodman, Denis, Raphael Marques, linmingye-rbc, Ian da Conceição da Silva, Marco and 28 more
The suggestion about transformIgnorePatterns in a jest.config.file didn't help me either. I also tried to add the transformIgnorePatterns using the jest key in my package.json without any success.
However, the tests started to run after I inlined the flag:
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
Reacted by agiledots, Lena Fuhrimann, Imran Khan, Berkay ÇIRAK, Sidney Gijzen, B.D.Heshan Madhuranga, Ümit Uysal, Mike, seyed-amir-mehrizi, Chen Bin and 34 more
Reacted by agiledots, Ingo Ruck, Darren Cooney, Reşat YILDIZ, seyed-amir-mehrizi, LaV0n, nothingnewlmao, Daniel Eriksson, devTap, Gee and 7 more
Reacted by agiledots, Imran Khan, Ingo Ruck, B.D.Heshan Madhuranga, seyed-amir-mehrizi, nothingnewlmao, Gee, Aleksander Skorek, AEDZ, tedyno and 13 more
This is for those who are using next/jest like we are.
Adding transformIgnorePatterns: ['/node_modules/(?!(axios)/)'], in jest.config didn't help because it wasn't set due to how next/jest works. To be able to set transformIgnorePatterns using next/jest you can follow this and then the suggestion for adding transformIgnorePatterns will work
This is still an issue with 1.1.2. I solved it by adding transformIgnorePatterns to my package.json. I also had to add react-native-daypicker to get that package to work with after upgrading axios jest:
Same problem here with "react-date-picker. After upgrading to axios 1.1.2 and "fixing" the jest issue using "transformIgnorePatterns" the tests fail with jest complaining about date picker. Adding that module also to ignore patterns does not help and does not feel right as it's a not related package.
This is for those who are using next/jest like we are.
Adding transformIgnorePatterns: ['/node_modules/(?!(axios)/)'], in jest.config didn't help because it wasn't set due to how next/jest works. To be able to set transformIgnorePatterns using next/jest you can follow this and then the suggestion for adding transformIgnorePatterns will work
will completely replace whatever you have in your custom jest config and also whatever comes from next jest. So if next/jest replaces/adds something it will be completely overwritten. The real issue is that next/jest adds node_modules as a hardcoded default which can be removed to resolve this issue, too:
even excluding it from the transformIgnorePattern does not work for me... when i run the tests in testenv "jsdom" it tries to load the node-lib of axios where it seems that URLSearchParams does not exist
However, this makes my Jest unit tests run about 2x longer. It would be nice if axios exported a module that supports Jest so we don't have to do this workaround?
Reacted by Imran Khan, Filipe Mariano, MihaiC, Žilvinas, lshawa, Felipe Pinto Becerra, Felipe Carvalho, Pravind21, Evgeny Kulikov, Vinícius Campitelli and 2 more
Reacted by xousik, lshawa, Felipe Carvalho, islam kamel and toothless13
Reacted by Michael Nelms, Robert Kirakosyan, Leevi Heino, A. Fox, Geraud Masson, Saloni Saloni, Hop Vu, Gabriel Lundmark, irene-chou, weiyang-xendit and 13 more
Reacted by Aqeel Bhat, Peter Fox and Manish Khedawat
Yah, that worked for me too (although I use npm instead of yarn :-).
It did require changing my client API calls to use the native fetch() APIs instead, but that was pretty straightforward since they were all concentrated in a set of modules encapsulating the server's API routes. Everything worked fine, including tweaking the middleware that passed along access tokens if the user was logged in.
I encountered the same error, and it turned into a nightmare. After debugging each package, I found this library to be the source of the issue.
Suggestion: If you don't need the latest version, please downgrade to version 0.xx.
(Till to 19/07/2024, at newest ver of axios, its still error. I try with Vite 5 and do the polyfill or custom inherits)
The suggestion about transformIgnorePatterns in a jest.config.file didn't help me either. I also tried to add the transformIgnorePatterns using the jest key in my package.json without any success.
However, the tests started to run after I inlined the flag:
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
I have encountered this error on a personal project and this update to package.json works as a temporary fix, is there a recommended way to solve this issue already or planned?
Activity
ddolcimascolo commentedon Oct 6, 2022
Yes, hit by this too. I suppose adding an exclusion to
transformIgnorePatternswould do it (not tested)dennybiasiolli commentedon Oct 6, 2022
@ddolcimascolo I can confirm, a temporary workaround is to add to
jest.config.jsCSWITH89 commentedon Oct 6, 2022
Tried adding a jest.config.js with:
And still encountering this error.
dennybiasiolli commentedon Oct 6, 2022
In another project with vue 2.7 and pinia, I had already set this
and it's working with the new axios too...but I don't know why 🥲
craigmcc commentedon Oct 7, 2022
Unfortunately, this still occurs with Axios 1.1.0 -- here's a link to a GitHub CI failure that illustrates the error:
https://github.com/cityteam/stats/actions/runs/3201927019/jobs/5230384125
I tried the suggestion about transformIgnorePatterns and it did not help either.
sharizard commentedon Oct 7, 2022
The suggestion about transformIgnorePatterns in a jest.config.file didn't help me either. I also tried to add the transformIgnorePatterns using the jest key in my package.json without any success.
However, the tests started to run after I inlined the flag:
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",yt-hzn commentedon Oct 7, 2022
This is for those who are using
next/jestlike we are.Adding
transformIgnorePatterns: ['/node_modules/(?!(axios)/)'],in jest.config didn't help because it wasn't set due to hownext/jestworks. To be able to settransformIgnorePatternsusingnext/jestyou can follow this and then the suggestion for addingtransformIgnorePatternswill workvisoft commentedon Oct 7, 2022
This is still an issue with
1.1.2. I solved it by addingtransformIgnorePatternsto mypackage.json. I also had to addreact-native-daypickerto get that package to work with after upgrading axios jest:vitalyster commentedon Oct 7, 2022
Still an issue in 1.1.2
pschyma commentedon Oct 8, 2022
Same problem here with "react-date-picker. After upgrading to axios 1.1.2 and "fixing" the jest issue using
"transformIgnorePatterns"the tests fail with jest complaining about date picker. Adding that module also to ignore patterns does not help and does not feel right as it's a not related package.adrcrv commentedon Oct 8, 2022
@yt-hzn is right! Here is how i fixed it:
pzi commentedon Oct 10, 2022
Note that
will completely replace whatever you have in your custom jest config and also whatever comes from next jest. So if next/jest replaces/adds something it will be completely overwritten. The real issue is that next/jest adds
node_modulesas a hardcoded default which can be removed to resolve this issue, too:This makes it less of a breaking change going forward and allows to still have a complete jest config set up and all in one place.
KillerCodeMonkey commentedon Oct 10, 2022
even excluding it from the transformIgnorePattern does not work for me... when i run the tests in testenv "jsdom" it tries to load the node-lib of axios where it seems that URLSearchParams does not exist
JeffreyStevens commentedon Oct 11, 2022
I hit this issue as well, I use create react app and therefore the workaround is to add to the package.json jest property this way:
However, this makes my Jest unit tests run about 2x longer. It would be nice if axios exported a module that supports Jest so we don't have to do this workaround?
abelardolg commentedon Oct 13, 2022
With all these lines, I received this error:
Error: You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
https://github.com/abelardolg/udemy_fherrera_vue3
71 remaining items
ajdruff commentedon Jun 29, 2023
This fixed it for me after receiving the axios error after installing an aws library
thereallukeguy commentedon Aug 5, 2023
This fixed it for me
yarn remove axioscraigmcc commentedon Aug 5, 2023
Yah, that worked for me too (although I use npm instead of yarn :-).
It did require changing my client API calls to use the native fetch() APIs instead, but that was pretty straightforward since they were all concentrated in a set of modules encapsulating the server's API routes. Everything worked fine, including tweaking the middleware that passed along access tokens if the user was logged in.
Update axios
adriandoinea commentedon Oct 2, 2023
This is the only thing that worked for me
package.json
"transformIgnorePatterns": ["node_modules/(?!axios)/"]unandreshevia commentedon Nov 11, 2023
I am pleased to report that the outcome was highly satisfactory with this solution.
hacker0limbo commentedon May 7, 2024
Anyone who faced this problem and have tried
transformIgnorePatternsbut does not work like me, please try usemoduleNameMapperinstead referring to this answer: https://stackoverflow.com/a/74297004/12733140SwapnaliY16 commentedon Jul 16, 2024
This is the only solution which worked for me! Thanks @hacker0limbo :)
b0yblake commentedon Jul 19, 2024
I encountered the same error, and it turned into a nightmare. After debugging each package, I found this library to be the source of the issue.
Suggestion: If you don't need the latest version, please downgrade to version 0.xx.
(Till to 19/07/2024, at newest ver of axios, its still error. I try with Vite 5 and do the polyfill or custom inherits)
conorheffron commentedon Mar 10, 2025
I have encountered this error on a personal project and this update to package.json works as a temporary fix, is there a recommended way to solve this issue already or planned?
See pull request at conorheffron/ironoc#298
Changes
jasonsaayman commentedon Apr 24, 2026
Please check the latest docs. This should no longer be the case, but if something is still amiss, please open an issue and tag me.
Update dependencies and move to aws sdk v3 (#860rt8jdk)