🗂

jestで同じテストケースを複数の値で実行したいとき

2021/07/26に公開

jest で同じテストケースを複数の値で実行したいとき

How to

jest.each を使う

https://jestjs.io/ja/docs/api#describeeachtablename-fn-timeout

  • describe.each(table)(name, fn, timeout)
  • test.each(table)(name, fn, timeout)
describe.each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3],
])(".add(%i, %i)", (a, b, expected) => {
  test(`returns ${expected}`, () => {
    expect(a + b).toBe(expected);
  });

  test(`returned value not be greater than ${expected}`, () => {
    expect(a + b).not.toBeGreaterThan(expected);
  });

  test(`returned value not be less than ${expected}`, () => {
    expect(a + b).not.toBeLessThan(expected);
  });
});

Discussion