最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to run tests with jest.each - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Feb 12, 2020 at 14:44 Thatkookooguy 7,0221 gold badge33 silver badges59 bronze badges asked Feb 6, 2020 at 11:01 mspoulsenmspoulsen 1,4962 gold badges13 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

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

发布评论

评论列表(0)

  1. 暂无评论