首页 > 解决方案 > 开玩笑地使用 test.each 时跳过一些测试

问题描述

我正在使用 test.each 来运行函数的一些输入数据组合:

const combinations = [
  [0,0],
  [1,0], // this combination doesn't work, I want to skip it
  [0,1],
  [1,0],
  [1,1],
]

describe('test all combinations', () => {
  test.each(combinations)(
    'combination a=%p and b=%p works',
    (a,b,done) => {
      expect(foo(a,b).toEqual(bar))
  });
});

现在,其中一些组合目前不起作用,我想暂时跳过这些测试。如果我只有一个测试,我会简单地做test.skip,但如果我想跳过输入数组中的一些特定值,我不知道这是如何工作的。

标签: javascriptjestjs

解决方案


不幸的是,jest 不支持跳过组合元素的注释。你可以做这样的事情,用一个简单的函数过滤掉你不想要的元素(我写得很快,你可以改进它)

const combinations = [
  [0,0],
  [1,0], // this combination doesn't work, I want to skip it
  [0,1],
  [1,0],
  [1,1],
]
const skip = [[1,0]];
const combinationsToTest = combinations.filter(item => 
      !skip.some(skipCombo => 
            skipCombo.every( (a,i) => a === item[i])
            )
      )           

describe('test all combinations', () => {
  test.each(combinationsToTest)(
    'combination a=%p and b=%p works',
    (a,b,done) => {
      expect(foo(a,b).toEqual(bar))
  });
});

推荐阅读