Skip to content

Commit 03e2738

Browse files
authored
chore: add e2e test (#51)
1 parent 3675aa6 commit 03e2738

File tree

12 files changed

+160
-18
lines changed

12 files changed

+160
-18
lines changed

bin/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ start_unit_test(
6565
.then((success) => {
6666
if (!success) {
6767
console.error(chalk.redBright("Test Failed") + "\n");
68-
exit(-1);
68+
exit(255);
6969
}
7070
})
7171
.catch((e) => {
7272
console.error(chalk.redBright(" Test crash, error message: ") + chalk.yellowBright(`${e.stack}`) + "\n");
73-
exit(-1);
73+
exit(255);
7474
});

docs/api-documents/configuration.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,52 @@ module.exports = {
4949
// mode: ["html", "json", "table"],
5050
};
5151
```
52+
53+
### Imports
54+
55+
```typescript
56+
export interface ImportsArgument {
57+
module: WebAssembly.Module;
58+
instance: WebAssembly.Instance;
59+
exports: (ASUtil & Record<string, unknown>);
60+
framework: UnitTestFramework;
61+
}
62+
export abstract class UnitTestFramework {
63+
/**
64+
* function to redirect log message to unittest framework
65+
* @param msg: message to log
66+
*/
67+
abstract log(msg: string): void;
68+
}
69+
```
70+
71+
There are 2 useful fields.
72+
73+
- `exports`: contains exported function from test cases and [AS help API](https://github.com/AssemblyScript/assemblyscript/blob/3defefd5b09248d697a2e6bd1e7201c0cf98def1/lib/loader/index.d.ts#L23).
74+
- `framework`: contains runtime provided function.
75+
76+
- `log`: redirect log message from test cases to unittest framework. It will be showed in failed info.
77+
::: details
78+
79+
```typescript
80+
test("failed test", () => {
81+
log("This is a log message for the failed test."); // log to be redirect
82+
expect(1 + 1).equal(3);
83+
});
84+
85+
test("succeed test", () => {
86+
log("This is a log message for the succeed test."); // log to be redirect
87+
expect(1 + 1).equal(2);
88+
});
89+
```
90+
91+
will output
92+
93+
```
94+
Error Message:
95+
failed test:
96+
*.test.ts:6:2 value: 2 expect: = 3
97+
This is a log message for the failed test. <- only log in failed test will be showed here
98+
```
99+
100+
:::

package-lock.json

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
},
1313
"prettier": "@schleifner/prettier-config",
1414
"scripts": {
15+
"watch:ts": "tsc --build ./src/tsconfig.json --watch",
1516
"build": "node scripts/build_instrumentation.js -j 2 && tsc --build ./transform/tsconfig.json && tsc --build ./src/tsconfig.json",
1617
"test:as": "node bin/as-test.js",
1718
"test:ts": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
1819
"test:cpp": "cmake -B build -S . && cmake --build build --parallel 2 --target wasm-instrumentation-test wasm-opt && build/bin/wasm-instrumentation-test",
19-
"test": "npm run test:as && npm run test:ts && npm run test:cpp",
20+
"test:e2e": " node tests/e2e/run.js",
21+
"test": "npm run test:as && npm run test:ts && npm run test:cpp && npm run test:e2e",
2022
"lint:ts": "eslint src transform tests/ts/test --max-warnings=0",
2123
"lint:as": "npx eslint --config ./assembly/eslint.config.mjs assembly --max-warnings=0",
2224
"lint": "npm run lint:ts && prettier -c .",
@@ -48,6 +50,7 @@
4850
"@types/node": "^22.0.0",
4951
"assemblyscript-prettier": "^3.0.1",
5052
"cross-env": "^7.0.3",
53+
"diff": "^8.0.2",
5154
"jest": "^29.5.0",
5255
"prettier": "^3.0.0",
5356
"ts-jest": "^29.1.0",

src/executionResult.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class ExecutionResult {
2323
for (const msg of value) {
2424
const [index, actualValue, expectValue] = msg;
2525
const debugLocation = expectInfo.get(index);
26-
let errorMsg = `${debugLocation ?? ""}\tvalue: ${actualValue}\texpect: ${expectValue}`;
26+
let errorMsg = `${debugLocation ?? ""} value: ${actualValue} expect: ${expectValue}`;
2727
if (errorMsg.length > 160) {
28-
errorMsg = `${debugLocation ?? ""}\nvalue: \n\t${actualValue}\nexpect: \n\t${expectValue}`;
28+
errorMsg = `${debugLocation ?? ""}\nvalue: \n ${actualValue}\nexpect: \n ${expectValue}`;
2929
}
3030
errorMsgs.push(errorMsg);
3131
}
@@ -52,9 +52,9 @@ export class ExecutionResult {
5252
if (this.fail !== 0) {
5353
log(chalk.red("Error Message: "));
5454
for (const [testcaseName, { assertMessages, logMessages }] of this.failedInfos.entries()) {
55-
log(`\t${testcaseName}: `);
55+
log(` ${testcaseName}: `);
5656
for (const assertMessage of assertMessages) {
57-
log("\t\t" + chalk.yellow(assertMessage));
57+
log(" " + chalk.yellow(assertMessage));
5858
}
5959
for (const logMessage of logMessages ?? []) {
6060
log(chalk.gray(logMessage));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os from "node:os";
2+
import path from "node:path";
3+
4+
const tmpFolder = path.join(os.tmpdir(), "as-test-e2e");
5+
6+
export default {
7+
include: ["tests/e2e/printLogInFailedInfo"],
8+
imports(runtime) {
9+
return {
10+
env: {
11+
log: (msg) => {
12+
runtime.framework.log(runtime.exports.__getString(msg));
13+
},
14+
},
15+
};
16+
},
17+
temp: tmpFolder,
18+
output: tmpFolder,
19+
mode: [],
20+
};

tests/e2e/printLogInFailedInfo/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function log(msg: string): void;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test, expect } from "../../../assembly";
2+
import { log } from "./env";
3+
4+
test("failed test", () => {
5+
log("This is a log message for the failed test.");
6+
expect(1 + 1).equal(3);
7+
});
8+
9+
test("succeed test", () => {
10+
log("This is a log message for the succeed test.");
11+
expect(1 + 1).equal(2);
12+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "assemblyscript/std/assembly.json",
3+
"include": ["./**/*.ts"]
4+
}

tests/e2e/run.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import assert from "node:assert";
2+
import { exec } from "node:child_process";
3+
import { diffChars } from "diff";
4+
import chalk from "chalk";
5+
6+
function assertStringEq(s1, s2) {
7+
const parts = diffChars(s1, s2);
8+
const diffs = [];
9+
let hasDiff = false;
10+
for (const part of parts) {
11+
if (part.added) {
12+
hasDiff = true;
13+
diffs.push(chalk.bgGreen(part.value));
14+
} else if (part.removed) {
15+
hasDiff = true;
16+
diffs.push(chalk.bgRed(part.value));
17+
} else {
18+
diffs.push(part.value);
19+
}
20+
}
21+
assert(!hasDiff, diffs.join(""));
22+
}
23+
24+
console.log("Running e2e test: printLogInFailedInfo");
25+
exec("node ./bin/as-test.js --config tests/e2e/printLogInFailedInfo/as-test.config.js", (error, stdout, stderr) => {
26+
assert(error.code === 255);
27+
const expectStdOut = `
28+
code analysis: OK
29+
compile testcases: OK
30+
instrument: OK
31+
execute testcases: OK
32+
33+
test case: 1/2 (success/total)
34+
35+
Error Message:
36+
failed test:
37+
tests/e2e/printLogInFailedInfo/source.test.ts:6:2 value: 2 expect: = 3
38+
This is a log message for the failed test.
39+
`.trimStart();
40+
41+
assertStringEq(expectStdOut, stdout);
42+
});

0 commit comments

Comments
 (0)