5

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!

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but it makes no difference. I still get the same error: .each called with an empty Array of table data.
@mspoulsen fixed my answer to also deal with what you said in the comment
@mspoulsen if something is still unclear in the answer, please let me know. Or mark the answer as the right solution please
beforeAll(() => files = [["a"], ["b"]]) is not working in this example...but thanks for the answer...I think I got it.
Jesus Christ! I have spent so much time on this issue. Had no idea there was a readdirSync! Thanks so much! Works like a charm :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.