New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating Axios from 0.27.2 to 1.0.0 breaks Jest tests in a create-react-app app #5026
Comments
Yes, hit by this too. I suppose adding an exclusion to |
@ddolcimascolo I can confirm, a temporary workaround is to add to transformIgnorePatterns: ['/node_modules/(?!(axios)/)'], |
Tried adding a jest.config.js with:
And still encountering this error. |
In another project with vue 2.7 and pinia, I had already set this transformIgnorePatterns: [
'node_modules/(vue-demi)',
], and it's working with the new axios too...but I don't know why 🥲 |
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. |
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:
|
This is for those who are using Adding |
This is still an issue with "jest": {
"transformIgnorePatterns": [
"/node_modules/(?!(axios|react-day-picker)/)"
]
}, |
Still an issue in 1.1.2 |
Same problem here with "react-date-picker. After upgrading to axios 1.1.2 and "fixing" the jest issue using |
@yt-hzn is right! Here is how i fixed it: // jest.config.js file
const nextJest = require('next/jest');
const createJestConfig = nextJest({ dir: './' });
const customJestConfig = {
// Custom Config Values
};
module.exports = async () => {
const asyncConfig = createJestConfig(customJestConfig);
const config = await asyncConfig();
const transformIgnorePatterns = ["node_modules/(?!axios)/"]
return {...config, transformIgnorePatterns}
} |
Note that module.exports = async () => {
const asyncConfig = createJestConfig(customJestConfig);
const config = await asyncConfig();
const transformIgnorePatterns = ["node_modules/(?!axios)/"]
return {...config, transformIgnorePatterns}
} 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 module.exports = async () => {
const asyncConfig = createJestConfig(customJestConfig);
/** @type {import('@jest/types/build/Config').InitialOptions} */
const config = await asyncConfig();
const nodeModulePatternIndex = config.transformIgnorePatterns.findIndex(
(pattern) => pattern === '/node_modules/'
);
if (nodeModulePatternIndex >= 0) {
config.transformIgnorePatterns.splice(nodeModulePatternIndex, 1);
}
return config;
}; 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. |
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 |
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? |
With all these lines, I received this error: |
hope this is going to be fixed soon ... |
I have same issues with react-native project. Downgrade to 0.27.2 and fine. Hope this issue will be resolve soon. |
This works for me:
moduleNameMapper: {
'^axios$': require.resolve('axios'),
}, ref: #5101 (comment) |
By the looks of it, 1.0.0+ is an ES module, so we can't use it in CommonJS projects. |
Thank you, it's work |
For those for who Made the difference in my project, not sure why... |
It works on my Vue3 project ! |
same issue with "jest": "^29.5.0", "axios": "^1.3.4" and None of these solutions works for me. |
Another odd data point: using axios@1.4.0 and jest@27 (due to create-react-app, although I tried manually upgrading to jest@29 and it didn't seem to affect this scenario), the |
This helped me |
|
This addition to |
Given the amount of time that has gone on since my original bug report, and the fact that the only potential workarounds have all been just that -- workarounds (even in my experiments are only sketchily working for me) -- I'm giving up on Axios. The native Node and browser implementations of the fetch APIs have satisfied my requirements, without this kind of hassle, so it's time to reduce dependencies in my various apps. One app at a time. FWIW, the React version I originally reported this for (18.2) is still current, which therefore means it will apply to anyone who uses Create React App (CRA) and even the current version of Axios (1.4), today. Truth be told, this does make me a little sad ... Axios was the first HTTP API that worked well for me on both the client and server sides. But time marches on. And I cannot afford to be stuck in a world where there is (say) a security vulnerability in Axios 0.whatever, that is only fixed in Axios 1.whatever, which would mean invalidating a very large portion of my client side tests. |
Just add "jest": { in package.json |
Speaking just for myself, I do not want a workaround. I want the libraries I use to not break my tests when I “upgrade” the version, with no changes to my tests. One might like to argue that the change from 0.x to 1.x is a “major” version change. Go for it. I do not care any more. Fetch does what I need, without an external dependency to worry about. Especially when it is over an issue of packaging, not even an API change. |
This fixed it for me after receiving the axios error after installing an aws library
|
This fixed it for me |
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. |
The new axios version adds content-encoding gzip header. Jest needs to be also updated. More info: axios/axios#5026
This is the only thing that worked for me |
I am pleased to report that the outcome was highly satisfactory with this solution. |
Anyone who faced this problem and have tried
|
I have several React applications created with a Typescript based create-react-app application (and using react-scripts@5.0.1, which depends on jest@27.4.3). 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 Axios@1.0.0 now emits only an ESM module, and no longer a CJS module, which causes known errors with Jest.
Test Runtime Error
Environment
The text was updated successfully, but these errors were encountered: