In this tutorial, we'll learn how to call an API in our test with axios.
QA Wolf gives you access to the axios library, which allows you to make HTTP requests.
For example, let's say we are testing the sign up flow for QA Wolf. When you sign up with email, you need to enter a login code that we send to your email address.
To test sign up on QA Wolf, we use a service called testmail.app that gives us access to a test email inbox. We can call their API to read the email with the login code.
In our test, we add the following code. Note the call to axios.get
, which makes a GET
request to the testmail.app API:
// get code from emailconst { data } = await axios.get("https://api.testmail.app/api/json", {params: {apikey: process.env.TESTMAIL_API_KEY,namespace: "namespace",livequery: true,pretty: true,tag,},timeout: 30 * 1000,});
If we log the response data
to the console, it looks like this. Our test can now enter the code from the email subject:
{emails: [{subject: "🐺 QA Wolf code: ABC-DEF",// ...},];}
You can also make other types of requests with axios. For example, here is a POST
request that creates a new user:
await axios.post("https://myapi.com/users/new",{email: "new@user.com",name: "New User",},{headers: {authorization: "Bearer token","content-type": "application/json",},});