From 1ae1e23d44c32ea6747cdedaf6594e5c868016ec Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 19 Jul 2024 17:45:30 +0100 Subject: [PATCH] test: add some snapshots of code generation --- eslint.config.js | 2 ++ test/__snapshots__/auto-import.client.js | 30 +++++++++++++++++ test/__snapshots__/auto-import.server.js | 11 +++++++ test/__snapshots__/worker.client.js | 13 ++++++++ test/transform.spec.ts | 41 ++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 test/__snapshots__/auto-import.client.js create mode 100644 test/__snapshots__/auto-import.server.js create mode 100644 test/__snapshots__/worker.client.js create mode 100644 test/transform.spec.ts diff --git a/eslint.config.js b/eslint.config.js index db7232e..a47ec82 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -11,4 +11,6 @@ export default createConfigForNuxt({ './playground', ], }, +}).prepend({ + ignores: ['test/__snapshots__/**'], }) diff --git a/test/__snapshots__/auto-import.client.js b/test/__snapshots__/auto-import.client.js new file mode 100644 index 0000000..deace03 --- /dev/null +++ b/test/__snapshots__/auto-import.client.js @@ -0,0 +1,30 @@ + +const counts = {} +const map = {} + +function initWorker (worker, name) { + map[name] = {} + counts[name] = 0 + worker.onmessage = (e) => { + const [resolve, reject] = map[name][e.data.id] + if ('error' in e.data) { + reject(new Error(e.data.error)) + } else { + resolve(e.data.result) + } + } + return worker +} +var _nuxt_worker_bob; +async function bob (...args) { + + _nuxt_worker_bob ||= initWorker(new Worker(new URL("somefile.ts", import.meta.url)), "bob") + + const id = counts["bob"]++ + return new Promise((resolve, reject) => { + map["bob"][id] = [resolve, reject] + _nuxt_worker_bob.postMessage({ name: "bob", args, id }) + }) +} + +export { bob } diff --git a/test/__snapshots__/auto-import.server.js b/test/__snapshots__/auto-import.server.js new file mode 100644 index 0000000..39b092f --- /dev/null +++ b/test/__snapshots__/auto-import.server.js @@ -0,0 +1,11 @@ + +var _nuxt_worker_bob; +async function bob (...args) { + + const { bob: fn } = await import("somefile.ts") + + return fn(...args) + +} + +export { bob } diff --git a/test/__snapshots__/worker.client.js b/test/__snapshots__/worker.client.js new file mode 100644 index 0000000..04b1664 --- /dev/null +++ b/test/__snapshots__/worker.client.js @@ -0,0 +1,13 @@ +const bob = () => 42; const foo = () => "thing" +const __worker_exports__ = { bob: bob } +self.onmessage = async (e) => { + const { name, args, id } = e.data + const fn = __worker_exports__[name] + if (!fn) { throw new Error('Worker function not found: ' + name) } + try { + const result = await fn(...args) + self.postMessage({ result, id }) + } catch (e) { + self.postMessage({ error: e.message, id }) + } +} \ No newline at end of file diff --git a/test/transform.spec.ts b/test/transform.spec.ts new file mode 100644 index 0000000..c64b5f3 --- /dev/null +++ b/test/transform.spec.ts @@ -0,0 +1,41 @@ +/* @vitest-environment node */ +import { describe, it, expect } from 'vitest' +import { WorkerTransformPlugin, WorkerPlugin, VIRTUAL_ID } from '../src/plugins/unplugin.js' + +describe('worker transform', () => { + it('should not transform code on the server', async () => { + const code = await transform('server', 'export const bob = () => 42; export const foo = () => "thing"') + expect(code).toBeUndefined() + }) + it('should transform code on the client', async () => { + const code = await transform('client', 'export const bob = () => 42; export const foo = () => "thing"') + expect(code).toMatchFileSnapshot('__snapshots__/worker.client.js') + }) +}) + +async function transform(mode: 'server' | 'client', code: string) { + const context = { workerExports: {}, reverseMap: { 'somefile.ts': ['bob'] } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const plugin = WorkerTransformPlugin({ mode, context }).raw({}, {} as any) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return (plugin as any).transform(code, 'somefile.ts')?.code +} + +describe('worker loader', () => { + it('should load worker on server', async () => { + const code = await load('server') + expect(code).toMatchFileSnapshot('__snapshots__/auto-import.server.js') + }) + it('should load worker on client', async () => { + const code = await load('client') + expect(code).toMatchFileSnapshot('__snapshots__/auto-import.client.js') + }) +}) + +async function load(mode: 'server' | 'client') { + const context = { workerExports: { bob: 'somefile.ts' }, reverseMap: { 'somefile.ts': ['bob'] } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const plugin = WorkerPlugin({ mode, context }).raw({}, {} as any) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return (plugin as any).load(VIRTUAL_ID + `?source=somefile.ts`)?.code +} -- 2.54.0