-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform.spec.ts
44 lines (38 loc) · 1.83 KB
/
transform.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* @vitest-environment node */
import { describe, it, expect } from 'vitest'
import { WorkerTransformPlugin, WorkerPlugin, VIRTUAL_ID } from '../src/plugins/unplugin.js'
describe('worker transform', () => {
const workerSource = 'export const foo = () => 42; export const bar = async () => "thing"; export const baz = function () { }'
it('should not transform code on the server', async () => {
const code = await transform('server', workerSource)
expect(code).toBeUndefined()
})
it('should transform code on the client', async () => {
const code = await transform('client', workerSource)
expect(code).toMatchFileSnapshot('__snapshots__/worker.client.js')
})
})
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')
})
})
// Helper utils
const context = { workerExports: { foo: 'somefile.ts', bar: 'somefile.ts' }, reverseMap: { 'somefile.ts': ['foo', 'bar'] } }
async function transform(mode: 'server' | 'client', code: string) {
// 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
}
async function load(mode: 'server' | 'client') {
// 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
}