- 问题描述:**
我已经为一个typescript类编写了两个测试,这两个测试都通过了,所以jest成功地检索到了测试文件,然后我使用了--coverage选项,但是看起来jest没有在这里选择覆盖的文件。
下面是我得到的输出:
api_jester | PASS src/tests/repositories/user.test.ts
api_jester | User Repository
api_jester | ✓ it should return an empty array (18ms)
api_jester | ✓ should successfully create a user and return its data (7ms)
api_jester |
api_jester | ----------|----------|----------|----------|----------|-------------------|
api_jester | File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
api_jester | ----------|----------|----------|----------|----------|-------------------|
api_jester | All files | 0 | 0 | 0 | 0 | |
api_jester | ----------|----------|----------|----------|----------|-------------------|
api_jester | Test Suites: 1 passed, 1 total
api_jester | Tests: 2 passed, 2 total
api_jester | Snapshots: 0 total
api_jester | Time: 3.208s
api_jester | Ran all test suites.
我试过使用collectCoverageFrom选项,但没有任何成功。我用github上的一些简单例子测试了覆盖,这些例子都能正常工作,所以问题不是来自我的环境。我猜我在配置中不知何故漏掉了一些东西,但我在这上面花了太多时间,我有点沮丧,所以也许一些新的外观会有所帮助。
- 项目架构:**
config
|__ jest.config.js
|__ tsconfig.json
src
|__tests
| |__repositories
| |__user.test.ts
|__repositories
|___ userRepository
|__User.ts
- 测试配置js:**
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["../src/tests/"],
transform: {
"^.+\\.tsx?$": "ts-jest"
},
collectCoverageFrom: ["../src/"],
moduleFileExtensions: ["ts", "js", "json"],
coverageDirectory: "../coverage"
};
- 包. json**
{
"name": "theralog_api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"build": "tsc",
"prettier": "npx prettier --write src/**/*.ts --config ./config/.prettierrc",
"eslint": "npx eslint --config ./config/.eslintrc ./src/**/**/*",
"start:dev": "npx nodemon -L --config ./config/api.nodemon.json",
"test:watch": "npx nodemon -L --config ./config/jester.nodemon.json",
"test:coverage": "npx jest --config ./config/jest.config.js --coverage --colors --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/compression": "^1.0.1",
"@types/express": "^4.17.1",
"@types/graphql-depth-limit": "^1.1.2",
"@types/jest": "^24.0.23",
"@types/node": "^12.7.12",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.5.0",
"apollo-server-testing": "2.9.7",
"babel-jest": "^24.9.0",
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"graphql-depth-limit": "^1.1.0",
"graphql-import": "^0.7.1",
"graphql-import-node": "0.0.4",
"jest": "^24.9.0",
"nodemon": "^1.19.3",
"prettier": "^1.18.2",
"ts-jest": "^24.1.0",
"ts-node": "^8.4.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.2"
},
"dependencies": {
"apollo-server-express": "^2.9.6",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"graphql": "^14.5.8",
"http": "0.0.0",
"lodash": "^4.17.15",
"ncp": "^2.0.0",
"pg": "^7.12.1",
"winston": "3.2.1"
}
}
- 小丑,恶魔,杰森**
{
"watch": ["../src"],
"ext": "ts",
"exec": "npx jest --config ./config/jest.config.js --watchAll"
}
3条答案
按热度按时间j8ag8udp1#
jest.config.js中缺少设置
collectCoverage: true
我还使用了更具描述性的collectCoverageFrom:
这样我就排除了一些不想计算覆盖率的文件,比如我的模块、模拟和测试。
我的完整文件与原始的Jest初始化过程和注解。
有关每个配置属性的详细说明,请访问:the Jest documentation
wqsoz72f2#
经过几页纸的大量研究,这对我得到覆盖率报告起到了作用:
将以下行置于脚本下:
并且,在package.json文件中添加以下jest配置代码,如下所示:
然后跑
r1zhe5dt3#
显然,你必须把你的源文件添加到 * roots * 中才能使它工作。
而不是这个:
还包括源文件:
之后,如果
collectCoverageFrom
正确,则按预期检测到覆盖率为0%的所有文件。