diff --git a/test/async.browser.js b/test/async.browser.js index bc1417546..de63b16c0 100644 --- a/test/async.browser.js +++ b/test/async.browser.js @@ -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(); - }); + } }); } diff --git a/test/async.js b/test/async.js index d888c6bf5..0742f69fc 100644 --- a/test/async.js +++ b/test/async.js @@ -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({