Jest: Wait for an async test to finish before running the next one

rap-2-h

Assuming I have these test cases (with jest and supertest):

describe('Test actors', async () => {
  const params = { /* ... */ }
  let actorId

  test(`Create an actor ${actorsUrl}`, async () => {
    const response = await request(app.callback())
      .post(actorsUrl)
      .send(params)
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .expect(200)
    expect(response.body.name).toBe(params.name)
    expect(response.body.address).toBe(params.address)
    actorId = response.body.id
  })

  test(`Get the actor created ${actorsUrl}/${actorsUrl}`, async () => {
    const response = await request(app.callback())
      .get(`${actorsUrl}/${actorsUrl}`)
      .set('Accept', 'application/json')
      .expect(200)
    expect(response.body.name).toBe(params.name)
    expect(response.body.address).toBe(params.address)
  })
})

I want to wait for first test to finish before running the second one (because the first one creates a Actor and the second one ask the API for the created Actor). Running this code fails because the actor is not already created.

Is there a way to wait for the first test to finish before calling the second one?

Adelin

is just a wrapper over , and in many cases it relies on jasmine's rules.

Therefore, you can make use of the same done callback that is passed to the test function:

test(`Create an actor ${actorsUrl}`, async (done) => {
    const response = await request(app.callback())
      .post(actorsUrl)
      .send(params)
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .expect(200)
    expect(response.body.name).toBe(params.name)
    expect(response.body.address).toBe(params.address)
    actorId = response.body.id
    done();
})
test(`Get the actor created ${actorsUrl}/${actorsUrl}`, async (done) => {
    const response = await request(app.callback())
      .get(`${actorsUrl}/${actorsUrl}`)
      .set('Accept', 'application/json')
      .expect(200)
    expect(response.body.name).toBe(params.name)
    expect(response.body.address).toBe(params.address)
    done();
})

You can read more about that in jest's async documentation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Wait action to finish before starting the next one

wait for async function in loop to finish executing before next iteration

Wait for download method to finish before running next .then - node.js

Make Mocha wait before running next test

Will cron.daily wait for jobs to finish before starting the next one?

How do I wait for this animation to finish before moving on to the next one?

Waiting for each animation in loop to finish before running the next one

protractor - wait for previous it block to finish before next

wait for for loop to finish before executing next function

Wait for forkjoin to finish before executing next code

Will JMeter wait for the HTTP response of a HTTP sample before running the next one?

Wait for async process to finish before redirect in koajs

Wait for multiple simultaneous powershell commands in other sessions to finish before running next commands

How to wait for asynchronous JSZip .forEach() call to finish before running next code?

Wait for function to finish running before iterating

How to wait for a parallel command to finish before executing the next one ( Shell script)

Run commands in parallel and wait for one group of commands to finish before starting the next

Wait for one function to finish before continuing?

Protractor - Wait for async calls to finish before before executing expect

Protractor - Wait for async promise before doing next

How to wait for bootstrap modal animation to finish in Vue jest test

How to wait for request to finish before execute the next operation (Xcode 11)

Perl: how to wait for a curl step to finish before processing to the next

How to wait for an execution to finish before doing next line

Wait for All Fades in a Function to Finish Before the Next Function Starts

Does .then wait for setState to finish before calling the next method?

Wait on each for loop iteration to finish before starting the next in nodejs

How to wait for React useEffect hook finish before moving to next screen

how to wait slideToggle finish before next button click