Skip to content

Commit 006a36a

Browse files
kevvasamcx
authored andcommitted
chore: bump supported msw version to v2.3.0 (#66351)
### What? This bumps the supported `msw` version when using the `playwright` test mode. There's no need for mocking requests and responses anymore since [they adopted](mswjs/msw#1436) the global fetch API in `msw`. ### Why? Users can extend `test` from `playwright` manually and create the `msw` fixtures, but it's better if it works out of the box with newer versions of `msw`. Fixes #63338. Co-authored-by: Sam Ko <[email protected]>
1 parent 9037e93 commit 006a36a

File tree

3 files changed

+173
-188
lines changed

3 files changed

+173
-188
lines changed

packages/next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
"lodash.curry": "4.1.1",
260260
"lru-cache": "5.1.1",
261261
"mini-css-extract-plugin": "2.4.4",
262-
"msw": "1.3.0",
262+
"msw": "2.3.0",
263263
"nanoid": "3.1.32",
264264
"native-url": "0.3.4",
265265
"neo-async": "2.6.1",

packages/next/src/experimental/testmode/playwright/msw.ts

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { test as base, defineConfig } from './index'
22
import type { NextFixture } from './next-fixture'
33
// eslint-disable-next-line import/no-extraneous-dependencies
4-
import {
5-
type RequestHandler,
6-
type MockedResponse,
7-
MockedRequest,
8-
handleRequest,
9-
} from 'msw'
4+
import { type RequestHandler, handleRequest } from 'msw'
105
// eslint-disable-next-line import/no-extraneous-dependencies
116
import { Emitter } from 'strict-event-emitter'
127

@@ -33,39 +28,13 @@ export const test = base.extend<{
3328
const emitter = new Emitter()
3429

3530
next.onFetch(async (request) => {
36-
const {
37-
body,
38-
method,
39-
headers,
40-
credentials,
41-
cache,
42-
redirect,
43-
integrity,
44-
keepalive,
45-
mode,
46-
destination,
47-
referrer,
48-
referrerPolicy,
49-
} = request
50-
const mockedRequest = new MockedRequest(new URL(request.url), {
51-
body: body ? await request.arrayBuffer() : undefined,
52-
method,
53-
headers: Object.fromEntries(headers),
54-
credentials,
55-
cache,
56-
redirect,
57-
integrity,
58-
keepalive,
59-
mode,
60-
destination,
61-
referrer,
62-
referrerPolicy,
63-
})
31+
const requestId = Math.random().toString(16).slice(2)
6432
let isUnhandled = false
6533
let isPassthrough = false
66-
let mockedResponse: MockedResponse | undefined
34+
let mockedResponse
6735
await handleRequest(
68-
mockedRequest,
36+
request.clone(),
37+
requestId,
6938
handlers.slice(0),
7039
{
7140
onUnhandledRequest: () => {
@@ -91,19 +60,7 @@ export const test = base.extend<{
9160
}
9261

9362
if (mockedResponse) {
94-
const {
95-
status,
96-
headers: responseHeaders,
97-
body: responseBody,
98-
delay,
99-
} = mockedResponse
100-
if (delay) {
101-
await new Promise((resolve) => setTimeout(resolve, delay))
102-
}
103-
return new Response(responseBody, {
104-
status,
105-
headers: new Headers(responseHeaders),
106-
})
63+
return mockedResponse
10764
}
10865

10966
return 'abort'

0 commit comments

Comments
 (0)