Unit tests

To test your script's logic, create unit tests in script.spec.ts and then run them using the npm test command.

Write the unit tests

  1. In the unit test file (test/script.spec.ts), use as-pect's AssemblyScript API functions to structure your tests and compare results.

  2. Create example objects for your test cases. Each Scripts API has TestHelper classes that you can use to do this.

    Each TestHelper class is a builder that has a .build method or .buildWithID method, depending on whether the object needs a unique identifier. For example, a product has an ID, so the ProductBuilder class has a .buildWithID method.

    The following example code creates a ProductBuilder to build products with the name Red Delicious and a tag named fruits:

    The following example code uses this same ProductBuilder to create products with different IDs:

    To create test cases for configurations, use the Configuration class.

    The following example uses a Configuration object to test different configuration values:

    Run the unit tests

    Run the following command:

When the test file runs successfully without any errors, the output looks like the following:

When the test file runs successfully but some of the unit tests fail, the output looks like the following:

When the test file fails, you find an error like the following in the output:

Learn more