Jest not preprocessing my JSX

前端 未结 4 1900
野性不改
野性不改 2020-12-17 09:47

I am following the Jest tutorial to test a react component and am running into preprocessing issues with my jsx. I assume the error is due to preprocessing, the error messag

相关标签:
4条回答
  • 2020-12-17 10:16

    I think you may just need to add the testFileExtensions and testFileExtensions to the jest section of your package.json.

    See the README.md of babel-jest:

    https://github.com/babel/babel-jest

    0 讨论(0)
  • 2020-12-17 10:30

    Changing my .babelrc config file to babel.config.js or babel.config.json worked for me because Jest ignores .babelrc.

    0 讨论(0)
  • 2020-12-17 10:32

    Using a .bablerc file in the project root directory fixed it for me.

    I was not using a .babelrc file while developing because I defined my presets in the webpack configuration file. But it turns out that when you run the unit test with jest, then jest is not aware of this presets as it does not know about webpack. So simply adding a .babelrc file with the presets should solve the issue for you too.

    Contents of .babelrc:

    { "presets": ["es2015", "react"] }

    0 讨论(0)
  • 2020-12-17 10:36

    I had a similar problem, and the solution was adding this to jest config file:

    "transform": {
          "^.+\\.js$": "babel-jest",
          "^.+\\.jsx$": "babel-jest"  // This line was missing
    }
    

    The reason it was needed in our project, is because we overridden the default "transform" value in jest config file.

    0 讨论(0)
提交回复
热议问题