Skip to content

Unit Testing

Run unit tests

aeproject test

The test command helps developers run their unit tests for æternity projects. The command executes the tests scripts that are located in the test folder of your æternity project.

Implement unit tests

In the test/exampleTest.js file you can find an example for unit testing using AEproject.

1. Dependencies

Javascript testing framework used with mocha for assertions, documented at https://www.chaijs.com/api/assert/

const { assert } = require("chai");

Helper and utilities for AEproject use, e.g. prefunded wallets, network definition and utility functions for SDK initialization and snapshotting.

const { networks, utils, wallets } = require("@aeternity/aeproject");

Read AEproject Library for a more detailed explanation about the usage of these imports.

2. SDK and Snapshotting Setup

Provide your initializations in mocha which need to be done once before all tests:

before(async () => ...)

Initialize the default SDK instance with provided utils:

aeSdk = utils.getSdk();

Get the filesystem definition for (custom) includes of the given contract:

const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);

Read the contract source from the filesystem:

const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);

Initialize the contract instance:

contract = await aeSdk.initializeContract({ sourceCode, filesystem });

Deploy the contract:

await contract.init();

Create a snapshot of the chain state once before all tests. This allows you to rollback to a clean state after each test if needed:

await utils.createSnapshot(aeSdk);

Rollback to the previously created snapshot after each test for a clean state in the following tests:

afterEach(async () => {
  await utils.rollbackSnapshot(aeSdk);
});

3. Example Test

it("ExampleContract: set and get", async () => {
  const set = await contract.set(42, {
    onAccount: utils.getDefaultAccounts()[1],
  });
  assert.equal(set.decodedEvents[0].name, "SetXEvent");
  assert.equal(
    set.decodedEvents[0].args[0],
    utils.getDefaultAccounts()[1].address,
  );
  assert.equal(set.decodedEvents[0].args[1], 42);

  const { decodedResult } = await contract.get();
  assert.equal(decodedResult, 42);
});

Use mocha for test setup and chai for assert. Then implement contract usage using the aeternity sdk as explained in the guide at https://github.com/aeternity/aepp-sdk-js/blob/develop/docs/guides/contracts.md#5-call-contract-entrypoints