Executing Code in Tests

For certain use cases, the built-in test actions may not be enough to give you the full flexibility to thoroughly test your APIs. In these cases, you can add custom code actions that will execute when your tests are called.

To add code in your test, add the Code.run action.

Your code should export a function through module.exports. This is the basic boilerplate:

module.exports = (context) => {
    return {};

The function takes one argument — context, which is an object holding all the variables in the test flow (data returned from previous API calls, test variables, environment variables, etc).

The function should return an object. Any data in the object will be added to the test context and can be used by subsequent actions. See the following test flow as an example:

Intentionally Failing Tests

In addition to adding and modifying data, you may want to use code snippets to perform complex validations on data (like ragex-matching strings or summing up numbers). If you want to cause a test to fail, just throw an exception in your code like so:

module.exports = (context) => {
    throw "This action will fail the test"

Whatever message you send in the exception will be added to the test results: