Jest setup “SyntaxError: Unexpected token export”

前端 未结 4 1693
迷失自我
迷失自我 2020-12-24 11:31

I\'m implementing tests into an existing project that currently has no tests. My tests are failing to compile node_modules/ imports.

/Users/me/m         


        
相关标签:
4条回答
  • 2020-12-24 11:39

    For create-react-app users who are looking for a fix, here's what worked for me:

    // package.json
    ...
      "jest": {
        "transformIgnorePatterns": [
          "<rootDir>/node_modules/(?!lodash-es)"
        ]
      },
    ...
    

    Overriding options in jest.config.js file didn't work for me. Keep in mind that not every option can be overridden, here's a list of supported options: https://create-react-app.dev/docs/running-tests#configuration

    0 讨论(0)
  • 2020-12-24 11:42

    If none of the above solutions worked for you, you can try this in your jest

    "moduleNameMapper": {
        "^lodash-es$": "lodash"
    }
    

    It will replace lodash-es with the commonjs version during testing runtime.

    0 讨论(0)
  • 2020-12-24 11:42

    Posting a more complete answer here:

    Jest by default does not transform node_modules because node_modules is huge. Most node modules are packaged to expose ES5 code because this is runnable without any further transformation (and largely backwards compatible).

    In your case, lodash-es specifically exposes ES modules, which will need to be built by Jest via babel.

    You can try narrowing your whitelist down so Jest doesn't try to pass every javascript file within node_modules through babel.

    I think the correct configuration in your case is:

    "jest": {
      "transformIgnorePatterns": [
        "/!node_modules\\/lodash-es/"
      ]
    }
    
    0 讨论(0)
  • 2020-12-24 11:57

    I had to add this into my .jestconfig:

    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!lodash-es)"
    ]
    
    0 讨论(0)
提交回复
热议问题