添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

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';
      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)/\"",
    
    "test": "react-app-rewired test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
              

    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.

    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'),
      pzi, marques-work, imransilvake, tomasklingen, junkor-1011, Baldrani, Guiqft, akozlov75, maxim-bliznetsov, shanelau, and 106 more reacted with thumbs up emoji
      Baldrani, matheusslg, wallynm, marceviana, Wooulf, hshine1226, Sergio-Pedretti, CharlesICON, botermbr, anddersonrds, and 31 more reacted with hooray emoji
      matheusslg, wallynm, Wooulf, hshine1226, Sergio-Pedretti, anddersonrds, 0x20F, kallehult, uzZ, jestanoff, and 35 more reacted with heart emoji
      Baldrani, matheusslg, wallynm, Wooulf, hshine1226, juanpicado, mtakeda, Sergio-Pedretti, CharlesICON, anddersonrds, and 27 more reacted with rocket emoji
        All reactions
              

    @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 undefined and therefore i get an error => e.g. Cannot read properties of undefined (reading 'get')

    pasha1248, StefanoConsonni, izakVoigt, 77pintu, qqKostya, Gust4voSales, fre-ben, Hermann-237, Raul220, sane4ek1994, and 11 more reacted with thumbs up emoji jinal1788 reacted with thumbs down emoji imransilvake, kkrull, slimbde, kiminpyo, dantersack, drasolon, Klevens, fyodore82, andrewthenew, FarNys, and 14 more reacted with hooray emoji All reactions

    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

    Ibaigilbiko, tarcon, andreiPereira, jamiehaywood, timo-klarshift, brunosana, bor01, design1online, and IustinPisticiuc reacted with thumbs up emoji serkon and Havardmj reacted with thumbs down emoji All reactions missjennbo, single9, EverStarck, StefanoConsonni, Azfletch, thifelipesilva, giovanni-bertoncelli, clsechi, jamesryan-dev, eleazar1595, and 5 more reacted with thumbs up emoji giovanni-bertoncelli and MattV-NL reacted with laugh emoji giovanni-bertoncelli, NiharR27, Stalinko, MattV-NL, and vignesh-krossark reacted with heart emoji single9, charithf, EverStarck, Azfletch, jmoyson, nhotalling-viagio, giovanni-bertoncelli, AndriScherbakov, jamesryan-dev, animir, and MattV-NL reacted with rocket emoji All reactions

    Try upgrading jest to latest (>29.2.x), it worked for me without having to change jest config.

    You save my time!

    christian98, kurund, kurpav, voronindenis, aleksandricbm, SashaZel, suhasrao24, Tskxe33, YuHyun-P, tanbt, and 21 more reacted with thumbs up emoji DeepakSharma04 reacted with laugh emoji BorisDmv, jvclbr, klaevv, and brt-henry reacted with heart emoji rcbevans, kurpav, miroberes, Stevensierraq, tanbt, DeepakSharma04, naziio, and erykmoskala reacted with rocket emoji All reactions

    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';
          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)/\"",
    
    "test": "react-app-rewired test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
    

    Thank You it worked

    Any permanent solution Found ??