简体   繁体   English

typescript function 的开玩笑测试用例,不返回

[英]jest test cases for typescript function with no return

I have component in reactjs-我在 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;
  }
}

I have written test case in jest for this as -我开玩笑地为此编写了测试用例 -

describe("initializeValues",()=>{

    test("Variable1",()=>{
      expect(window.variable1$).toBeUndefined();
     });

    test("Variable2",()=>{
      expect(window.variable2$).toBeUndefined();
     });
});

The test cases written above runs successfully.上面写的测试用例运行成功。 But it shows only 5% of code coverage.但它只显示了 5% 的代码覆盖率。

How can I add more test cases when function is not returning anything?当 function 没有返回任何内容时,如何添加更多测试用例? (with just assignment of values) (仅分配值)

I am not quite sure what const.variable1Value is but anyway.我不太确定const.variable1Value是什么,但无论如何。 In the test you should prepare the test preconditions and run the tested function. You can check in coverage report what actually is covered.在测试中,您应该准备测试前提条件并运行测试的 function。您可以在覆盖率报告中查看实际覆盖的内容。 There are information about eg else path not taken and you must also cover that.有关于else path not taken等信息,您还必须涵盖这些信息。

In this case following tests are needed:在这种情况下,需要进行以下测试:

  • for the first condition:对于第一个条件:
// 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")
});

Same set for the second if statement.第二个if语句的集合相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM