Unverified Commit 6b58c956 authored by Aria's avatar Aria Committed by GitHub

Update documentation for experimental Playwright test mode + msw v2 (#71841)

Next.js 15 updated `msw` from 1.3.0 to 2.3.0
(https://github.com/vercel/next.js/pull/66351), this PR updates the
example in the experimental Playwright test mode documentation to
reflect this.

Additionally:
- Added catch-all passthrough route to example.
- Fixed issue in the example with how `mswHandlers` was used
(https://playwright.dev/docs/test-fixtures#fixtures-options)

Relevant discussions:

https://github.com/vercel/next.js/issues/71832

https://github.com/vercel/next.js/discussions/71330

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 88238fb5
...@@ -72,30 +72,37 @@ test('/product/shoe', async ({ page, next }) => { ...@@ -72,30 +72,37 @@ test('/product/shoe', async ({ page, next }) => {
### Or use the `next/experimental/testmode/playwright/msw` ### Or use the `next/experimental/testmode/playwright/msw`
```javascript ```javascript
import { test, expect, rest } from 'next/experimental/testmode/playwright/msw' import {
test,
expect,
http,
HttpResponse,
passthrough,
} from 'next/experimental/testmode/playwright/msw'
test.use({ test.use({
mswHandlers: [ mswHandlers: [
rest.get('http://my-db/product/shoe', (req, res, ctx) => { [
return res( http.get('http://my-db/product/shoe', () => {
ctx.status(200), return HttpResponse.json({
ctx.json({
title: 'A shoe', title: 'A shoe',
}) })
) }),
}), // allow all non-mocked routes to pass through
http.all('*', () => {
return passthrough()
}),
],
{ scope: 'test' }, // or 'worker'
], ],
}) })
test('/product/shoe', async ({ page, msw }) => { test('/product/shoe', async ({ page, msw }) => {
msw.use( msw.use(
rest.get('http://my-db/product/boot', (req, res, ctx) => { http.get('http://my-db/product/boot', () => {
return res.once( return HttpResponse.json({
ctx.status(200), title: 'A boot',
ctx.json({ })
title: 'A boot',
})
)
}) })
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment