Skip to content

Commit

Permalink
fix(browser): fix dynamic import inside worker (#6569)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 25, 2024
1 parent f8ff76a commit ea2d429
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
}
},
},
{
name: 'vitest:browser:worker',
transform(code, id, _options) {
// https://github.com/vitejs/vite/blob/ba56cf43b5480f8519349f7d7fe60718e9af5f1a/packages/vite/src/node/plugins/worker.ts#L46
if (/(?:\?|&)worker_file&type=\w+(?:&|$)/.test(id)) {
const s = new MagicString(code)
s.prepend('globalThis.__vitest_browser_runner__ = { wrapDynamicImport: f => f() };\n')
return {
code: s.toString(),
map: s.generateMap({ hires: 'boundary' }),
}
}
},
},
// TODO: remove this when @testing-library/vue supports ESM
{
name: 'vitest:browser:support-testing-library',
Expand Down
11 changes: 11 additions & 0 deletions test/browser/fixtures/worker/src/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'

test('worker dynamic dep', async () => {
const worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
const data = await new Promise((resolve, reject) => {
worker.addEventListener("message", (e) => resolve(e.data))
worker.addEventListener("messageerror", (e) => reject(e))
worker.postMessage("ping");
});
expect(data).toMatchInlineSnapshot(`"worker-dynamic-dep-ok"`);
})
1 change: 1 addition & 0 deletions test/browser/fixtures/worker/src/worker-dynamic-dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "worker-dynamic-dep-ok"
4 changes: 4 additions & 0 deletions test/browser/fixtures/worker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
self.onmessage = async () => {
const mod = await import("./worker-dynamic-dep");
self.postMessage(mod.default);
}
17 changes: 17 additions & 0 deletions test/browser/fixtures/worker/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'

const provider = process.env.PROVIDER || 'playwright'
const name =
process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')

export default defineConfig({
cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)),
test: {
browser: {
enabled: true,
provider,
name,
},
},
})
13 changes: 13 additions & 0 deletions test/browser/specs/worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from 'vitest'
import { runBrowserTests } from './utils'

test('worker', async () => {
const { ctx } = await runBrowserTests({
root: './fixtures/worker',
})
expect(Object.fromEntries(ctx.state.getFiles().map(f => [f.name, f.result.state]))).toMatchInlineSnapshot(`
{
"src/basic.test.ts": "pass",
}
`)
})

0 comments on commit ea2d429

Please sign in to comment.