From 2fb612909db95b7d8e4a0dcae76f8760e8d9a34b Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 9 Oct 2025 11:35:07 +0800 Subject: [PATCH 1/4] chore(doc): update configuration document and example --- .github/workflows/ci-test.yml | 8 ++++ config.d.ts | 29 ++++++++++++++ docs/api-documents/configuration.md | 50 +----------------------- example/.gitignore | 2 + example/as-test.config.cjs | 14 ++----- example/as-test.config.js | 16 +++----- example/{ => assembly}/source.ts | 0 example/{ => assembly}/source2.ts | 0 example/{ => lib}/env.ts | 0 example/package-lock.json | 59 +++++++++++++++++++++++++++++ example/package.json | 11 ++++++ example/{ => tests}/source.test.ts | 6 +-- example/{ => tests}/source2.test.ts | 6 +-- package.json | 6 +-- 14 files changed, 128 insertions(+), 79 deletions(-) create mode 100644 config.d.ts create mode 100644 example/.gitignore rename example/{ => assembly}/source.ts (100%) rename example/{ => assembly}/source2.ts (100%) rename example/{ => lib}/env.ts (100%) create mode 100644 example/package-lock.json create mode 100644 example/package.json rename example/{ => tests}/source.test.ts (84%) rename example/{ => tests}/source2.test.ts (56%) diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index 03cd1be..fb3eedb 100644 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -22,3 +22,11 @@ jobs: - run: npm run build - run: npm run lint - run: npm run test + + - name: example + working-directory: ./example + run: | + set -e + npm ci + npm run test + npm run test:cjs diff --git a/config.d.ts b/config.d.ts new file mode 100644 index 0000000..9cd8e25 --- /dev/null +++ b/config.d.ts @@ -0,0 +1,29 @@ +import { Imports } from "./dist/interface.d.ts"; + +export type OutputMode = "html" | "json" | "table"; + +export declare class Config { + /** Files to include in testing and coverage statistics */ + include: string[]; + /** Files to exclude from testing and coverage statistics, has higher priority than include */ + exclude?: string[]; + + /** whether to collect coverage information, default is true */ + collectCoverage?: boolean; + + /** create an wasm instance for each test files. default is false (will be true in next major version) */ + isolated?: boolean; + + /** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug -O0 */ + flags?: string; + imports?: Imports; + + /** template file path, default "coverage" */ + temp?: string; + + /** report file path, default "coverage" */ + output?: string; + + /** output report mode, default is "table" */ + mode?: OutputMode | OutputMode[]; +} diff --git a/docs/api-documents/configuration.md b/docs/api-documents/configuration.md index 70f487e..ccf592d 100644 --- a/docs/api-documents/configuration.md +++ b/docs/api-documents/configuration.md @@ -1,54 +1,8 @@ ## Configuration -This is the template of `as-test.config.js`: +The [template](https://github.com/wasm-ecosystem/assemblyscript-unittest-framework/blob/main/example/as-test.config.js) of `as-test.config.js` -```javascript -module.exports = { - // test related code folder - include: ["source", "tests"], - exclude: [], - - /** optional: assemblyscript compile flag, default is --exportStart _start -O0 */ - flags: "", - - /** - * optional: import functions - * @param {ImportsArgument} runtime - * @returns - */ - imports(runtime) { - return { - env: { - logInfo(ptr, len) { - let buf = runtime.exports!.__getArrayBuffer(ptr); - let str = Buffer.from(buf).toString("utf8"); - runtime.framework.log(str); // log to unittest framework - console.log(str); // log to console - }, - }, - console: { - log(ptr) { - runtime.framework.log(runtime.exports!.__getString(msg)); - } - } - builtin: { - getU8FromLinkedMemory(a) { - return 1; - }, - }, - }; - }, - - /** optional: template file path, default "coverage" */ - // temp: "coverage", - - /** optional: report file path, default "coverage" */ - // output: "coverage", - - /** optional: test result output format, default "table" */ - // mode: ["html", "json", "table"], -}; -``` +The [type declaration](https://github.com/wasm-ecosystem/assemblyscript-unittest-framework/blob/main/config.d.ts) of config. ### Imports diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..b1bc6fe --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/coverage \ No newline at end of file diff --git a/example/as-test.config.cjs b/example/as-test.config.cjs index 03f5516..cc1f4f6 100644 --- a/example/as-test.config.cjs +++ b/example/as-test.config.cjs @@ -1,15 +1,10 @@ +/** @type {import("assemblyscript-unittest-framework/config.d.ts").Config} */ module.exports = { - include: ["example"], - exclude: ["assembly/coverageCollector.ts", "assembly/mock.ts"], + include: ["tests"], + exclude: ["lib"], - /** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug -O0 */ flags: "", - /** - * import functions - * @param {ImportsArgument} runtime - * @returns - */ imports(runtime) { return { env: { @@ -20,9 +15,6 @@ module.exports = { }; }, - /** template file path, default "coverage" */ temp: "coverage", - - /** report file path, default "coverage" */ output: "coverage", }; diff --git a/example/as-test.config.js b/example/as-test.config.js index 3b83be1..5d72d65 100644 --- a/example/as-test.config.js +++ b/example/as-test.config.js @@ -1,15 +1,10 @@ +/** @type {import("assemblyscript-unittest-framework/config.d.ts").Config} */ export default { - include: ["example"], - exclude: ["assembly/coverageCollector.ts", "assembly/mock.ts"], + include: ["tests"], + exclude: ["lib"], - /** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug -O0 */ flags: "", - /** - * import functions - * @param {ImportsArgument} runtime - * @returns - */ imports(runtime) { return { env: { @@ -20,9 +15,8 @@ export default { }; }, - /** template file path, default "coverage" */ temp: "coverage", - - /** report file path, default "coverage" */ output: "coverage", + + isolated: false, }; diff --git a/example/source.ts b/example/assembly/source.ts similarity index 100% rename from example/source.ts rename to example/assembly/source.ts diff --git a/example/source2.ts b/example/assembly/source2.ts similarity index 100% rename from example/source2.ts rename to example/assembly/source2.ts diff --git a/example/env.ts b/example/lib/env.ts similarity index 100% rename from example/env.ts rename to example/lib/env.ts diff --git a/example/package-lock.json b/example/package-lock.json new file mode 100644 index 0000000..88bcfe0 --- /dev/null +++ b/example/package-lock.json @@ -0,0 +1,59 @@ +{ + "name": "example", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "example", + "dependencies": { + "assemblyscript-unittest-framework": "file:.." + } + }, + "..": { + "version": "0.0.0", + "license": "Apache-2.0", + "dependencies": { + "@assemblyscript/loader": ">=0.25.1", + "chalk": "5.6.2", + "commander": "^8.3.0", + "cross-spawn": "^7.0.3", + "fs-extra": "^11.1.1", + "glob": "^11.0.0", + "ignore": "^7.0.3", + "semver": "^7.5.3", + "source-map": "^0.7.4", + "wasmparser": "5.11.1" + }, + "bin": { + "as-test": "bin/as-test.js" + }, + "devDependencies": { + "@schleifner/eslint-config-base": "^2.0.0", + "@schleifner/prettier-config": "^1.0.0", + "@types/fs-extra": "^11.0.4", + "@types/jest": "^29.5.0", + "@types/node": "^22.0.0", + "assemblyscript-prettier": "^3.0.1", + "concurrently": "^9.2.1", + "cross-env": "^7.0.3", + "diff": "^8.0.2", + "jest": "^29.5.0", + "prettier": "^3.0.0", + "ts-jest": "^29.1.0", + "ts-node": "^10.9.2", + "typescript": "^5.7.0", + "vitepress": "^1.6.3" + }, + "engines": { + "node": ">= 16.6.0" + }, + "peerDependencies": { + "assemblyscript": ">=0.25.1" + } + }, + "node_modules/assemblyscript-unittest-framework": { + "resolved": "..", + "link": true + } + } +} diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..aad8ada --- /dev/null +++ b/example/package.json @@ -0,0 +1,11 @@ +{ + "name": "example", + "type": "module", + "dependencies": { + "assemblyscript-unittest-framework": "file:.." + }, + "scripts": { + "test": "as-test", + "test:cjs": "as-test --config as-test.config.js" + } +} diff --git a/example/source.test.ts b/example/tests/source.test.ts similarity index 84% rename from example/source.test.ts rename to example/tests/source.test.ts index b06fe0c..d65612f 100644 --- a/example/source.test.ts +++ b/example/tests/source.test.ts @@ -1,6 +1,6 @@ -import { describe, test, expect, mock } from "../assembly"; -import { log } from "./env"; -import { add, Test } from "./source"; +import { describe, test, expect, mock } from "assemblyscript-unittest-framework/assembly"; +import { log } from "../lib/env"; +import { add, Test } from "../assembly/source"; describe("example 1", () => { test("two plus two is four", () => { diff --git a/example/source2.test.ts b/example/tests/source2.test.ts similarity index 56% rename from example/source2.test.ts rename to example/tests/source2.test.ts index 669706e..903ed2d 100644 --- a/example/source2.test.ts +++ b/example/tests/source2.test.ts @@ -1,7 +1,7 @@ -import { describe, test, expect } from "../assembly"; -import { quick_sort } from "./source2"; +import { describe, test, expect } from "assemblyscript-unittest-framework/assembly"; +import { quick_sort } from "../assembly/source2"; -describe("quick_sork", () => { +describe("quick_sort", () => { test("1", () => { let d = [1, 2, 3]; quick_sort(d); diff --git a/package.json b/package.json index 2dcbbdd..6733da0 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "lint:as": "npx eslint --config ./assembly/eslint.config.mjs assembly --max-warnings=0", "lint": "npm run lint:ts && prettier -c .", "lint:fix:ts": "eslint src transform tests/ts/test --fix && prettier --write .", - "example": "node bin/as-test.js --config example/as-test.config.cjs ; node bin/as-test.js --config example/as-test.config.js", "docs:dev": "vitepress dev docs", "docs:build": "vitepress build docs" }, @@ -79,11 +78,12 @@ "files": [ "assembly/**/*", "bin/**/*", - "dist/**/*", "build_wasm/bin/wasm-instrumentation.js", + "config.d.ts", + "dist/**/*", "docs/**/*", - "transform/**/*", "resource/**/*", + "transform/**/*", "LICENSE", "README.md" ] From 532c1ea6af0d34e9f18550db0837c14db6ebe401 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 9 Oct 2025 11:53:29 +0800 Subject: [PATCH 2/4] fix --- example/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/package.json b/example/package.json index aad8ada..71e7821 100644 --- a/example/package.json +++ b/example/package.json @@ -5,7 +5,7 @@ "assemblyscript-unittest-framework": "file:.." }, "scripts": { - "test": "as-test", - "test:cjs": "as-test --config as-test.config.js" + "test": "not as-test", + "test:cjs": "not as-test --config as-test.config.js" } } From 6c6534e3c394a12845aa2ae3f8809ff429a58d22 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 9 Oct 2025 13:21:48 +0800 Subject: [PATCH 3/4] fix --- example/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/package.json b/example/package.json index 71e7821..7a10ac0 100644 --- a/example/package.json +++ b/example/package.json @@ -5,7 +5,7 @@ "assemblyscript-unittest-framework": "file:.." }, "scripts": { - "test": "not as-test", - "test:cjs": "not as-test --config as-test.config.js" + "test": "! as-test", + "test:cjs": "! as-test --config as-test.config.js" } } From c8e56337a8f9775146104829943ec37d5a594d8b Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 9 Oct 2025 14:24:13 +0800 Subject: [PATCH 4/4] Apply suggestion from @HerrCai0907 --- config.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.d.ts b/config.d.ts index 9cd8e25..8a6fabf 100644 --- a/config.d.ts +++ b/config.d.ts @@ -18,10 +18,10 @@ export declare class Config { flags?: string; imports?: Imports; - /** template file path, default "coverage" */ + /** template folder path, default "./coverage" */ temp?: string; - /** report file path, default "coverage" */ + /** report folder path, default "./coverage" */ output?: string; /** output report mode, default is "table" */