Skip to content

Commit a80b6a3

Browse files
committed
refactor(playwright): use @msw/playwright binding
Add @msw/playwright to the playwright example. Update Playwright to avoid TypeError: this[#page].routeWebSocket is not a function errors when using @msw/playwright. Use the `@msw/playwright` binding
1 parent 6da9367 commit a80b6a3

File tree

8 files changed

+83
-50
lines changed

8 files changed

+83
-50
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/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.utils.js'
23

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

examples/with-playwright/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"postinstall": "pnpm exec playwright install"
77
},
88
"devDependencies": {
9-
"@playwright/test": "^1.40.1",
9+
"@msw/playwright": "^0.4.2",
10+
"@playwright/test": "^1.55.0",
1011
"@web/dev-server": "^0.2.1",
1112
"msw": "2.11.2",
12-
"playwright": "^1.33.0",
13+
"playwright": "^1.55.0",
1314
"typescript": "^5.0.4"
1415
},
1516
"msw": {
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-remix/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
"react-dom": "^18.2.0"
2121
},
2222
"devDependencies": {
23-
"@playwright/test": "^1.33.0",
23+
"@playwright/test": "^1.55.0",
2424
"@remix-run/dev": "^1.15.0",
2525
"@remix-run/eslint-config": "^1.15.0",
2626
"@types/react": "^18.0.35",
2727
"@types/react-dom": "^18.0.11",
2828
"eslint": "^8.38.0",
2929
"msw": "2.11.2",
30-
"playwright": "^1.33.0",
30+
"playwright": "^1.55.0",
3131
"typescript": "^4.9.5"
3232
},
3333
"engines": {

examples/with-svelte/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"postinstall": "pnpm exec playwright install"
1616
},
1717
"devDependencies": {
18-
"@playwright/test": "^1.33.0",
18+
"@playwright/test": "^1.55.0",
1919
"@sveltejs/adapter-auto": "^2.0.1",
2020
"@sveltejs/kit": "^1.16.2",
2121
"@typescript-eslint/eslint-plugin": "^5.59.2",
@@ -24,7 +24,7 @@
2424
"eslint-config-prettier": "^8.8.0",
2525
"eslint-plugin-svelte": "^2.27.4",
2626
"msw": "2.11.2",
27-
"playwright": "^1.33.0",
27+
"playwright": "^1.55.0",
2828
"prettier": "^2.8.8",
2929
"prettier-plugin-svelte": "^2.10.0",
3030
"svelte": "^3.59.0",

pnpm-lock.yaml

Lines changed: 54 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)