|
| 1 | +import { BroadcastChannel, Worker } from 'node:worker_threads' |
| 2 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 3 | +import type { BirpcReturn } from 'birpc' |
| 4 | +import { createBirpc } from 'birpc' |
| 5 | +import { DevEnvironment } from '../../..' |
| 6 | +import { type ViteDevServer, createServer } from '../../../server' |
| 7 | + |
| 8 | +describe('running module runner inside a worker and using the ModuleRunnerTransport#invoke API', () => { |
| 9 | + let worker: Worker |
| 10 | + let server: ViteDevServer |
| 11 | + let rpc: BirpcReturn< |
| 12 | + unknown, |
| 13 | + { invoke: (data: any) => Promise<{ result: any } | { error: any }> } |
| 14 | + > |
| 15 | + let handleInvoke: (data: any) => Promise<{ result: any } | { error: any }> |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + worker = new Worker( |
| 19 | + new URL('./fixtures/worker.invoke.mjs', import.meta.url), |
| 20 | + { |
| 21 | + stdout: true, |
| 22 | + }, |
| 23 | + ) |
| 24 | + await new Promise<void>((resolve, reject) => { |
| 25 | + worker.on('message', () => resolve()) |
| 26 | + worker.on('error', reject) |
| 27 | + }) |
| 28 | + server = await createServer({ |
| 29 | + root: __dirname, |
| 30 | + logLevel: 'error', |
| 31 | + server: { |
| 32 | + middlewareMode: true, |
| 33 | + watch: null, |
| 34 | + hmr: { |
| 35 | + port: 9610, |
| 36 | + }, |
| 37 | + }, |
| 38 | + environments: { |
| 39 | + worker: { |
| 40 | + dev: { |
| 41 | + createEnvironment: (name, config) => { |
| 42 | + return new DevEnvironment(name, config, { |
| 43 | + hot: false, |
| 44 | + }) |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | + handleInvoke = (data: any) => server.environments.ssr.hot.handleInvoke(data) |
| 51 | + rpc = createBirpc( |
| 52 | + { |
| 53 | + invoke: (data: any) => handleInvoke(data), |
| 54 | + }, |
| 55 | + { |
| 56 | + post: (data) => worker.postMessage(data), |
| 57 | + on: (data) => worker.on('message', data), |
| 58 | + }, |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + afterAll(() => { |
| 63 | + server.close() |
| 64 | + worker.terminate() |
| 65 | + rpc.$close() |
| 66 | + }) |
| 67 | + |
| 68 | + async function run(id: string) { |
| 69 | + const channel = new BroadcastChannel('vite-worker:invoke') |
| 70 | + return new Promise<any>((resolve, reject) => { |
| 71 | + channel.onmessage = (event) => { |
| 72 | + try { |
| 73 | + resolve((event as MessageEvent).data) |
| 74 | + } catch (e) { |
| 75 | + reject(e) |
| 76 | + } |
| 77 | + } |
| 78 | + channel.postMessage({ id }) |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + it('correctly runs ssr code', async () => { |
| 83 | + const output = await run('./fixtures/default-string.ts') |
| 84 | + expect(output).toStrictEqual({ |
| 85 | + result: 'hello world', |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('triggers an error', async () => { |
| 90 | + handleInvoke = async () => ({ error: new Error('This is an Invoke Error') }) |
| 91 | + const output = await run('dummy') |
| 92 | + expect(output).not.toHaveProperty('result') |
| 93 | + expect(output.error).toContain('Error: This is an Invoke Error') |
| 94 | + }) |
| 95 | + |
| 96 | + it('triggers an unknown error', async () => { |
| 97 | + handleInvoke = async () => ({ error: 'a string instead of an error' }) |
| 98 | + const output = await run('dummy') |
| 99 | + expect(output).not.toHaveProperty('result') |
| 100 | + expect(output.error).toContain('Error: Unknown invoke error') |
| 101 | + }) |
| 102 | +}) |
0 commit comments