@@ -3,24 +3,17 @@ import { describe, it, expect } from 'vitest'
33import { WorkerTransformPlugin , WorkerPlugin , VIRTUAL_ID } from '../src/plugins/unplugin.js'
44
55describe ( 'worker transform' , ( ) => {
6+ const workerSource = 'export const foo = () => 42; export const bar = async () => "thing"; export const baz = function () { }'
67 it ( 'should not transform code on the server' , async ( ) => {
7- const code = await transform ( 'server' , 'export const bob = () => 42; export const foo = () => "thing"' )
8+ const code = await transform ( 'server' , workerSource )
89 expect ( code ) . toBeUndefined ( )
910 } )
1011 it ( 'should transform code on the client' , async ( ) => {
11- const code = await transform ( 'client' , 'export const bob = () => 42; export const foo = () => "thing"' )
12+ const code = await transform ( 'client' , workerSource )
1213 expect ( code ) . toMatchFileSnapshot ( '__snapshots__/worker.client.js' )
1314 } )
1415} )
1516
16- async function transform ( mode : 'server' | 'client' , code : string ) {
17- const context = { workerExports : { } , reverseMap : { 'somefile.ts' : [ 'bob' ] } }
18- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19- const plugin = WorkerTransformPlugin ( { mode, context } ) . raw ( { } , { } as any )
20- // eslint-disable-next-line @typescript-eslint/no-explicit-any
21- return ( plugin as any ) . transform ( code , 'somefile.ts' ) ?. code
22- }
23-
2417describe ( 'worker loader' , ( ) => {
2518 it ( 'should load worker on server' , async ( ) => {
2619 const code = await load ( 'server' )
@@ -32,8 +25,18 @@ describe('worker loader', () => {
3225 } )
3326} )
3427
28+ // Helper utils
29+
30+ const context = { workerExports : { foo : 'somefile.ts' , bar : 'somefile.ts' } , reverseMap : { 'somefile.ts' : [ 'foo' , 'bar' ] } }
31+
32+ async function transform ( mode : 'server' | 'client' , code : string ) {
33+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34+ const plugin = WorkerTransformPlugin ( { mode, context } ) . raw ( { } , { } as any )
35+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36+ return ( plugin as any ) . transform ( code , 'somefile.ts' ) ?. code
37+ }
38+
3539async function load ( mode : 'server' | 'client' ) {
36- const context = { workerExports : { bob : 'somefile.ts' } , reverseMap : { 'somefile.ts' : [ 'bob' ] } }
3740 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3841 const plugin = WorkerPlugin ( { mode, context } ) . raw ( { } , { } as any )
3942 // eslint-disable-next-line @typescript-eslint/no-explicit-any
0 commit comments