我正在测试一个带有 Jest 的 Angular 项目(使用 jest-preset-angular
)。
在收集报道时,我得到一个未覆盖的分支,我不明白为什么。我可以用 3 个文件重现这个问题。some-dependency.ts
export class SomeDependency {}
some-service.ts
import { Injectable } from '@angular/core';
import { SomeDependency } from './some-dependency';
@Injectable()
export class SomeService {
constructor(private dependency: SomeDependency) {
console.log('service created');
}
}
some-service.spec
import { SomeService } from './some-service';
describe('DerivedClass', () => {
it('should create', () => {
expect(new SomeService(null)).toBeTruthy();
});
});
通过运行
yarn jest --coverage some-service
,我得到以下报道:--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files | 100 | 75 | 100 | 100 | |
some-dependency.ts | 100 | 100 | 100 | 100 | |
some-service.ts | 100 | 75 | 100 | 100 | 6 |
--------------------|----------|----------|----------|----------|-------------------|
在 HTML 报告中,关于未发现内容的信息太少。
我注意到删除
@Injectable
装饰器使覆盖率恢复到 100%有人有解释吗?有没有办法在保持
@Injectable
的同时获得 100% 的覆盖率装饰师?编辑:我添加了一个 console.log 来证明构造函数被正确调用。黄色突出显示由 Istambul 报告给出,有助于查看未覆盖的分支。但是这里对我来说没有分支,因为没有条件。
最佳答案
通过与@markusdresch创建的全新项目的覆盖率确实是100%的对比,我终于发现了一个ts-jest
jest.config.js
中设置的选项对代码覆盖率造成副作用。
{
// ...
globals: {
'ts-jest': {
// ...
isolatedModules: true,
},
},
}
isolatedModules
设置为 true 会导致原始问题中描述的未覆盖分支。通过将其设置为 false 或将其删除,覆盖率将恢复为 100%。我希望我可以使用
isolatedModules = true
并且仍然有 100% 的覆盖率,但我想这应该是一个全新的问题。
关于angular - Jest 报道中意外发现的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516328/