admin管理员组

文章数量:1302318

I have a very large array of JSON objects. I need to run Jest tests on each individual element. I tried iterating through the array first and then write the tests in the loop as such:

describe("Tests", (f) => {
  it("has all fields and they are valid", () => {
    expect(f.portions! >= 0).toBeTruthy();
    expect(f.name.length > 0 && typeof f.name === "string").toBeTruthy();
  });

  it("has an image", () => {
    expect(f.image).toBeTruthy();
  });
});

However with this code Jest plains that " Your test suite must contain at least one test."

Do I have to loop over this array for every single test I have?

I have a very large array of JSON objects. I need to run Jest tests on each individual element. I tried iterating through the array first and then write the tests in the loop as such:

describe("Tests", (f) => {
  it("has all fields and they are valid", () => {
    expect(f.portions! >= 0).toBeTruthy();
    expect(f.name.length > 0 && typeof f.name === "string").toBeTruthy();
  });

  it("has an image", () => {
    expect(f.image).toBeTruthy();
  });
});

However with this code Jest plains that " Your test suite must contain at least one test."

Do I have to loop over this array for every single test I have?

Share edited Feb 13, 2023 at 8:21 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Sep 16, 2021 at 19:32 SimoneMetaSimoneMeta 1001 silver badge8 bronze badges 1
  • Does this answer your question? Jest looping through dynamic test cases – Michael Freidgeim Commented May 11, 2023 at 1:23
Add a ment  | 

1 Answer 1

Reset to default 8

Jest does have describe.each, test.each and it.each methods for your needs. It allows you to make same tests with different input/output.

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

Examples :

With global describe.each :

const params = [
  [true, false, false],
  [true, true, true],
  [false, true, false],
  [false, false, true],
];

describe.each(params)('With params %s, %s, %s', (a, b, c) => {
  it(`${a} === ${b} should be ${c}`, () => {
    expect(a === b).toBe(c);
  });
});

Output :

 PASS  test/integration-tests/test.spec.ts (5.938s)
  With params true, false, false
    √ true === false should be false (2ms)
  With params true, true, true
    √ true === true should be true
  With params false, true, false
    √ false === true should be false (1ms)
  With params false, false, true
    √ false === false should be true

Or with simple it.each :

const params = [
  [true, false, false],
  [true, true, true],
  [false, true, false],
  [false, false, true],
];

describe('Dumb test', () => {
  it.each(params)('%s === %s should be %s', (a, b, c) => {
    expect(a === b).toBe(c);
  });
});

Output :

 PASS  test/integration-tests/test.spec.ts
  Dumb test
    √ true === false should be false (2ms)
    √ true === true should be true
    √ false === true should be false
    √ false === false should be true

本文标签: javascriptLooping through an array and run a Jest test for each element does not workStack Overflow