MCPcopy
hub / github.com/mswjs/msw / loadExample

Function loadExample

test/browser/playwright.extend.ts:94–142  ·  view source on GitHub ↗
({ page, webpackServer, waitForMswActivation }, use)

Source from the content-addressed store, hash-verified

92 use(await getWebpackServer())
93 },
94 async loadExample({ page, webpackServer, waitForMswActivation }, use) {
95 const pageExceptions: Array<Error> = []
96 page.on('pageerror', (error) => pageExceptions.push(error))
97
98 const workerConsole = new WorkerConsole()
99 let compilation: Compilation | undefined
100
101 await use(async (entry, options = {}) => {
102 const resolvedEntry =
103 entry instanceof URL ? url.fileURLToPath(entry) : entry
104
105 compilation = await webpackServer.compile(
106 Array.prototype.concat([], resolvedEntry),
107 options,
108 )
109
110 // Allow arbitrary setup code before navigating to the compilation preview.
111 // This is useful to set up console watchers and other side-effects.
112 options.beforeNavigation?.(compilation)
113
114 // Forward browser runtime errors/warnings to the test runner.
115 page.on('pageerror', console.error)
116
117 const oncePageReady = [
118 page.waitForLoadState('domcontentloaded', { timeout: 15_000 }),
119 page.waitForEvent('load', { timeout: 30_000 }),
120 page.waitForLoadState('networkidle', { timeout: 5_000 }),
121 ]
122 page.goto(compilation.previewUrl)
123 await Promise.all(oncePageReady)
124
125 // All examples await the MSW activation message by default.
126 // Support opting-out from this behavior for tests where activation
127 // is not expected (e.g. when testing activation errors).
128 if (!options.skipActivation) {
129 await waitForMswActivation()
130 }
131
132 await workerConsole.init(page)
133
134 return {
135 compilation,
136 workerConsole,
137 }
138 })
139
140 workerConsole.removeAllListeners()
141 await compilation?.dispose()
142 },
143 async waitFor({}, use) {
144 await use(waitFor)
145 },

Callers 15

query.test.tsFile · 0.85
link.test.tsFile · 0.85
cookies.test.tsFile · 0.85
variables.test.tsFile · 0.85
logging.test.tsFile · 0.85
mutation.test.tsFile · 0.85
errors.test.tsFile · 0.85

Calls 5

initMethod · 0.95
removeAllListenersMethod · 0.95
waitForMswActivationFunction · 0.85
disposeMethod · 0.80
useFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…