1
- import { http } from 'msw/core/http'
2
- import { setupWorker } from 'msw/browser'
3
1
import type {
4
2
IframeChannelEvent ,
5
3
IframeMockEvent ,
@@ -11,43 +9,6 @@ import { channel } from '../channel'
11
9
export function createModuleMocker ( ) {
12
10
const mocks : Map < string , string | null | undefined > = new Map ( )
13
11
14
- const worker = setupWorker (
15
- http . get ( / .+ / , async ( { request } ) => {
16
- const path = cleanQuery ( request . url . slice ( location . origin . length ) )
17
- if ( ! mocks . has ( path ) ) {
18
- return passthrough ( )
19
- }
20
-
21
- const mock = mocks . get ( path )
22
-
23
- // using a factory
24
- if ( mock === undefined ) {
25
- const exports = await getFactoryExports ( path )
26
- const module = `const module = __vitest_mocker__.get('${ path } ');`
27
- const keys = exports
28
- . map ( ( name ) => {
29
- if ( name === 'default' ) {
30
- return `export default module['default'];`
31
- }
32
- return `export const ${ name } = module['${ name } '];`
33
- } )
34
- . join ( '\n' )
35
- const text = `${ module } \n${ keys } `
36
- return new Response ( text , {
37
- headers : {
38
- 'Content-Type' : 'application/javascript' ,
39
- } ,
40
- } )
41
- }
42
-
43
- if ( typeof mock === 'string' ) {
44
- return Response . redirect ( mock )
45
- }
46
-
47
- return Response . redirect ( injectQuery ( path , 'mock=auto' ) )
48
- } ) ,
49
- )
50
-
51
12
let started = false
52
13
let startPromise : undefined | Promise < unknown >
53
14
@@ -58,13 +19,53 @@ export function createModuleMocker() {
58
19
if ( startPromise ) {
59
20
return startPromise
60
21
}
61
- startPromise = worker
62
- . start ( {
22
+ startPromise = Promise . all ( [
23
+ import ( 'msw/browser' ) ,
24
+ import ( 'msw/core/http' ) ,
25
+ ] ) . then ( ( [ { setupWorker } , { http } ] ) => {
26
+ const worker = setupWorker (
27
+ http . get ( / .+ / , async ( { request } ) => {
28
+ const path = cleanQuery ( request . url . slice ( location . origin . length ) )
29
+ if ( ! mocks . has ( path ) ) {
30
+ return passthrough ( )
31
+ }
32
+
33
+ const mock = mocks . get ( path )
34
+
35
+ // using a factory
36
+ if ( mock === undefined ) {
37
+ const exports = await getFactoryExports ( path )
38
+ const module = `const module = __vitest_mocker__.get('${ path } ');`
39
+ const keys = exports
40
+ . map ( ( name ) => {
41
+ if ( name === 'default' ) {
42
+ return `export default module['default'];`
43
+ }
44
+ return `export const ${ name } = module['${ name } '];`
45
+ } )
46
+ . join ( '\n' )
47
+ const text = `${ module } \n${ keys } `
48
+ return new Response ( text , {
49
+ headers : {
50
+ 'Content-Type' : 'application/javascript' ,
51
+ } ,
52
+ } )
53
+ }
54
+
55
+ if ( typeof mock === 'string' ) {
56
+ return Response . redirect ( mock )
57
+ }
58
+
59
+ return Response . redirect ( injectQuery ( path , 'mock=auto' ) )
60
+ } ) ,
61
+ )
62
+ return worker . start ( {
63
63
serviceWorker : {
64
64
url : '/__vitest_msw__' ,
65
65
} ,
66
66
quiet : true ,
67
67
} )
68
+ } )
68
69
. finally ( ( ) => {
69
70
started = true
70
71
startPromise = undefined
0 commit comments