繁体   English   中英

没有使用 react-scripts 和 jest 找到测试

[英]No tests found using react-scripts and jest

试图只运行测试。

我需要测试减速器和存储

我阅读了 jest tutorial-react 手册,它与我的有点不同,但我还发现“react-scripts test”也应该起作用的信息

我的 package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "classnames": "^2.3.1",
    "faker": "^4.1.0",
    "jest": "^27.4.7",
    "miragejs": "^0.1.43",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "redux": "^4.1.2",
    "seedrandom": "^3.0.5",
    "web-vitals": "^2.1.3"
  },
  "devDependencies": {
    "jest-watch-typeahead": "^0.6.5",
    "prettier": "^2.5.1"
  }

我正在尝试使用

npm test
npm test --config jest.config.js
npm test --setupFile ./src/setupTests.js
npm test --setupFiles ./src/setupTests.js

Output

Active Filters: filename .\\src\\setupTests.js/
 › Press c to clear filters.
 
Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.
 

按 A

No tests found, exiting with code 0

Watch Usage: Press w to show more.

./jest.config.js

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
    setupFiles: ['./src/setupTests.js'], 
    verbose: true,
};

module.exports = config;

./src/setupTests.js

import '@testing-library/jest-dom/extend-expect';

test('adds 1 + 2 to equal 3', () => {
    expect(1 + 2).toBe(3);
});

这是 Jest 文档中关于它如何查找测试文件的说明:

默认情况下,它会在__tests__文件夹中查找.js.jsx.ts.tsx文件,以及任何后缀为.test.spec的文件(例如Component.test.jsComponent.spec.js ) . 它还会找到名为test.jsspec.js的文件。

由于setupTests.js文件名不符合此标准,因此 Jest 不会将其识别为测试文件。 如果您将文件重命名为以.test.js或将其移动到__tests__文件夹中,Jest 应该能够找到它并运行测试。 如果您需要保持文件名不变,可以通过在jest.config.js中设置testRegex来更改此默认行为(在此处阅读有关此设置的更多信息)。

jest.config.js中的setupFiles属性用于在测试实际运行之前设置环境。 可以在此处找到该设置的文档,但似乎不需要为您提供的这种基于案例的代码进行任何设置。

暂无
暂无

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

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