Toivo Säwén
Toivo Säwén

Reputation: 2042

Skip some tests when using test.each in jest

I am using test.each to run through some combinations of input data for a function:

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))
  });
});

Now, some of these combinations don't currently work and I want to skip those tests for the time being. If I just had one test I would simply do test.skip, but I don't know how this works if I want to skip some specific values in the input array.

Upvotes: 4

Views: 3706

Answers (1)

Abishek Aditya
Abishek Aditya

Reputation: 812

Unfortunately jest doesn't support the annotations for skipping elements of combinations. You could do something like this, where you filter out the elements you do not want using a simple function (I have written one quickly, you can improve on it)

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))
  });
});

Upvotes: 3

Related Questions