Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jest.each asynchronously

I am having problems loading filenames into jest.each asynchronously.

My code:

let files: string[][]

function getFilesWorking() {
  files = [["test1"], ["test2"]]
}

async function getFilesAsync() {
  files = await Promise.resolve([["test1"], ["test2"]])
}

beforeAll(() => {
  console.log("before")
})

describe.only("Name of the group", () => {
  getFilesAsync()
  test.each(files)("runs", f => {})
})

beforeAll is executed before each test but NOT before initialization of test.each, so I end up with undefined.

How can I load files before using test.each?

like image 400
mspoulsen Avatar asked Nov 23 '25 14:11

mspoulsen


1 Answers

As of Jest 28.1.3 and prior, this is not possible. There is an open issue documenting this behavior.

The best thing you can do for now is put your tests in a regular it() test and do a deep value comparison:

it('tests an array of cases', async () => {
  const data = await getSomeAsyncData()
  const expectedData = [ ... ]

  expect(data).toEqual(expectedData)
})
like image 170
Zach Gollwitzer Avatar answered Nov 26 '25 03:11

Zach Gollwitzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!