Tests

You are here:
< All Topics

You can write test scripts for your API requests in javaScript. 

Pre-Requests and Tests are handled by an API called pw.

  • .expect(value)
  • .not
  • .toBe(value)
  • .toBeLevel2xx(value)
  • .toBeLevel3xx(value)
  • .toBeLevel4xx(value)
  • .toBeLevel5xx(value)
  • .toBeType(type)
  • .toHaveLength(number)
  • .test(name, fn)
  • .repsonse

Let’s look at how to use pw.expect() and pw.test() to write our tests.

pw.expect(value)

Expect returns an expectation object, on which you can call matcher functions. The example below calls the matcher function ‘toBe’ on the expectation object that is returned by calling pw.expect with the response id (pw.response.body.id) as an argument.

Use pw.expect directly for quick and convenient testing. Every pw.expect statement will generate a line on the test report.

// This test will pass
pw.expect(1).toBe(1);

// This test will fail
pw.expect(2).not.toBe(2);

 

pw.test(name,fn)

Create a group of tests, with the name as a string and fn as a callback function to write tests associated with the group. The test results will include the given name for better organization.

Wrap expect statements with pw.test to group and describe related statements.

// This will return 4 lines on the test report, grouped under "Arithmetic operations"
pw.test("Arithmetic operations", () => {
  const size = 500 + 500;
  pw.expect(size).toBe(1000);
  pw.expect(size - 500).toBe(500);
  pw.expect(size * 4).toBe(4000);
  pw.expect(size / 4).toBe(250);
});

If neither a pw.expect nor pw.test statement is present, no test reports will be generated.

// This will not generate any test reports
(99 + 1).toBe(100);

However, you cannot nest .test within a .test callback function.

pw.test("a group of tests", () => {
  pw.expect(10).toBe(10);
  // more tests here
});

 

pw.toBe(value)

Test for exact equality using toBe.

pw.expect(pw.response.body.category).toBe(“Sneakers”);

toBe uses strict equality and is recommended for primitive data types.

// These tests will fail
pw.expect("hello").toBe("Hello");
pw.expect(5).toBe("5");
pw.expect([]).toBe([]);

 

pw.not

Test for the inverse by adding .not before calling the matcher function.

// These tests will pass
pw.expect(true).not.toBe(false);
pw.expect(200).not.toBeLevel3xx();

 

pw.toBeLevelxxx()

There are four different matcher functions for quick and convenient testing of the http status code that is returned:

  • toBeLevel2xx()
  • toBeLevel3xx()
  • toBeLevel4xx()
  • toBeLevel5xx()

For example, an argument passed to expect must be within 200 and 299 inclusive to pass toBeLevel2xx().

// These tests will pass
pw.expect(204).toBeLevel2xx();
pw.expect(308).toBeLevel3xx();
pw.expect(404).toBeLevel4xx();
pw.expect(503).toBeLevel5xx();

If the argument passed to expect is a non-numeric value, it is first parsed with parseInt().

// This test will pass
pw.expect("404").toBeLevel4xx();

 

pw.toBeType(type)

Use .toBeType(type) for type checking. The argument for this method should be “string”, “boolean”, “number”, “object”, “undefined”, “bigint”, “symbol” or “function”.

 

// These tests will pass
pw.expect(5).toBeType("number");
pw.expect("Hello, world!").toBeType("string");
pw.expect(5).not.toBeType("string");
pw.expect("Hello, world!").not.toBeType("number");

 

pw.toHaveLength(number)

Use .toHaveLength(number) to check that an object has a .length property and it is set to a certain numeric value.

// These expectations will pass
pw.expect("testgrid").toHaveLength(10);
pw.expect("testgrid").not.toHaveLength(9);
pw.expect(["apple", "banana", "coconut"]).toHaveLength(3);
pw.expect(["apple", "banana", "coconut"]).not.toHaveLength(4);

 

pw.response

Assert response data by accessing the pw.response object.

// This test will pass
pw.test("Response is ok", () => {
  pw.expect(pw.response.status).toBe(200);
  pw.expect(pw.response.body).not.toHaveProperty("errors");
});

Currently supported response values

status: -number- The status code as an integer.

headers: -object- The response headers.

body: -object- the data in the response. In many requests, this is the JSON sent by the server.

Table of Contents