diff --git a/docs/release-note.md b/docs/release-note.md index 781de7b..6d539df 100644 --- a/docs/release-note.md +++ b/docs/release-note.md @@ -4,10 +4,14 @@ 🚀 Highlight Features -- Improved the as-test performances. -- Introduce new features `isolated: false` to significantly reduce test execution time in large projects. ([#73](https://github.com/wasm-ecosystem/assemblyscript-unittest-framework/pull/73)) +- Introduced new features `isolated: false` to significantly reduce test execution time in large projects. ([#73](https://github.com/wasm-ecosystem/assemblyscript-unittest-framework/pull/73)) - Introduce setup and teardown API. ([#77](https://github.com/wasm-ecosystem/assemblyscript-unittest-framework/pull/77)) +🛠️ Improvements + +- Improved the as-test performances. +- Improved the error messages when test case assert failed. + ## 1.3.1 🚀 Highlight Features @@ -18,7 +22,7 @@ - `2` means invalid AS file. - `>2` means configuration error. -🚀 Improvements +🛠️ Improvements - Proper handling of situations where AS files are not invalid. @@ -35,7 +39,7 @@ - Expose the framework's `log` function in the configuration file, and the logs redirected to this function will be appended to the final test report. - Support test crashes and provide good call stack information. -🚀 Improvements +🛠️ Improvements - Code coverage calculation. - Skip type definitions. diff --git a/src/core/execute.ts b/src/core/execute.ts index f28e002..5087709 100644 --- a/src/core/execute.ts +++ b/src/core/execute.ts @@ -47,7 +47,7 @@ async function nodeExecutor( const binaryBuffer = await readFile(instrumentResult.instrumentedWasm); const binary = binaryBuffer.buffer.slice(binaryBuffer.byteOffset, binaryBuffer.byteOffset + binaryBuffer.byteLength); const importFuncList = parseImportFunctionInfo(binary as ArrayBuffer); - supplyDefaultFunction(importFuncList, importObject); + supplyDefaultFunction(importFuncList, importObject, importsArg); const ins = await instantiate(binary, importObject); importsArg.module = ins.module; importsArg.instance = ins.instance; diff --git a/src/utils/index.ts b/src/utils/index.ts index d28ddad..5d750ad 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ import { Imports as ASImports } from "@assemblyscript/loader"; -import { ImportFunctionInfo } from "../interface.js"; +import { ImportFunctionInfo, ImportsArgument } from "../interface.js"; import { TypeKind } from "wasmparser/dist/cjs/WasmParser.js"; export function json2map(json: Record): Map { @@ -45,16 +45,34 @@ export function checkGenerics(functionName: string): string | undefined { return undefined; } -export function supplyDefaultFunction(infos: ImportFunctionInfo[], importObject: ASImports) { +export function supplyDefaultFunction( + infos: ImportFunctionInfo[], + importObject: ASImports, + importsArg: ImportsArgument +) { for (const info of infos) { const module = info.module; const name = info.name; - if (importObject[module]?.[name] === undefined) { - if (importObject[module] === undefined) { - importObject[module] = {}; - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars - (importObject[module] as any)[name] = (..._args: unknown[]): unknown => { + const importObjectModule = importObject[module] ?? {}; + importObject[module] = importObjectModule; + if (importObjectModule[name] !== undefined) { + continue; + } + if (module === "env" && name === "abort") { + importObjectModule[name] = (msg: number, file: number, line: number, col: number) => { + const exports = importsArg.exports!; + throw new WebAssembly.RuntimeError( + `abort: ${exports.__getString(msg)} at ${exports.__getString(file)}:${line}:${col}` + ); + }; + } else if (module === "env" && name === "trace") { + importObjectModule[name] = (msg: number, n: number, ...args: number[]) => { + const exports = importsArg.exports!; + importsArg.framework.log(`trace: ${exports.__getString(msg)}${n > 0 ? " " : ""}${args.slice(0, n).join(", ")}`); + }; + } else { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + importObjectModule[name] = (..._args: unknown[]): unknown => { return info.return?.kind === TypeKind.i64 ? BigInt(0) : 0; }; } diff --git a/tests/e2e/assertFailed/assertOnTest.test.ts b/tests/e2e/assertFailed/assertOnTest.test.ts index 8d4415e..0342925 100644 --- a/tests/e2e/assertFailed/assertOnTest.test.ts +++ b/tests/e2e/assertFailed/assertOnTest.test.ts @@ -1,7 +1,6 @@ import { test, expect } from "../../../assembly"; -import { log } from "./env"; test("assert on test", () => { - log("This test will fail due to an assertion error"); - assert(false, "This assertion is expected to fail"); + trace("this test will fail due to an assertion error"); + assert(false, "this assertion is expected to fail"); }); diff --git a/tests/e2e/assertFailed/env.ts b/tests/e2e/assertFailed/env.ts deleted file mode 100644 index b8c4440..0000000 --- a/tests/e2e/assertFailed/env.ts +++ /dev/null @@ -1 +0,0 @@ -export declare function log(msg: string): void; diff --git a/tests/e2e/assertFailed/stdout.txt b/tests/e2e/assertFailed/stdout.txt index 3353fdf..efe8171 100644 --- a/tests/e2e/assertFailed/stdout.txt +++ b/tests/e2e/assertFailed/stdout.txt @@ -8,14 +8,14 @@ test case: 0/2 (success/total) Error Message: assert on test: Test Crashed! -This test will fail due to an assertion error -Reason: unreachable - at start:tests/e2e/assertFailed/assertOnTest.test~anonymous|0 (tests/e2e/assertFailed/assertOnTest.test.ts:6:2) - at executeTestFunction (tests/e2e/assertFailed/tmp/assertOnTest.test.instrumented.wasm:1:649) +trace: this test will fail due to an assertion error +Reason: abort: this assertion is expected to fail at tests/e2e/assertFailed/assertOnTest.test.ts:5:3 + at start:tests/e2e/assertFailed/assertOnTest.test~anonymous|0 (tests/e2e/assertFailed/assertOnTest.test.ts:5:2) + at executeTestFunction (tests/e2e/assertFailed/tmp/assertOnTest.test.instrumented.wasm:1:780) tests/e2e/assertFailed/tmp/assertOnInit.test - init: Test Crashed! -Reason: unreachable +Reason: abort: null at tests/e2e/assertFailed/assertOnInit.test.ts:1:1 at start:tests/e2e/assertFailed/assertOnInit.test (tests/e2e/assertFailed/assertOnInit.test.ts:1:0) - at ~start (tests/e2e/assertFailed/tmp/assertOnInit.test.instrumented.wasm:1:265) + at ~start (tests/e2e/assertFailed/tmp/assertOnInit.test.instrumented.wasm:1:289) diff --git a/tests/e2e/setup-teardown/stdout.txt b/tests/e2e/setup-teardown/stdout.txt index 0c675e0..05761c3 100644 --- a/tests/e2e/setup-teardown/stdout.txt +++ b/tests/e2e/setup-teardown/stdout.txt @@ -8,7 +8,7 @@ test case: 18/19 (success/total) Error Message: tests/e2e/setup-teardown/tmp/setup_out_of_block.test - init: Test Crashed! -Reason: unreachable +Reason: abort: register setup function failed at assembly/implement.ts:20:3 at assembly/implement/beforeEachImpl (assembly/implement.ts:20:2) at assembly/index/beforeEach (assembly/index.ts:47:2) at start:tests/e2e/setup-teardown/setup_out_of_block.test (tests/e2e/setup-teardown/tmp/setup_out_of_block.test.instrumented.wasm:1:547)