Skip to content

Commit

Permalink
test: use handleInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 4, 2024
1 parent a05f99e commit 72ee49b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ if (!parentPort) {
throw new Error('File "worker.js" must be run in a worker thread')
}

let invokeReturn;

/** @type {import('worker_threads').MessagePort} */
const pPort = parentPort

createBirpc({
// @ts-ignore
setInvokeReturn(returnValue) { invokeReturn = returnValue }
}, {
/** @type {import('birpc').BirpcReturn<{ invoke: (data: any) => any }>} */
const rpc = createBirpc({}, {
post: (data) => pPort.postMessage(data),
on: (data) => pPort.on('message', data),
})
Expand All @@ -26,7 +22,7 @@ const runner = new ModuleRunner(
{
root: fileURLToPath(new URL('./', import.meta.url)),
transport: {
invoke() { return invokeReturn }
invoke(data) { return rpc.invoke(data) }
},
hmr: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ const createWorkerTransport = (w: Worker): HotChannel => {
describe('running module runner inside a worker and using the ModuleRunnerTransport#invoke API', () => {
let worker: Worker
let server: ViteDevServer
let rpc: BirpcReturn<{ setInvokeReturn: (res: unknown) => void }>
let rpc: BirpcReturn<
unknown,
{ invoke: (data: any) => Promise<{ result: any } | { error: any }> }
>
let handleInvoke: (data: any) => Promise<{ result: any } | { error: any }>

beforeAll(async () => {
worker = new Worker(
Expand Down Expand Up @@ -80,8 +84,11 @@ describe('running module runner inside a worker and using the ModuleRunnerTransp
},
},
})
rpc = createBirpc<{ setInvokeReturn: (res: unknown) => void }>(
{},
handleInvoke = (data: any) => server.environments.ssr.hot.handleInvoke(data)
rpc = createBirpc(
{
invoke: (data: any) => handleInvoke(data),
},
{
post: (data) => worker.postMessage(data),
on: (data) => worker.on('message', data),
Expand All @@ -95,10 +102,7 @@ describe('running module runner inside a worker and using the ModuleRunnerTransp
rpc.$close()
})

async function triggerModuleRunnerInvoke(
invokeReturn: { result: any } | { error: any },
) {
await rpc.setInvokeReturn(invokeReturn)
async function run(id: string) {
const channel = new BroadcastChannel('vite-worker:invoke')
return new Promise<any>((resolve, reject) => {
channel.onmessage = (event) => {
Expand All @@ -108,34 +112,27 @@ describe('running module runner inside a worker and using the ModuleRunnerTransp
reject(e)
}
}
channel.postMessage({ id: 'test' })
channel.postMessage({ id })
})
}

it('correctly runs ssr code', async () => {
const output = await triggerModuleRunnerInvoke({
result: {
code: "__vite_ssr_exports__.default = 'hello invoke world'",
id: '\0virtual:invoke-default-string',
},
})
expect(output).toEqual({
result: 'hello invoke world',
const output = await run('./fixtures/default-string.ts')
expect(output).toStrictEqual({
result: 'hello world',
})
})

it('triggers an error', async () => {
const output = await triggerModuleRunnerInvoke({
error: new Error('This is an Invoke Error'),
})
handleInvoke = async () => ({ error: new Error('This is an Invoke Error') })
const output = await run('dummy')
expect(output).not.toHaveProperty('result')
expect(output.error).toContain('Error: This is an Invoke Error')
})

it('triggers an unknown error', async () => {
const output = await triggerModuleRunnerInvoke({
error: 'a string instead of an error',
})
handleInvoke = async () => ({ error: 'a string instead of an error' })
const output = await run('dummy')
expect(output).not.toHaveProperty('result')
expect(output.error).toContain('Error: Unknown invoke error')
})
Expand Down

0 comments on commit 72ee49b

Please sign in to comment.