[英]jest test cases for typescript function with no return
我在 reactjs 中有组件-
export const initializeValues=()=>{
if(window.variable1$==undefined || window.variable1$==null)
{
window.variable1$=const.variable1Value;
}
if(window.variable2$==undefined || window.variable2$==null)
{
window.variable2$=const.variable2Value;
}
}
我开玩笑地为此编写了测试用例 -
describe("initializeValues",()=>{
test("Variable1",()=>{
expect(window.variable1$).toBeUndefined();
});
test("Variable2",()=>{
expect(window.variable2$).toBeUndefined();
});
});
上面写的测试用例运行成功。 但它只显示了 5% 的代码覆盖率。
当 function 没有返回任何内容时,如何添加更多测试用例? (仅分配值)
我不太确定const.variable1Value
是什么,但无论如何。 在测试中,您应该准备测试前提条件并运行测试的 function。您可以在覆盖率报告中查看实际覆盖的内容。 有关于else path not taken
等信息,您还必须涵盖这些信息。
在这种情况下,需要进行以下测试:
// covers first part of first if statement
it("should set variable1 if undefined", () => {
window.variable1 = undefined;
initializeValues();
expect(window.variable1).toEqual(variable1Value)
});
// covers second part of first if statement
it("should set variable1 if null", () => {
window.variable1 = null;
initializeValues();
expect(window.variable1).toEqual(variable1Value)
});
// covers else part of first if statement
it("should not set variable1 if already set", () => {
window.variable1 = "some value different than variable1Value";
initializeValues();
expect(window.variable1).toEqual("some value different than variable1Value")
});
第二个if
语句的集合相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.