From 172a49dce2ce5cb57d16abee3fd6019b3169a602 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 10 Oct 2025 07:32:33 +0800 Subject: [PATCH 1/4] abort --- src/core/execute.ts | 2 +- src/utils/index.ts | 32 ++++++++++++++++----- tests/e2e/assertFailed/assertOnTest.test.ts | 5 ++-- tests/e2e/assertFailed/env.ts | 1 - tests/e2e/assertFailed/stdout.txt | 12 ++++---- 5 files changed, 34 insertions(+), 18 deletions(-) delete mode 100644 tests/e2e/assertFailed/env.ts diff --git a/src/core/execute.ts b/src/core/execute.ts index d2534da..d2221e8 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..816ff7a 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] = {}; - } + 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-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars - (importObject[module] as any)[name] = (..._args: unknown[]): unknown => { + 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) From 441c784018c6c7379d47e48572004696ce0a9211 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 10 Oct 2025 11:36:52 +0800 Subject: [PATCH 2/4] doc --- docs/release-note.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/release-note.md b/docs/release-note.md index c1d4330..b7dc01c 100644 --- a/docs/release-note.md +++ b/docs/release-note.md @@ -4,8 +4,12 @@ 🚀 Highlight Features +- 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)) + +🛠️ Improvements + - 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)) +- Improved the error messages when test case assert failed. ## 1.3.1 @@ -17,7 +21,7 @@ - `2` means invalid AS file. - `>2` means configuration error. -🚀 Improvements +🛠️ Improvements - Proper handling of situations where AS files are not invalid. @@ -34,7 +38,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. From de5f69a376590becce0356b76b76d0b259082bcd Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 10 Oct 2025 11:51:18 +0800 Subject: [PATCH 3/4] lint --- src/utils/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 816ff7a..5d750ad 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -71,7 +71,7 @@ export function supplyDefaultFunction( importsArg.framework.log(`trace: ${exports.__getString(msg)}${n > 0 ? " " : ""}${args.slice(0, n).join(", ")}`); }; } else { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars + // eslint-disable-next-line @typescript-eslint/no-unused-vars importObjectModule[name] = (..._args: unknown[]): unknown => { return info.return?.kind === TypeKind.i64 ? BigInt(0) : 0; }; From e7ff9226b71e8ab1d29e335ff1ebbc41cfc3d0cc Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 10 Oct 2025 15:58:28 +0800 Subject: [PATCH 4/4] fix --- tests/e2e/setup-teardown/stdout.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)