mspoulsen
mspoulsen

Reputation: 1476

Unable to run tests with jest.each

I am trying to load files into an array and then run tests on them. This is my code:

let files: string[] = []

describe.only("Name of the group", () => {
  beforeAll(() => {
    files = ["a", "b"]
  })

  test.each(files)("runs", f => {
    console.log(f)
  })
})

However, I get

Error: .each called with an empty Array of table data.

What am I doing wrong?

Thanks!

Upvotes: 5

Views: 5439

Answers (1)

Thatkookooguy
Thatkookooguy

Reputation: 7012

test.each expects a table value as input. which means an array of arrays. But that is fixed automatically so don't worry about it.

But the call order is important here! Notice that the tests are defined before they are actually run. So, beforeAll will run after the tests were defined. This means the files array won't be defined while the tests are being registered.

In order to fix this, you need to make sure the files array is populated before the tests are read and registered

So something like this:

const files: string[][] = [ ['test1'],['test2'] ];

describe('Something Something', () => {
  describe('Name of the group', () => {
    test.each(files)('runs %s', (f) => {});
  });
});

Upvotes: 7

Related Questions