AssemblyScript Unit Test Framework
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
-
Cannot find module 'assemblyscript-unittest-framework' or its corresponding type declarations.
cause The `assemblyscript-unittest-framework` package is not installed or TypeScript cannot find its declaration files.fixRun `npm install assemblyscript-unittest-framework` (or `yarn add`) and ensure `typescript` and `ts-node` are correctly configured if using TypeScript. -
Error: No test files found. Please provide files option in config.
cause The `TestRunner` was initialized without valid `files` configuration, or the paths provided did not match any test files.fixVerify that the `files` array in your `TestRunner` configuration correctly points to your AssemblyScript test files (e.g., `files: ['./assembly/**/*.spec.ts']`) and that the paths are resolveable from your runner script's context. -
TypeError: Cannot read properties of undefined (reading 'addReporter')
cause `addReporter` was called on an undefined `runner` object, often due to an error during `TestRunner` instantiation or incorrect import.fixEnsure `TestRunner` is correctly imported as a named export (`import { TestRunner } from 'assemblyscript-unittest-framework';`) and that `new TestRunner(...)` is called with valid arguments before attempting to call its methods. -
WebAssembly.instantiate(): Imports argument must be an object
cause This error typically occurs if the AssemblyScript compiler (used by the framework) or the test runtime expects specific imports for the WASM module that are not being provided or are malformed.fixCheck your AssemblyScript `asc` compiler options within `TestRunner` config. Ensure `warpo` is correctly installed as a peer dependency. Also, verify that your AssemblyScript code has all necessary `import` statements if relying on external WASM functions.
Warnings
- breaking The default value of the `isolated` configuration option switched from `true` to `false`. This means tests will run in a shared WASM instance by default, potentially affecting state between tests if not properly managed.
- breaking The `endTest` API has been removed. Test completion is now handled implicitly by the framework.
- breaking The `--testcase` CLI argument was removed, indicating a shift in how specific test cases are targeted from the command line.
- gotcha Version 1.3.1 included an urgent update for the `chalk` dependency to `5.6.2` due to a malicious version (`chalk@5.6.1`) being published. While this was a dependency issue, it highlights the importance of keeping dependencies up to date.
- gotcha For writing tests in AssemblyScript, the `test`, `expect`, `describe`, etc., APIs are imported from `assemblyscript-unittest`, not `assemblyscript-unittest-framework`.
Install
-
npm install assemblyscript-unittest-framework -
yarn add assemblyscript-unittest-framework -
pnpm add assemblyscript-unittest-framework
Imports
- TestRunner
const TestRunner = require('assemblyscript-unittest-framework').TestRunner;import { TestRunner } from 'assemblyscript-unittest-framework'; - ConsoleReporter
import ConsoleReporter from 'assemblyscript-unittest-framework/reporter';
import { ConsoleReporter } from 'assemblyscript-unittest-framework'; - createConfig
import { Configuration } from 'assemblyscript-unittest-framework';import { createConfig } from 'assemblyscript-unittest-framework';
Quickstart
/* 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);
});