Skip to content

Commit 6a8d25d

Browse files
authored
test(fallback-mode): move worker to the test (#2680)
1 parent afa3606 commit 6a8d25d

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { http, HttpResponse } from 'msw'
22
import { setupWorker } from 'msw/browser'
33

4-
const worker = setupWorker(
5-
http.get('*/user', () => {
6-
return HttpResponse.json({ name: 'John Maverick' })
7-
}),
8-
)
9-
10-
worker.start()
11-
12-
Object.assign(window, { worker })
4+
Object.assign(window, {
5+
msw: {
6+
setupWorker,
7+
http,
8+
HttpResponse,
9+
},
10+
})

test/browser/msw-api/setup-worker/fallback-mode/fallback-mode.test.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { SetupWorkerApi } from 'msw/browser'
1+
import type { http, HttpResponse } from 'msw'
2+
import type { setupWorker } from 'msw/browser'
23
import { createTeardown } from 'fs-teardown'
34
import { Page } from '@playwright/test'
45
import { HttpServer } from '@open-draft/test-server/lib/http.js'
56
import { fromTemp } from '../../../../support/utils'
67
import { test, expect } from '../../../playwright.extend'
78

89
declare namespace window {
9-
export const worker: SetupWorkerApi
10+
export const msw: {
11+
setupWorker: typeof setupWorker
12+
http: typeof http
13+
HttpResponse: typeof HttpResponse
14+
}
1015
}
1116

1217
const fsMock = createTeardown({
@@ -94,6 +99,13 @@ test('prints a fallback start message in the console', async ({
9499
}, testInfo) => {
95100
const consoleSpy = spyOnConsole()
96101
await gotoStaticPage(page, testInfo.workerIndex)
102+
103+
await page.evaluate(async () => {
104+
const { setupWorker } = window.msw
105+
const worker = setupWorker()
106+
await worker.start()
107+
})
108+
97109
const consoleGroups = consoleSpy.get('startGroupCollapsed')
98110

99111
await waitFor(() => {
@@ -110,6 +122,17 @@ test('responds with a mocked response to a handled request', async ({
110122
const consoleSpy = spyOnConsole()
111123
await gotoStaticPage(page, testInfo.workerIndex)
112124

125+
await page.evaluate(async () => {
126+
const { setupWorker, http, HttpResponse } = window.msw
127+
128+
const worker = setupWorker(
129+
http.get('*/user', () => {
130+
return HttpResponse.json({ name: 'John Maverick' })
131+
}),
132+
)
133+
await worker.start()
134+
})
135+
113136
const response = await fetch(server.https.url('/user'))
114137

115138
// Prints the request message group in the console.
@@ -139,6 +162,12 @@ test('warns on the unhandled request by default', async ({
139162
const consoleSpy = spyOnConsole()
140163
await gotoStaticPage(page, testInfo.workerIndex)
141164

165+
await page.evaluate(async () => {
166+
const { setupWorker } = window.msw
167+
const worker = setupWorker()
168+
await worker.start()
169+
})
170+
142171
await fetch(server.http.url('/unknown-resource'))
143172

144173
expect(consoleSpy.get('warning')).toEqual(
@@ -162,8 +191,12 @@ test('stops the fallback interceptor when called "worker.stop()"', async ({
162191
const consoleSpy = spyOnConsole()
163192
await gotoStaticPage(page, testInfo.workerIndex)
164193

165-
await page.evaluate(() => {
166-
window.worker.stop()
194+
await page.evaluate(async () => {
195+
const { setupWorker } = window.msw
196+
const worker = setupWorker()
197+
await worker.start()
198+
199+
worker.stop()
167200
})
168201

169202
expect(consoleSpy.get('log')).toContain('[MSW] Mocking disabled.')

0 commit comments

Comments
 (0)