Skip to content

Commit ba0712e

Browse files
feat(tests): replace mocha with vitest
1 parent f0a2937 commit ba0712e

File tree

199 files changed

+1918
-1550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+1918
-1550
lines changed

package-lock.json

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

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"devDependencies": {
1818
"@bytecodealliance/preview2-shim": "^0.17.1",
1919
"cross-env": "^7.0.3",
20-
"mocha": "^11.1.0",
21-
"semver": "^7.7.2"
20+
"semver": "^7.7.2",
21+
"vitest": "^3.2.4"
2222
},
2323
"dependencies": {
2424
"@bytecodealliance/jco": "^1.9.1",
@@ -35,10 +35,9 @@
3535
"build:release": "make release",
3636
"build:weval": "make release-weval",
3737
"build:debug": "make debug",
38-
"test": "mocha -u tdd test/test.js --timeout 120000",
39-
"test:release": "mocha -u tdd test/test.js --timeout 120000",
40-
"test:weval": "cross-env WEVAL_TEST=1 mocha -u tdd test/test.js --timeout 120000",
41-
"test:debug": "cross-env DEBUG_TEST=1 mocha -u tdd test/test.js --timeout 120000",
38+
"test": "vitest run -c test/vitest.ts",
39+
"test:weval": "cross-env WEVAL_TEST=1 vitest run -c test/vitest.ts",
40+
"test:debug": "cross-env DEBUG_TEST=1 vitest run -c test/vitest.ts",
4241
"prepack": "node scripts/prepack.mjs"
4342
},
4443
"files": [

test/bindings.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { fileURLToPath, URL } from 'node:url';
2+
import { readFile, readdir, mkdir, writeFile } from 'node:fs/promises';
3+
4+
import { componentize } from '@bytecodealliance/componentize-js';
5+
import { transpile } from '@bytecodealliance/jco';
6+
7+
import { suite, test } from 'vitest';
8+
9+
import {
10+
DEBUG_TRACING_ENABLED,
11+
LOG_DEBUGGING_ENABLED,
12+
WEVAL_TEST_ENABLED,
13+
DEBUG_TEST_ENABLED,
14+
maybeLogging,
15+
} from './util.js';
16+
17+
suite('Bindings', async () => {
18+
const bindingsCases = await readdir(new URL('./fixtures/cases', import.meta.url));
19+
20+
for (const name of bindingsCases) {
21+
test.concurrent(name, async () => {
22+
const source = await readFile(
23+
new URL(`./fixtures/cases/${name}/source.js`, import.meta.url),
24+
'utf8',
25+
);
26+
27+
const test = await import(`./fixtures/cases/${name}/test.js`);
28+
29+
// Determine the relevant WIT world to use
30+
let witWorld,
31+
witPath,
32+
worldName,
33+
isWasiTarget = false;
34+
if (test.worldName) {
35+
witPath = fileURLToPath(new URL('./wit', import.meta.url));
36+
worldName = test.worldName;
37+
isWasiTarget = true;
38+
} else {
39+
try {
40+
witWorld = await readFile(
41+
new URL(`./fixtures/cases/${name}/world.wit`, import.meta.url),
42+
'utf8',
43+
);
44+
} catch (e) {
45+
if (e?.code == 'ENOENT') {
46+
try {
47+
isWasiTarget = true;
48+
witPath = fileURLToPath(
49+
new URL(`./fixtures/cases/${name}/wit`, import.meta.url),
50+
);
51+
await readdir(witPath);
52+
} catch (e) {
53+
if (e?.code === 'ENOENT') {
54+
witPath = fileURLToPath(new URL('./wit', import.meta.url));
55+
worldName = 'test2';
56+
} else {
57+
throw e;
58+
}
59+
}
60+
} else {
61+
throw e;
62+
}
63+
}
64+
}
65+
66+
const enableFeatures = test.enableFeatures || ['http'];
67+
const disableFeatures =
68+
test.disableFeatures ||
69+
(isWasiTarget ? [] : ['random', 'clocks', 'http', 'stdio']);
70+
71+
let testArg;
72+
try {
73+
const { component, imports } = await componentize(source, {
74+
sourceName: `${name}.js`,
75+
witWorld,
76+
witPath,
77+
worldName,
78+
enableFeatures,
79+
disableFeatures: maybeLogging(disableFeatures),
80+
enableAot: WEVAL_TEST_ENABLED,
81+
debugBuild: DEBUG_TEST_ENABLED,
82+
});
83+
const map = {
84+
'wasi:cli-base/*': '@bytecodealliance/preview2-shim/cli-base#*',
85+
'wasi:clocks/*': '@bytecodealliance/preview2-shim/clocks#*',
86+
'wasi:filesystem/*': '@bytecodealliance/preview2-shim/filesystem#*',
87+
'wasi:http/*': '@bytecodealliance/preview2-shim/http#*',
88+
'wasi:io/*': '@bytecodealliance/preview2-shim/io#*',
89+
'wasi:logging/*': '@bytecodealliance/preview2-shim/logging#*',
90+
'wasi:poll/*': '@bytecodealliance/preview2-shim/poll#*',
91+
'wasi:random/*': '@bytecodealliance/preview2-shim/random#*',
92+
'wasi:sockets/*': '@bytecodealliance/preview2-shim/sockets#*',
93+
};
94+
for (let [impt] of imports) {
95+
if (impt.startsWith('wasi:')) continue;
96+
if (impt.startsWith('[')) impt = impt.slice(impt.indexOf(']') + 1);
97+
let importName = impt.split('/').pop();
98+
if (importName === 'test') importName = 'imports';
99+
map[impt] = `../../fixtures/cases/${name}/${importName}.js`;
100+
}
101+
102+
const {
103+
files,
104+
imports: componentImports,
105+
exports: componentExports,
106+
} = await transpile(component, {
107+
name,
108+
map,
109+
wasiShim: true,
110+
validLiftingOptimization: false,
111+
tracing: DEBUG_TRACING_ENABLED,
112+
});
113+
114+
testArg = { imports, componentImports, componentExports };
115+
116+
await mkdir(new URL(`./output/${name}/interfaces`, import.meta.url), {
117+
recursive: true,
118+
});
119+
120+
await writeFile(
121+
new URL(`./output/${name}.component.wasm`, import.meta.url),
122+
component,
123+
);
124+
125+
for (const file of Object.keys(files)) {
126+
let source = files[file];
127+
await writeFile(
128+
new URL(`./output/${name}/${file}`, import.meta.url),
129+
source,
130+
);
131+
}
132+
133+
var instance = await import(`./output/${name}/${name}.js`);
134+
} catch (e) {
135+
if (test.err) {
136+
test.err(e);
137+
return;
138+
}
139+
throw e;
140+
}
141+
await test.test(instance, testArg);
142+
});
143+
}
144+
});

test/builtins.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { readFile, readdir, mkdir, writeFile } from 'node:fs/promises';
2+
import { spawn } from 'node:child_process';
3+
import { fileURLToPath, URL } from 'node:url';
4+
import { strictEqual } from 'node:assert';
5+
6+
import { componentize } from '@bytecodealliance/componentize-js';
7+
import { transpile } from '@bytecodealliance/jco';
8+
9+
import { suite, test, assert } from 'vitest';
10+
11+
import {
12+
DEBUG_TRACING_ENABLED,
13+
LOG_DEBUGGING_ENABLED,
14+
WEVAL_TEST_ENABLED,
15+
DEBUG_TEST_ENABLED,
16+
maybeLogging,
17+
} from './util.js';
18+
19+
suite('Builtins', async () => {
20+
const builtins = await readdir(
21+
new URL('./fixtures/builtins', import.meta.url),
22+
);
23+
24+
for (const filename of builtins) {
25+
const name = filename.slice(0, -3);
26+
test.concurrent(name, async () => {
27+
const {
28+
source,
29+
test: runTest,
30+
disableFeatures,
31+
enableFeatures,
32+
} = await import(`./fixtures/builtins/${name}.js`);
33+
34+
const { component } = await componentize(
35+
source,
36+
`
37+
package local:runworld;
38+
world runworld {
39+
export run: func();
40+
}
41+
`,
42+
{
43+
sourceName: `${name}.js`,
44+
// also test the debug build while we are about it (unless testing Weval)
45+
debugBuild: DEBUG_TEST_ENABLED,
46+
enableFeatures,
47+
disableFeatures: maybeLogging(disableFeatures),
48+
enableAot: WEVAL_TEST_ENABLED,
49+
},
50+
);
51+
52+
const { files } = await transpile(component, {
53+
name,
54+
wasiShim: true,
55+
tracing: DEBUG_TRACING_ENABLED,
56+
});
57+
58+
await mkdir(new URL(`./output/${name}/interfaces`, import.meta.url), {
59+
recursive: true,
60+
});
61+
62+
await writeFile(
63+
new URL(`./output/${name}.component.wasm`, import.meta.url),
64+
component,
65+
);
66+
67+
for (const file of Object.keys(files)) {
68+
await writeFile(
69+
new URL(`./output/${name}/${file}`, import.meta.url),
70+
files[file],
71+
);
72+
}
73+
74+
await writeFile(
75+
new URL(`./output/${name}/run.js`, import.meta.url),
76+
`
77+
import { run } from './${name}.js';
78+
run();
79+
`,
80+
);
81+
82+
try {
83+
await runTest(async function run() {
84+
let stdout = '',
85+
stderr = '',
86+
timeout;
87+
try {
88+
await new Promise((resolve, reject) => {
89+
const cp = spawn(
90+
process.argv[0],
91+
[
92+
fileURLToPath(
93+
new URL(`./output/${name}/run.js`, import.meta.url),
94+
),
95+
],
96+
{ stdio: 'pipe' },
97+
);
98+
cp.stdout.on('data', (chunk) => {
99+
stdout += chunk;
100+
});
101+
cp.stderr.on('data', (chunk) => {
102+
stderr += chunk;
103+
});
104+
cp.on('error', reject);
105+
cp.on('exit', (code) =>
106+
code === 0 ? resolve() : reject(new Error(stderr || stdout)),
107+
);
108+
timeout = setTimeout(() => {
109+
reject(
110+
new Error(
111+
'test timed out with output:\n' +
112+
stdout +
113+
'\n\nstderr:\n' +
114+
stderr,
115+
),
116+
);
117+
}, 10_000);
118+
});
119+
} catch (err) {
120+
throw { err, stdout, stderr };
121+
} finally {
122+
clearTimeout(timeout);
123+
}
124+
125+
return { stdout, stderr };
126+
});
127+
} catch (err) {
128+
if (err.stderr) console.error(err.stderr);
129+
throw err.err || err;
130+
}
131+
});
132+
}
133+
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { strictEqual } from 'node:assert';
1+
import { assert } from 'vitest';
22

33
export const source = `
44
export function run () {
@@ -50,10 +50,10 @@ export const source = `
5050

5151
export async function test (run) {
5252
const { stdout, stderr } = await run();
53-
strictEqual(stdout, `{ a: { value: "a" }, b: { c: "d" }, e: ["f"], g: [{ g: "i" }], l: [ l () {
53+
assert.strictEqual(stdout, `{ a: { value: "a" }, b: { c: "d" }, e: ["f"], g: [{ g: "i" }], l: [ l () {
5454
5555
}], m: [Getter], n: [Getter], o: [ function () {
5656
5757
}], p: [ () => {}], q: 5, s: 29879287298374924, t: Set(3) { 1, 2, 3 }, u: Map(3) { 1 => 2, 3 => 4, [ function foo () {}] => {} }, v: Symbol.for("blah"), w: Symbol(), x: undefined, y: null, z: URL { hash: "", host: "site.com", hostname: "site.com", href: "https://site.com/x?a&b", origin: "https://site.com", password: "", pathname: "/x", port: "", protocol: "https:", search: "?a&b", searchParams: URLSearchParams {}, username: "" }, zz: Uint8Array [1, 2, 3], zzz: Z {} }\n`);
58-
strictEqual(stderr, '');
58+
assert.strictEqual(stderr, '');
5959
}
File renamed without changes.

0 commit comments

Comments
 (0)