Jest 没有找到我的所有测试文件。它忽略一个目录。当我运行 npm run jest:watch 我在 ./server 中的测试及其子目录进行了测试,但在 ./client
中找不到测试我尝试过的
我尝试创建一个通用测试文件并移动它。我发现它在以下情况下会起作用。 我还尝试过使用文件的直接路径 - 但这不起作用。
可能的原因
我认为这可能与我不寻常的目录结构有关
./client
./server
相对于
./server.js
./other server files..
./client/...client files
或者对我来说尝试从node.js应用程序运行jest并让它查看客户端目录中的文件(其中包含用React编写的应用程序)可能会失败 - 我不关心在其上运行测试 react 组件(仅限模块)。或者,这可能与客户端目录中的 Jest 有关,搞乱了事情。
我的设置
我的文件夹结构如下所示
./
./client (react client app)
./client/package.json
./client/node_modules
package.json
node.modules
./server (node.js api app)
./server/package.json
我的 Jest 是从 ./package.json 文件运行的。
我尝试过的其他事情
我尝试对我的文件使用直接路径,以下代码有效
"testMatch": ["<rootDir>/server/delete.test.js"],
但是这段代码(在我想要的文件夹中)没有
"testMatch": ["<rootDir>/client/delete.test.js"],
代码设置
./package.json
{
"name": "Infoshot",
"version": "0.0.4",
"description": "Research Tool",
"main": "cd ./server && server.js",
"scripts": {
"start": "cd ./server && node server.js",
"server": "cd ./server && nodemon server.js",
"client": "npm start --prefix ./client/",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "cd ./client && NPM_CONFIG_PRODUCTION=false npm install && CI= npm run build",
"compile-saas": "node-sass ./client/src/scss/a_main.scss ./server/nodeClient/public/css/main.css -w",
"test": "jest",
"test:watch": "npm run test -- --watch"
},
"jest": {
"testEnvironment": "node",
"testPathIgnorePatterns": [
"./node_modules"
]
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.0",
"bcryptjs": "^2.4.3",
"config": "^3.2.2",
"dotenv": "^8.2.0",
"ejs": "^3.1.5",
"enzyme": "^3.11.0",
"express": "^4.17.1",
"express-validator": "^6.2.0",
"ijavascript": "^5.2.0",
"jest": "^26.6.3",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.7.0",
"nodemailer": "^6.4.14",
"nodemailer-sendgrid": "^1.0.3"
},
"devDependencies": {
"concurrently": "^4.1.2",
"node-sass": "^5.0.0",
"nodemon": "^1.19.2"
}
}
删除.test.json
describe.only('DELETE TEST: getDocument', () => {
test('Say Hi', async () => {
const bum = 'bum';
expect(bum).toBe(bum);
});
});
./client/package.json
正如我之前所说,我认为这无关紧要,因为它不会运行,因为我使用 npm run jest:watch 从 ./server 运行 jest
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"config": "^3.3.2",
"dotenv": "^8.2.0",
"enzyme-adapter-react-16": "^1.15.3",
"node-sass": "^4.14.1",
"react": "^16.9.0",
"react-beautiful-dnd": "^13.0.0",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.4.4",
"react-transition-group": "^4.3.0",
"serve": "^11.3.2",
"styled-components": "^5.1.1",
"uuid": "^3.3.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"previewProd": "react-scripts build && serve"
},
"jest": {
"collectCoverageFrom": [
"src/components/documentEditor/*.js",
"<rootDir>/server/",
"!<rootDir>/node_modules/",
"!<rootDir>/src/index.js",
"!<rootDir>/src/registerServiceWorker.js"
]
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000",
"devDependencies": {
"@testing-library/react": "^10.4.9"
}
}
最佳答案
我发现,如果我删除了 ./client/package.json 中所有提及 jest 的内容,并将 ./package.json 替换为
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**"
]
成功了。
关于javascript - 开 Jest 没有找到所有测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65188011/