我目前遇到的问题是,新添加的方法调用会在已经测试过的函数中自动进行测试,从而增加了测试覆盖率,尽管这些新方法没有显式测试。
原始代码:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "name", "output" ];
declare readonly nameTarget: HTMLInputElement;
declare readonly outputTarget: HTMLElement;
greet() {
this.outputTarget.innerHTML = this.name;
}
get name() {
return this.nameTarget.value
}
}
字符串
对此进行测试:
import HelloController from '../controllers/hello_controller'
import { Application } from '@hotwired/stimulus';
describe("HelloController", () => {
describe("greet", () => {
beforeEach(() => {
document.body.innerHTML = `<div data-controller="hello">
<input class="input" data-hello-target="name" type="text">
<p class="output" data-hello-target="output"></p>
<button data-action="click->hello#greet">Greet</button>
</div>`;
const application = Application.start();
application.register("hello", HelloController);
});
it("greets Person with given name", () => {
const input = document.querySelector(".input") as HTMLInputElement;
const output = document.querySelector(".output");
const button = document.querySelector("button");
expect(output.innerHTML).toEqual("");
input.value = "foo";
button.click();
expect(output.innerHTML).toEqual("foo");
});
});
});
型
现在,新的方法被添加到类(_doSomething
)中:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "name", "output" ];
declare readonly nameTarget: HTMLInputElement;
declare readonly outputTarget: HTMLElement;
greet() {
this.outputTarget.innerHTML = this.name;
this._doSomething();
}
get name() {
return this.nameTarget.value
}
_doSomething(): void {
const wert = document.querySelector('body').getAttribute('data-ab-test');
}
}
型
测试未变更。
如果我再上《笑话》的话。新增方法100%覆盖。虽然没有测试。
在我看来,新方法应该被认为是未经测试的,代码覆盖率应该降低。但是覆盖率保持在100%,并且新方法被显示为经过测试的。我错过了什么?
1条答案
按热度按时间mnowg1ta1#
Jest将测试执行期间调用的所有方法都视为代码覆盖率报告中的“已覆盖”。要从覆盖范围中排除特定的方法,可以在Jest配置中使用exclude配置选项,或者在方法定义之前添加注解/* 伊斯坦布尔ignore next */。
选项1:使用.coveragerc文件
在项目的根目录下创建一个.coveragerc文件(如果还没有的话)。
将以下配置添加到.coveragerc文件
字符串
或-选项2:在代码中使用注解
型