如何重构代码以解决lint错误?

我一直在为交换案例编写测试用例,有2个测试用例。我收到此皮棉错误,提示Update or refactor this function so that its implementation does not duplicate other。由于切换案例包含相似的案例,因此我需要帮助来重新编写我的代码。

这是我的2例

  it('case 1',() => {
    data = merge(Run.String_Window,{
      0: '3',});
    expect(service.getName('year-1',element,frequency)).toEqual(serve);
  });

  it('case 2',{
      0: '5',});
    expect(service.getName('year-2',frequency)).toEqual(serve);
  });


clorchid 回答:如何重构代码以解决lint错误?

如果要对多个值运行相同的测试,则可以创建一个数组,其中每个项目都包含输入数据和预期数据。然后,您可以遍历该数组并为每个数据集执行测试:

const testCases = [
  {
    desc: 'case 1',input: { /* ... */ },expected: { /* ... */ }
  },{
    desc: 'case 2',expected: { /* ... */ }
  }
  // case 3,4,5....
];

testCases.forEach(tc => {
  it(tc.desc,() => {
    //...
    expect(/*...*/).toEqual(tc.expected);
  });
});
本文链接:https://www.f2er.com/3136015.html

大家都在问