Skip to content
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
28 changes: 19 additions & 9 deletions test/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ suite('Bindings', async () => {
'utf8',
);

const test = await import(`./cases/${name}/test.js`);
// NOTE: import separated from await due to issues on windows (see note in util.js)
const testcasePromise = import(`./cases/${name}/test.js`);
const testcase = await testcasePromise;

// Determine the relevant WIT world to use
let witWorld,
witPath,
worldName,
isWasiTarget = false;
if (test.worldName) {
if (testcase.worldName) {
witPath = fileURLToPath(new URL('./wit', import.meta.url));
worldName = test.worldName;
worldName = testcase.worldName;
isWasiTarget = true;
} else {
try {
Expand Down Expand Up @@ -61,12 +63,13 @@ suite('Bindings', async () => {
}
}

const enableFeatures = test.enableFeatures || ['http'];
const enableFeatures = testcase.enableFeatures || ['http'];
const disableFeatures =
test.disableFeatures ||
testcase.disableFeatures ||
(isWasiTarget ? [] : ['random', 'clocks', 'http', 'stdio']);

let testArg;
let instance;
try {
const { component, imports } = await componentize(source, {
sourceName: `${name}.js`,
Expand All @@ -77,6 +80,7 @@ suite('Bindings', async () => {
disableFeatures: maybeLogging(disableFeatures),
debugBuild: DEBUG_TEST_ENABLED,
});

const map = {
'wasi:cli-base/*': '@bytecodealliance/preview2-shim/cli-base#*',
'wasi:clocks/*': '@bytecodealliance/preview2-shim/clocks#*',
Expand All @@ -88,6 +92,7 @@ suite('Bindings', async () => {
'wasi:random/*': '@bytecodealliance/preview2-shim/random#*',
'wasi:sockets/*': '@bytecodealliance/preview2-shim/sockets#*',
};

for (let [impt] of imports) {
if (impt.startsWith('wasi:')) continue;
if (impt.startsWith('[')) impt = impt.slice(impt.indexOf(']') + 1);
Expand Down Expand Up @@ -130,15 +135,20 @@ suite('Bindings', async () => {
const outputPath = fileURLToPath(
new URL(`./output/${name}/${name}.js`, import.meta.url),
);
var instance = await import(outputPath);

// NOTE: import separated from await due to issues on windows (see note in util.js)
const instancePromise = import(outputPath);
instance = await instancePromise;

} catch (e) {
if (test.err) {
test.err(e);
if (testcase.err) {
testcase.err(e);
return;
}
throw e;
}
await test.test(instance, testArg);

await testcase.test(instance, testArg);
});
}
});
5 changes: 4 additions & 1 deletion test/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ suite('Builtins', async () => {
for (const filename of builtins) {
const name = filename.slice(0, -3);
test.concurrent(name, async () => {
// NOTE: import separated from await due to issues on windows (see note in util.js)
const builtinModulePromise = import(`./builtins/${name}.js`);

const {
source,
test: runTest,
disableFeatures,
enableFeatures,
} = await import(`./builtins/${name}.js`);
} = await builtinModulePromise;

const { component } = await componentize(
source,
Expand Down
21 changes: 16 additions & 5 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ export async function setupComponent(opts) {
}

const componentJsPath = join(wasiDir, 'component.js');
var instance = await import(componentJsPath);

return {
instance,
outputDir,
};
// NOTE: On Windows, vitest has stated suddenly hanging when processing code with an
// `await import(...)` in it, when the `...` is a bare identifier.
//
// This can be worked around in many ways:
// - doing a trivial tranformation on the identifier/argument
// - returning a more traditional promise
// - separating await and import() calls
//
// Here we choose to return the promise more traditionally
return import(componentJsPath)
.then(instance => {
return {
instance,
outputDir,
};
});
}