Skip to content

Tests ​

Write assertions that run after a response is received. Test scripts let you validate that an API is behaving correctly - checking status codes, response shape, headers, or timing - without leaving the request editor.

Where to Write Tests ​

Open a saved request and click the Tests tab in the request form. A dot appears on the tab label when a script is present.

Test script editorTest script editor

Test results appear in the Test Results tab of the response panel after you send the request.

Test results in the response panelTest results in the response panel

Writing Tests ​

Use the test() function to define a named test. Each test contains one or more assertions using expect().

js
test('status is 200', () => {
  expect(response.status).toBe(200);
});

test('response has an id field', () => {
  const data = response.json();
  expect(data.id).toBeDefined();
});

All tests in the script are run. A test passes if it throws no errors; it fails if an assertion throws.

More examples — status checks, body inspection, timing, and saving values for later requests — are in the Examples section below, after the API reference.

test(name, fn) ​

ParameterTypeDescription
namestringLabel shown in test results
fn() => voidFunction containing assertions; any thrown error marks the test as failed

expect(actual) Matchers ​

MatcherDescription
.toBe(expected)Strict equality using Object.is
.toEqual(expected)Deep equality (compares JSON structure)
.toBeTruthy()Passes if the value is truthy
.toBeFalsy()Passes if the value is falsy
.toContain(item)Passes if an array contains the item, or a string contains the substring
.toHaveLength(n)Passes if .length equals n
.toBeGreaterThan(n)Passes if the value is greater than n
.toBeLessThan(n)Passes if the value is less than n
.toBeGreaterThanOrEqual(n)Passes if the value is greater than or equal to n
.toBeLessThanOrEqual(n)Passes if the value is less than or equal to n
.toMatch(pattern)Passes if the string matches a substring or RegExp
.toBeNull()Passes if the value is null
.toBeUndefined()Passes if the value is undefined
.toBeDefined()Passes if the value is not undefined
.notNegates the following matcher, e.g. expect(x).not.toBe(null)

Available Globals ​

response ​

PropertyTypeDescription
response.statusnumberHTTP status code, e.g. 200
response.statusTextstringStatus text, e.g. "OK"
response.headersRecord<string, string>Response headers
response.bodystringRaw response body
response.durationnumberTime taken in milliseconds
response.json()unknownParses response.body as JSON; throws if the body is not valid JSON

request ​

Read-only view of the request that was sent. The same properties as in pre-request scripts, plus parsed params and form.

PropertyTypeDescription
request.methodstringHTTP method that was sent
request.urlstringURL that was sent (after variable substitution)
request.headersRecord<string, string> | undefinedHeaders that were sent
request.bodystring | undefinedBody that was sent
request.paramsRecord<string, string>Query parameters parsed from the URL
request.formRecord<string, string>Form fields for form-data / x-www-form-urlencoded bodies; file entries map to their file name

environment ​

Read and write variables in the active environment. The same API as in pre-request scripts.

MethodDescription
environment.get(key)Returns the current value of key - as a Number or Boolean when the variable has that type (falling back to the string when the value cannot represent it) - or an empty string
environment.set(key, value)Sets the current value of key
environment.has(key)Returns whether key exists in the active environment
environment.clear(key)Removes the current value of key; without a key, clears every visible variable in the active environment

environment.set() in a test script writes to the variable's current value, so you can pass a value extracted from a response (like a token or ID) to later requests. The value's type is inherited as well - environment.set('count', 42) marks count as a Number so it is sent unquoted in JSON bodies, while a string ID from a response stays a String and is sent quoted. See Variable Types and JSON Bodies.

environment.clear() removes the current value only, so the committed initial value applies again on the next send.

collection ​

Read and write the variables of the request's collection. The methods mirror environment exactly. See Collection Variables for how collection variables work and where they sit in the resolution order.

MethodDescription
collection.get(key)Returns the current value of key - typed like environment.get - or an empty string if the variable does not exist
collection.set(key, value)Sets the current value of key in the collection's variables
collection.has(key)Returns whether key exists in the collection's variables
collection.clear(key)Removes the current value of key; without a key, clears every visible collection variable

collection.set() writes to the variable's current value, stored in the gitignored collection sidecar so scripts never cause unintended git changes. An environment variable with the same key still wins. The value's type is inherited the same way as environment.set().

Examples ​

js
// Check status
test('status is 201', () => {
  expect(response.status).toBe(201);
});

// Parse and inspect the response body
test('user has expected fields', () => {
  const user = response.json();
  expect(user.name).toBeDefined();
  expect(user.email).toContain('@');
});

// Assert response time
test('response is fast', () => {
  expect(response.duration).toBeLessThan(2000);
});

// Save a value for subsequent requests
test('extract token', () => {
  const data = response.json();
  environment.set('authToken', data.token);
});

// Clear a script-set value so the committed initial value applies again
test('reset the cached token', () => {
  environment.clear('authToken');
});

// Read a variable from the request's collection
test('collection supplies the api version', () => {
  expect(collection.get('api_version')).toBe('v2');
});

// Write a collection variable for later requests (stored locally, not committed)
test('record the run timestamp', () => {
  collection.set('lastRunAt', new Date().toISOString());
});

// Assert a value sent with the request comes back in the response
test('echoes the sent value', () => {
  const data = response.json();
  expect(data.name).toBe(request.form.name);
});

// Assert a value from the JSON request body comes back in the response
test('echoes the sent JSON body value', () => {
  const sent = JSON.parse(request.body);
  expect(response.json().name).toBe(sent.name);
});

// Check the request was built as expected
test('sends the expected query parameter', () => {
  expect(request.params.version).toBe('v2');
});

Limitations ​

  • Scripts time out after 5 seconds
  • No network access (fetch and XMLHttpRequest are not available)
  • No require or import - scripts run in an isolated sandbox
  • No async/await - scripts must be synchronous
  • Tests do not run for streaming (SSE) responses

Released under the MIT License.