I am having a similar issue to Test functions cannot both take a 'done' callback, but none of the solutions are working or applicable to me: I have a test file which needs to run done()
in order to setup an async test, but am getting the error:
Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.
Returned value: Promise {}
Because I am using jest v28.1.3.
In order to setup my environment, I run a recursive async function which looks like the following:
const iAmReady = async (done) => {
setTimeout(async () => {
try {
await request.get({ uri: 'http://localhost:7575/ready' })
done()
} catch (err) {
attempts += 1
if (attempts > 10) {
throw new Error('Failed to connect.')
}
iAmReady(done)
}
}, 500)
}
I then set this function to run in the beforeAll
:
beforeAll(async (done) => {
await iAmReady(done)
})
it('test', () => {
// test stuff
})
Unfortunately when I pass in the done as shown here, although it works an my environment is set up I get the error I stated above.
If I don't pass in the done and remove its usage from iAmReady
, the setup is ignored and done only after the tests run.
What can I do to replace the done
and make this work?