1- import { http } from 'msw/core/http'
2- import { setupWorker } from 'msw/browser'
31import type {
42 IframeChannelEvent ,
53 IframeMockEvent ,
@@ -11,43 +9,6 @@ import { channel } from '../channel'
119export function createModuleMocker ( ) {
1210 const mocks : Map < string , string | null | undefined > = new Map ( )
1311
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-
5112 let started = false
5213 let startPromise : undefined | Promise < unknown >
5314
@@ -58,13 +19,53 @@ export function createModuleMocker() {
5819 if ( startPromise ) {
5920 return startPromise
6021 }
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 ( {
6363 serviceWorker : {
6464 url : '/__vitest_msw__' ,
6565 } ,
6666 quiet : true ,
6767 } )
68+ } )
6869 . finally ( ( ) => {
6970 started = true
7071 startPromise = undefined
0 commit comments