React-Scripts 和 Jest - 运行 npm test 上未找到测试
React-Scripts and Jest - No tests found on running npm test
我正在测试 reducers 和 storage。
我阅读了 jest
tutorial-react 手册,它与我的有点不同,但我还发现“react-scripts 测试”也应该有效的信息
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
输出
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 文档是这样描述它如何找到测试文件的:
By default it looks for .js
, .jsx
, .ts
and .tsx
files inside of __tests__
folders, as well as any files with a suffix of .test
or .spec
(e.g. Component.test.js
or Component.spec.js
). It will also find files called test.js
or spec.js
.
由于 setupTests.js
文件名不符合此条件,Jest 不会将其识别为测试文件。如果您重命名文件以 .test.js
结尾或将其移动到 __tests__
文件夹中,Jest 应该能够找到它并且 运行 测试。如果您需要保持文件名不变,可以通过在 jest.config.js
中设置 testRegex
来更改此默认行为(阅读有关此设置的更多信息 here)。
jest.config.js
中的setupFiles
属性实际用于测试前的环境搭建运行。可以找到该设置的文档 here,但似乎没有必要为您提供的这种基于案例的代码进行任何设置。
我正在测试 reducers 和 storage。
我阅读了 jest
tutorial-react 手册,它与我的有点不同,但我还发现“react-scripts 测试”也应该有效的信息
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
输出
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 文档是这样描述它如何找到测试文件的:
By default it looks for
.js
,.jsx
,.ts
and.tsx
files inside of__tests__
folders, as well as any files with a suffix of.test
or.spec
(e.g.Component.test.js
orComponent.spec.js
). It will also find files calledtest.js
orspec.js
.
由于 setupTests.js
文件名不符合此条件,Jest 不会将其识别为测试文件。如果您重命名文件以 .test.js
结尾或将其移动到 __tests__
文件夹中,Jest 应该能够找到它并且 运行 测试。如果您需要保持文件名不变,可以通过在 jest.config.js
中设置 testRegex
来更改此默认行为(阅读有关此设置的更多信息 here)。
jest.config.js
中的setupFiles
属性实际用于测试前的环境搭建运行。可以找到该设置的文档 here,但似乎没有必要为您提供的这种基于案例的代码进行任何设置。