Skip to content

chore: enhance e2e test by extracting common part #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@

/docs/.vitepress/dist
/docs/.vitepress/cache

/tests/e2e/*/tmp
19 changes: 19 additions & 0 deletions tests/e2e/assertFailed/as-test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from "node:path";

const __dirname = path.dirname(new URL(import.meta.url).pathname);

export default {
include: [__dirname],
imports(runtime) {
return {
env: {
log: (msg) => {
runtime.framework.log(runtime.exports.__getString(msg));
},
},
};
},
temp: path.join(__dirname, "tmp"),
output: path.join(__dirname, "tmp"),
mode: [],
};
1 change: 1 addition & 0 deletions tests/e2e/assertFailed/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function log(msg: string): void;
12 changes: 12 additions & 0 deletions tests/e2e/assertFailed/source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test, expect } from "../../../assembly";
import { log } from "./env";

test("failed test", () => {
log("This is a log message for the failed test.");
assert(false, "This is AS assert failure");
});

test("succeed test", () => {
log("This is a log message for the succeed test.");
expect(1 + 1).equal(2);
});
3 changes: 3 additions & 0 deletions tests/e2e/assertFailed/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
code analysis: OK
compile testcases: OK
instrument: OK
4 changes: 4 additions & 0 deletions tests/e2e/assertFailed/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
8 changes: 5 additions & 3 deletions tests/e2e/printLogInFailedInfo/as-test.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os from "node:os";
import path from "node:path";
import { URL } from "node:url";

const tmpFolder = path.join(os.tmpdir(), "as-test-e2e");
const __dirname = path.dirname(new URL(import.meta.url).pathname);

export default {
include: ["tests/e2e/printLogInFailedInfo"],
include: [__dirname],
imports(runtime) {
return {
env: {
Expand All @@ -14,7 +16,7 @@ export default {
},
};
},
temp: tmpFolder,
output: tmpFolder,
temp: path.join(__dirname, "tmp"),
output: path.join(__dirname, "tmp"),
mode: [],
};
11 changes: 11 additions & 0 deletions tests/e2e/printLogInFailedInfo/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
code analysis: OK
compile testcases: OK
instrument: OK
execute testcases: OK

test case: 1/2 (success/total)

Error Message:
failed test:
tests/e2e/printLogInFailedInfo/source.test.ts:6:2 value: 2 expect: = 3
This is a log message for the failed test.
79 changes: 48 additions & 31 deletions tests/e2e/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,58 @@ import assert from "node:assert";
import { exec } from "node:child_process";
import { diffChars } from "diff";
import chalk from "chalk";
import { argv } from "node:process";
import { readFileSync } from "node:fs";

function assertStringEq(s1, s2) {
const parts = diffChars(s1, s2);
const diffs = [];
let hasDiff = false;
for (const part of parts) {
if (part.added) {
hasDiff = true;
diffs.push(chalk.bgGreen(part.value));
} else if (part.removed) {
hasDiff = true;
diffs.push(chalk.bgRed(part.value));
} else {
diffs.push(part.value);
}
}
assert(!hasDiff, diffs.join(""));
function getDiff(s1, s2) {
const handleEscape = (c) => {
if (c === "\n") return "\n'\\n'";
return c;
};
return diffChars(s1, s2)
.map((part) => {
if (part.added) {
return chalk.bgGreen(handleEscape(part.value));
} else if (part.removed) {
return chalk.bgRed(handleEscape(part.value));
} else {
return part.value;
}
})
.join("");
}

console.log("Running e2e test: printLogInFailedInfo");
exec("node ./bin/as-test.js --config tests/e2e/printLogInFailedInfo/as-test.config.js", (error, stdout, stderr) => {
assert(error.code === 255);
const expectStdOut = `
code analysis: OK
compile testcases: OK
instrument: OK
execute testcases: OK
function isEnabled(name) {
const enabledTests = argv.slice(2);
if (enabledTests.length === 0) {
return true; // Run all tests by default
}
return enabledTests.includes(name);
}

test case: 1/2 (success/total)
function runEndToEndTest(name, handle) {
if (isEnabled(name)) {
console.log(`Running e2e test: ${name}`);
exec(`node ./bin/as-test.js --config tests/e2e/${name}/as-test.config.js`, (error, stdout, stderr) => {
// standard check
const expectStdOut = readFileSync(`tests/e2e/${name}/stdout.txt`, "utf-8");
if (expectStdOut !== stdout) {
console.log(`=========STDOUT ${name}=========`);
console.log(getDiff(expectStdOut, stdout));
console.log(`=========STDERR ${name}=========`);
console.log(stderr);
process.exit(1);
}
// customize check
handle(error, stdout, stderr);
});
}
}

Error Message:
failed test:
tests/e2e/printLogInFailedInfo/source.test.ts:6:2 value: 2 expect: = 3
This is a log message for the failed test.
`.trimStart();
runEndToEndTest("printLogInFailedInfo", (error, stdout, stderr) => {
assert(error.code === 255);
});

assertStringEq(expectStdOut, stdout);
runEndToEndTest("assertFailed", (error, stdout, stderr) => {
assert(error.code === 255);
});