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

  1. Use React and Axios latest version
  2. Create Jest test
  3. 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

Most upvoted comments

I found that this did however work for me because it forces those imports to resolve with the CJS module instead:

  moduleNameMapper: {
    '^axios$': require.resolve('axios'),
  },

"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\""

this worked, when I restart test command

Works for me:

moduleNameMapper: {
    'axios': 'axios/dist/node/axios.cjs'
}

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

"jest": {
    "transformIgnorePatterns": ["node_modules/(?!axios)/"]
},

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:

npm i axios@1.2.0-alpha.1

Please provide feedback in either the pinned issue or the discussion thread 🧵

I found that this did however work for me because it forces those imports to resolve with the CJS module instead:

  moduleNameMapper: {
    '^axios$': require.resolve('axios'),
  },

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.

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

  1. Use React and Axios latest version
  2. Create Jest test
  3. 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)/\"",

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/*

Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled code. Since all files inside node_modules are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may use transformIgnorePatterns to allow transpiling such modules. You’ll find a good example of this use case in React Native Guide.

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" } },

In my case, I was also facing the same problem So I got to solve with this:

"jest": {
    "transformIgnorePatterns": ["node_modules/(?!axios)/"]
},

That’s why we moved to Vitest.

but have one question jest can’t work with Vite? Even knowing Vitest is integrated with jest, So still vitest should used? btw, I’m also using Vite

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.

I found that this did however work for me because it forces those imports to resolve with the CJS module instead:

  moduleNameMapper: {
    '^axios$': require.resolve('axios'),
  },

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.