Skip to content

Commit

Permalink
skips tests in JSPI when the NodeJS version doesn't support it
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinrp committed Jan 28, 2025
1 parent f3c58ca commit 22e6033
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 116 deletions.
178 changes: 90 additions & 88 deletions test/async.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,98 +130,100 @@ export async function asyncBrowserTest(_fixtures) {
await componentCleanup();
});

test("Transpile async (browser, JSPI)", async () => {
const componentName = "async-call";
const {
instance,
cleanup: componentCleanup,
outputDir,
} = await setupAsyncTest({
asyncMode: "jspi",
component: {
name: "async_call",
path: resolve("test/fixtures/components/async_call.component.wasm"),
imports: {
"something:test/test-interface": {
callAsync: async () => "called async",
callSync: () => "called sync",
if (typeof WebAssembly?.Suspending === "function") {
test("Transpile async (browser, JSPI)", async () => {
const componentName = "async-call";
const {
instance,
cleanup: componentCleanup,
outputDir,
} = await setupAsyncTest({
asyncMode: "jspi",
component: {
name: "async_call",
path: resolve("test/fixtures/components/async_call.component.wasm"),
imports: {
"something:test/test-interface": {
callAsync: async () => "called async",
callSync: () => "called sync",
},
},
},
},
jco: {
transpile: {
extraArgs: {
asyncImports: ["something:test/test-interface#call-async"],
asyncExports: ["run-async"],
jco: {
transpile: {
extraArgs: {
asyncImports: ["something:test/test-interface#call-async"],
asyncExports: ["run-async"],
},
},
},
},
});
const moduleName = componentName.toLowerCase().replaceAll("-", "_");
const moduleRelPath = `${moduleName}/${moduleName}.js`;

strictEqual(
instance.runSync instanceof AsyncFunction,
false,
"runSync() should be a sync function",
);
strictEqual(
instance.runAsync instanceof AsyncFunction,
true,
"runAsync() should be an async function",
);

// Start a test web server
const {
server,
serverPort,
cleanup: webServerCleanup,
} = await startTestWebServer({
routes: [
// NOTE: the goal here is to serve relative paths via the browser hash
//
// (1) browser visits test page (served by test web server)
// (2) browser requests component itself by looking at URL hash fragment
// (i.e. "#transpiled:async_call/async_call.js" -> , "/transpiled/async_call/async_call.js")
// (i.e. "/transpiled/async_call/async_call.js" -> file read of /tmp/xxxxxx/async_call/async_call.js)
{
urlPrefix: "/transpiled/",
basePathURL: pathToFileURL(`${outputDir}/`),
},
// Serve all other files (ex. the initial HTML for the page)
{ basePathURL: import.meta.url },
],
});

// Start a browser to visit the test server
const browser = await puppeteer.launch({
args: [
"--enable-experimental-webassembly-jspi",
"--flag-switches-begin",
"--enable-features=WebAssemblyExperimentalJSPI",
"--flag-switches-end",
],
});

// Load the test page in the browser, which will trigger tests against
// the component and/or related browser polyfills
const {
page,
output: { json },
} = await loadTestPage({
browser,
serverPort,
path: "fixtures/browser/test-pages/something__test.async.html",
hash: `transpiled:${moduleRelPath}`,
});
const moduleName = componentName.toLowerCase().replaceAll("-", "_");
const moduleRelPath = `${moduleName}/${moduleName}.js`;

strictEqual(
instance.runSync instanceof AsyncFunction,
false,
"runSync() should be a sync function",
);
strictEqual(
instance.runAsync instanceof AsyncFunction,
true,
"runAsync() should be an async function",
);

// Start a test web server
const {
server,
serverPort,
cleanup: webServerCleanup,
} = await startTestWebServer({
routes: [
// NOTE: the goal here is to serve relative paths via the browser hash
//
// (1) browser visits test page (served by test web server)
// (2) browser requests component itself by looking at URL hash fragment
// (i.e. "#transpiled:async_call/async_call.js" -> , "/transpiled/async_call/async_call.js")
// (i.e. "/transpiled/async_call/async_call.js" -> file read of /tmp/xxxxxx/async_call/async_call.js)
{
urlPrefix: "/transpiled/",
basePathURL: pathToFileURL(`${outputDir}/`),
},
// Serve all other files (ex. the initial HTML for the page)
{ basePathURL: import.meta.url },
],
});

// Start a browser to visit the test server
const browser = await puppeteer.launch({
args: [
"--enable-experimental-webassembly-jspi",
"--flag-switches-begin",
"--enable-features=WebAssemblyExperimentalJSPI",
"--flag-switches-end",
],
});

// Load the test page in the browser, which will trigger tests against
// the component and/or related browser polyfills
const {
page,
output: { json },
} = await loadTestPage({
browser,
serverPort,
path: "fixtures/browser/test-pages/something__test.async.html",
hash: `transpiled:${moduleRelPath}`,
});

// Check the output expected to be returned from handle of the
// guest export (this depends on the component)
deepStrictEqual(json, { responseText: "callAsync" });

await browser.close();
await webServerCleanup();
await componentCleanup();
});

// Check the output expected to be returned from handle of the
// guest export (this depends on the component)
deepStrictEqual(json, { responseText: "callAsync" });

await browser.close();
await webServerCleanup();
await componentCleanup();
});
}
});
}
58 changes: 30 additions & 28 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,43 @@ export async function asyncTest(_fixtures) {
ok(source.toString().includes("export { test"));
});

test("Transpile async (NodeJS, JSPI)", async () => {
const { instance, cleanup, component } = await setupAsyncTest({
asyncMode: "jspi",
component: {
name: "async_call",
path: resolve("test/fixtures/components/async_call.component.wasm"),
imports: {
'something:test/test-interface': {
callAsync: async () => "called async",
callSync: () => "called sync",
if (typeof WebAssembly?.Suspending === "function") {
test("Transpile async (NodeJS, JSPI)", async () => {
const { instance, cleanup, component } = await setupAsyncTest({
asyncMode: "jspi",
component: {
name: "async_call",
path: resolve("test/fixtures/components/async_call.component.wasm"),
imports: {
'something:test/test-interface': {
callAsync: async () => "called async",
callSync: () => "called sync",
},
},
},
},
jco: {
transpile: {
extraArgs: {
asyncImports: [
"something:test/test-interface#call-async",
],
asyncExports: [
"run-async",
],
jco: {
transpile: {
extraArgs: {
asyncImports: [
"something:test/test-interface#call-async",
],
asyncExports: [
"run-async",
],
},
},
},
},
});
});

strictEqual(instance.runSync instanceof AsyncFunction, false, "runSync() should be a sync function");
strictEqual(instance.runAsync instanceof AsyncFunction, true, "runAsync() should be an async function");
strictEqual(instance.runSync instanceof AsyncFunction, false, "runSync() should be a sync function");
strictEqual(instance.runAsync instanceof AsyncFunction, true, "runAsync() should be an async function");

strictEqual(instance.runSync(), "called sync");
strictEqual(await instance.runAsync(), "called async");
strictEqual(instance.runSync(), "called sync");
strictEqual(await instance.runAsync(), "called async");

await cleanup();
});
await cleanup();
});
}

test("Transpile async (NodeJS, Asyncify)", async () => {
const { instance, cleanup } = await setupAsyncTest({
Expand Down

0 comments on commit 22e6033

Please sign in to comment.