AssemblyScript Unit Test Framework

2.1.0 · active · verified Sun Apr 19

assemblyscript-unittest-framework is a comprehensive testing solution for AssemblyScript and WebAssembly projects. It provides a robust suite of features including function mocking, code coverage statistics, and a rich expectation API. The current stable version is 2.1.0, with an active release cadence reflecting continuous development and feature additions, often multiple releases within a few months. This framework distinguishes itself by enabling developers to write tests directly in AssemblyScript and execute them within a Node.js environment, bridging the gap between host and WASM execution. Key differentiators include its dedicated support for the AssemblyScript ecosystem, offering deep integration for compiling and running WASM tests, and providing advanced capabilities like isolated test execution (configurable since v2.0.0) and detailed reporting.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and run AssemblyScript unit tests using the framework. It includes a `package.json` for dependencies and scripts, a sample AssemblyScript test file (`index.spec.ts`) using the `assemblyscript-unittest` APIs, and a TypeScript runner script (`run.ts`) that initializes and executes the `TestRunner` with `ConsoleReporter`.

/* package.json */
{
  "name": "my-as-tests",
  "version": "1.0.0",
  "description": "Example AssemblyScript tests",
  "main": "./dist/run.js",
  "type": "module",
  "scripts": {
    "test": "node --loader ts-node/esm run.ts",
    "build:assembly": "asc assembly/index.spec.ts --target release --explicit-start --noEmit",
    "build:runner": "tsc"
  },
  "devDependencies": {
    "assemblyscript": ">=0.25.1",
    "warpo": ">=2.2.0",
    "assemblyscript-unittest-framework": "^2.1.0",
    "ts-node": "^10.9.1",
    "typescript": "^5.0.0"
  }
}

/* assembly/index.spec.ts */
import { test, describe, expect, beforeEach, afterEach } from "assemblyscript-unittest";

describe("My math functions", () => {
  let sum: i32;

  beforeEach(() => {
    sum = 0;
  });

  afterEach(() => {
    // Clean up if necessary
  });

  test("should add two numbers correctly", () => {
    sum = 1 + 2;
    expect<i32>(sum).toBe(3, "1 + 2 should be 3");
  });

  test("should handle negative numbers", () => {
    sum = -1 + (-2);
    expect<i32>(sum).toBe(-3, "-1 + -2 should be -3");
  });
});

/* run.ts */
import { TestRunner, ConsoleReporter } from 'assemblyscript-unittest-framework';
import { resolve } from 'path';

async function main(): Promise<void> {
  const runner = new TestRunner({
    files: [resolve(__dirname, './assembly/**/*.spec.ts')],
    rootDir: resolve(__dirname, './assembly'),
    // Options for AssemblyScript compiler
    asc: [
      '--target', 'release',
      '--explicitStart',
      '--noEmitDts'
    ],
    // Control isolated execution (default changed to false in v2.0.0)
    isolated: false,
    // Optional: Pass environment variables to WASM instance
    // env: { DEBUG: "true" }
  });

  runner.addReporter(new ConsoleReporter());

  const success = await runner.run();
  if (!success) {
    process.exit(1);
  }
}

main().catch(err => {
  console.error('Test Runner Error:', err);
  process.exit(1);
});

view raw JSON →