SDET Unicorns

Using Async-Await with SuperTest

Table of Content

In this post, I will cover how async-await can help us write better and clean tests. async-await makes working with asynchronous programming a bit easier for us, let’s see how it works –

Async-Await

async functions and await keywords were introduced in ECMAScript 2017 edition. When you pass an async keyword to a function, it returns a promise. And, the await keyword waits for the promise to be fulfilled before moving on to the next step.

So how does it relate to our API tests? Well, let’s take a look at an example. We’ll write a test to create a user post, this test will first create a user and then use the userId to create a post.

it('/posts', () => {
  // create user
  request
    .post('users')
    .set('Authorization', `Bearer ${TOKEN}`)
    .send(data)
    .then((res) => {
      expect(res.body.data).to.deep.include(data);
      userId = res.body.data.id;

      // create post using the above userId
      request
        .post('posts')
        .set('Authorization', `Bearer ${TOKEN}`)
        .send(data)
        .then((res) => {
          expect(res.body.data).to.deep.include(data);
        });
    });
});

Instead of creating that massive chain and callbacks like the way we did above, we can instead use async-await to make it look a bit cleaner –

// create async function with 'it' block
it('/posts', async () => {
  // use await to fulfill the promise and get response
  const userRes = await request
    .post('users')
    .set('Authorization', `Bearer ${TOKEN}`)
    .send(data);
  expect(userRes.body.data).to.deep.include(data);
  userId = res.body.data.id;

  // do the same for post request too
  const postRes = await request
    .post('posts')
    .set('Authorization', `Bearer ${TOKEN}`)
    .send(data);

  expect(postRes.body.data).to.deep.include(data);
});

With the help of async-await, we are making the code look synchronous where it’ll do one thing, complete that and then move to another task. To make it even cleaner, we can create an async function for the user generation and call it like this –

userId = await createRandomUser();

To see a detailed explanation of the code above along with other optimization tips, check out the video below:

You can also clone the GitHub repo to access this code


To learn more about API testing, check out my free tutorial series here –

JavaScript API Automation Testing Tutorial Series

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »