Skip to content

Commit 904ca8d

Browse files
committed
refactor(playwright): use @msw/playwright binding
1 parent 45acbac commit 904ca8d

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
22
import { handlers } from './handlers'
33

4-
window.worker = setupWorker(...handlers)
4+
export const worker = setupWorker(...handlers)

examples/with-playwright/app/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
<script>
88
globalThis.process = { env: {} }
99
</script>
10-
<script type="module" src="/browser.js"></script>
1110
<script type="module">
12-
await window.worker.start()
11+
if (process.env.NODE_ENV === 'development') {
12+
const { worker } = await import('./browser.js')
13+
await worker.start()
14+
}
1315

1416
async function main() {
1517
fetch('/user')

examples/with-playwright/playwright.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ export default defineConfig({
1111
devtools: true,
1212
},
1313
},
14+
testDir: 'tests',
15+
projects: [
16+
// Setup file following https://playwright.dev/docs/test-global-setup-teardown
17+
{
18+
name: 'setup',
19+
testMatch: 'playwright.setup.ts',
20+
},
21+
{
22+
name: 'tests',
23+
dependencies: ['setup'],
24+
},
25+
],
1426
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test as testBase } from '@playwright/test'
2+
import { createNetworkFixture, type NetworkFixture } from '@msw/playwright'
3+
import { handlers } from './app/handlers.js'
4+
5+
interface Fixtures {
6+
network: NetworkFixture
7+
}
8+
9+
// Set up `test` with the msw playwright binding following https://github.com/mswjs/playwright
10+
export const test = testBase.extend<Fixtures>({
11+
// Create a fixture that will control the network in your tests.
12+
network: createNetworkFixture({
13+
initialHandlers: handlers,
14+
}),
15+
})

examples/with-playwright/example.test.ts renamed to examples/with-playwright/tests/example.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test, expect } from '@playwright/test'
1+
import { expect } from '@playwright/test'
2+
import { test } from '../playwright.setup.js'
23

34
test('receives a mocked response to a REST API request', async ({ page }) => {
45
await page.goto('/', { waitUntil: 'networkidle' })

0 commit comments

Comments
 (0)