简体   繁体   English

可以运行 React App 或使用 babel-jest 对其进行测试

[英]Can either run React App or tests for it with babel-jest

I am trying to set up tests for the existing React app.我正在尝试为现有的 React 应用程序设置测试。 We are using React 16.8.13 After some struggle I've made it work.我们正在使用React 16.8.13经过一番努力,我让它工作了。 For that I had to install jest 24.9.0 and babel-jest 24.9.0 .为此,我必须安装jest 24.9.0babel-jest 24.9.0 The tests run fine and I am getting a coverage report.测试运行良好,我得到了覆盖率报告。 However when I run yarn start I am getting an error:但是,当我运行yarn start时出现错误:

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "23.6.0"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-jest was detected higher up in the tree:

  /<Path to my App>/node_modules/babel-jest (version: 24.9.0) 

removing both jest and babel-jest from devDependencies fixes the issue, but now running yarn test gives me an error:devDependencies中删除jestbabel-jest解决了这个问题,但是现在运行yarn test给了我一个错误:

No tests found
In /<Path to my App>
  32 files checked.
  testMatch:  - 32 matches
  testPathIgnorePatterns: /node_modules/ - 32 matches
  testRegex: (\/tests\/.*\.test)\.[jt]sx?$ - 0 matches
Pattern:  - 0 matches
error Command failed with exit code 1.

What do I need to do to have both app and tests runnable?我需要做什么才能使应用程序和测试都可运行?

Based on the No tests found , it looks like your tests might not be in the right folder.根据No tests found ,您的测试似乎不在正确的文件夹中。 They should be in a folder in src called __tests__ and have a test.js or spec.js suffix.它们应该位于src中名为__tests__的文件夹中,并具有test.jsspec.js后缀。

See this answer for more info: Create React App cannot find tests有关更多信息,请参阅此答案: 创建 React App 找不到测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM