我正在測驗reducers和storage。我閱讀了jest 教程反應手冊,它與我的有點不同,但我還發現“反應腳本測驗”也應該作業的資訊
包.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);
});
uj5u.com熱心網友回復:
這是 Jest 檔案中關于它如何查找測驗檔案的說明:
默認情況下,它會查找檔案夾內的 、 和 檔案
.js,以及.jsx任何帶有 or 后綴的檔案(例如or )。它還將查找名為or的檔案。.ts.tsx__tests__.test.specComponent.test.jsComponent.spec.jstest.jsspec.js
由于setupTests.js檔案名不符合此標準,因此 Jest 不會將其識別為測驗檔案。如果您將檔案重命名為結尾.test.js或將其移動到__tests__檔案夾中,Jest 應該能夠找到它并運行測驗。如果您需要保持檔案名不變,可以通過設定更改此默認行為(testRegex在此處jest.config.js閱讀有關此設定的更多資訊)。
setupFilesin 屬性用于在jest.config.js測驗實際運行之前設定環境。可以在此處找到該設定的檔案,但似乎不需要為您提供的這種基于案例的代碼進行任何設定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416392.html
標籤:
