admin管理员组文章数量:1289911
I am working on backend Typescript project where I am trying to get coverage report for unit test case. Jest returns empty coverage report in terminal as well as in html report stating nothing. I also tried with -- --coverage --watchAll=false
but it also returns the empty document.
package.json
"scripts":{
"unit-test": "jest --config='./config/jest/jest_unit.config.js' --forceExit --detectOpenHandles",
}
jest_unit.config.js
/**
* @file Jest Unit Test Configuration File
*/
module.exports = {
roots: ['../../tests'],
testRegex: '.*_unit.test.(js|ts|tsx)?$',
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
moduleFileExtensions: ['ts', 'js'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testEnvironment: 'node',
reporters: [
'default',
[
'../../node_modules/jest-html-reporter',
{
pageTitle: 'Unit Test Report',
outputPath: 'tests/reports/unit-test-report.html',
includeFailureMsg: true,
},
],
],
collectCoverage: true,
collectCoverageFrom: [
'../../api/**/*.ts'
],
moduleFileExtensions: ["ts", "js", "json"],
coverageDirectory: "../../coverage",
coveragePathIgnorePatterns: [
"<rootDir>/node_modules"
],
coverageReporters: [
"json",
"lcov",
"text"
],
coverageThreshold: {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
};
Folder Structure
|-- app
|-- controllers
|-- schemas
|-- config
|-- jest
|-- jest_unit.config.js
|-- package.json
|-- tests
|-- api
|-- modules
|-- m1
|-- controllers
|-- m1_controller_unit.test.ts
|-- m1_controller_integration.test.ts
|-- m2
|-- models
|-- m1_model_unit.test.ts
|-- m1_model_integration.test.ts
|-- m3
|-- schemas
|-- m1_schema_unit.test.ts
|-- m1_schema_integration.test.ts
Jest Coverage htm and terminal shows no coverage lines
I am working on backend Typescript project where I am trying to get coverage report for unit test case. Jest returns empty coverage report in terminal as well as in html report stating nothing. I also tried with -- --coverage --watchAll=false
but it also returns the empty document.
package.json
"scripts":{
"unit-test": "jest --config='./config/jest/jest_unit.config.js' --forceExit --detectOpenHandles",
}
jest_unit.config.js
/**
* @file Jest Unit Test Configuration File
*/
module.exports = {
roots: ['../../tests'],
testRegex: '.*_unit.test.(js|ts|tsx)?$',
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
moduleFileExtensions: ['ts', 'js'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testEnvironment: 'node',
reporters: [
'default',
[
'../../node_modules/jest-html-reporter',
{
pageTitle: 'Unit Test Report',
outputPath: 'tests/reports/unit-test-report.html',
includeFailureMsg: true,
},
],
],
collectCoverage: true,
collectCoverageFrom: [
'../../api/**/*.ts'
],
moduleFileExtensions: ["ts", "js", "json"],
coverageDirectory: "../../coverage",
coveragePathIgnorePatterns: [
"<rootDir>/node_modules"
],
coverageReporters: [
"json",
"lcov",
"text"
],
coverageThreshold: {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
};
Folder Structure
|-- app
|-- controllers
|-- schemas
|-- config
|-- jest
|-- jest_unit.config.js
|-- package.json
|-- tests
|-- api
|-- modules
|-- m1
|-- controllers
|-- m1_controller_unit.test.ts
|-- m1_controller_integration.test.ts
|-- m2
|-- models
|-- m1_model_unit.test.ts
|-- m1_model_integration.test.ts
|-- m3
|-- schemas
|-- m1_schema_unit.test.ts
|-- m1_schema_integration.test.ts
Jest Coverage htm and terminal shows no coverage lines
Share Improve this question edited Apr 8, 2023 at 18:00 MatthewMartin 33.2k33 gold badges115 silver badges172 bronze badges asked Nov 7, 2020 at 12:22 muthumuthu 8032 gold badges12 silver badges28 bronze badges5 Answers
Reset to default 6I was able to fix this issue by moving config file to root. Everything is good here in this configuration. I don't know why jest behaves like this.
Updated structure
Folder Structure
|-- app
|-- controllers
|-- schemas
|-- jest_unit.config.js
|-- package.json
|-- tests
|-- api
|-- modules
|-- m1
|-- controllers
|-- m1_controller_unit.test.ts
|-- m1_controller_integration.test.ts
|-- m2
|-- models
|-- m1_model_unit.test.ts
|-- m1_model_integration.test.ts
|-- m3
|-- schemas
|-- m1_schema_unit.test.ts
|-- m1_schema_integration.test.ts
package.json
"scripts":{
"unit-test": "jest --config='./jest_unit.config.js' --forceExit --detectOpenHandles",
}
Try changing your collectCoverageFrom
as follows:
collectCoverageFrom: [
'../../tests/**'
],
The **
means to include all files, recursively. Details are at https://jestjs.io/docs/en/configuration#collectcoveragefrom-array
I got it working with this
package.json
...
"scripts": {
...
"test": "jest --maxWorkers=1 --coverage"
...
jest.config.js
module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
isolatedModules: true
}
},
testPathIgnorePatterns: ['.d.ts', '.js'],
collectCoverageFrom: [
'**/*.ts',
'!**/build/**',
'!**/node_modules/**',
'!**/vendor/**'
]
};
I'm also facing that issue randomly, I don't have a fix or know why, but clearing the cache and running it again always works:
jest --clearCache
jest --coverage
I believe this might be related to ts-jest not jest itself.
Make sure your coveragePathIgnorePatterns does not have directory with tests that supposed to be cover.
本文标签: javascriptJest coverage in a node typescript project always returns emptyStack Overflow
版权声明:本文标题:javascript - Jest coverage in a node typescript project always returns empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414467a2377430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论