Skip to content

Commit cd2ae1b

Browse files
authored
update msw to 2.6.4 (#125)
1 parent 76f1686 commit cd2ae1b

File tree

18 files changed

+346
-349
lines changed

18 files changed

+346
-349
lines changed

examples/with-angular/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212

1313
1. Create [`src/mocks/browser.ts`](./src/mocks/browser.ts) and configure the `worker` instance.
1414
1. In [`src/main.ts`](./src/main.ts), start the worker instance. Defer your application's rendering by implementing a `prepareApp()` function.
15+
16+
## Tests with Jest
17+
18+
If you are using Jest, make sure to provide [`jest-fixed-jsdom`](https://github.com/mswjs/jest-fixed-jsdom) as the custom `testEnvironment` in your [`jest.config.js`](./jest.config.js) file.

examples/with-angular/jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
preset: 'jest-preset-angular',
3+
testEnvironment: 'jest-fixed-jsdom',
34
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
45
globalSetup: 'jest-preset-angular/global-setup',
56
testEnvironmentOptions: {
@@ -9,6 +10,6 @@ module.exports = {
910
Request,
1011
Response,
1112
TextEncoder,
12-
TextDecoder
13+
TextDecoder,
1314
},
1415
}

examples/with-angular/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"cypress": "^12.11.0",
3737
"jest": "^29.5.0",
3838
"jest-environment-jsdom": "^29.5.0",
39+
"jest-fixed-jsdom": "^0.0.9",
3940
"jest-preset-angular": "13.0.1",
40-
"msw": "2.2.2",
41+
"msw": "2.6.4",
4142
"start-server-and-test": "^2.0.0",
4243
"tslib": "2.3.0",
4344
"typescript": "~4.9"

examples/with-angular/src/mockServiceWorker.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
/* tslint:disable */
33

44
/**
5-
* Mock Service Worker (2.2.2).
5+
* Mock Service Worker.
66
* @see https://github.com/mswjs/msw
77
* - Please do NOT modify this file.
88
* - Please do NOT serve this file on production.
99
*/
1010

11-
const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911'
11+
const PACKAGE_VERSION = '2.6.4'
12+
const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074'
1213
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
1314
const activeClientIds = new Set()
1415

@@ -48,7 +49,10 @@ self.addEventListener('message', async function (event) {
4849
case 'INTEGRITY_CHECK_REQUEST': {
4950
sendToClient(client, {
5051
type: 'INTEGRITY_CHECK_RESPONSE',
51-
payload: INTEGRITY_CHECKSUM,
52+
payload: {
53+
packageVersion: PACKAGE_VERSION,
54+
checksum: INTEGRITY_CHECKSUM,
55+
},
5256
})
5357
break
5458
}
@@ -58,7 +62,12 @@ self.addEventListener('message', async function (event) {
5862

5963
sendToClient(client, {
6064
type: 'MOCKING_ENABLED',
61-
payload: true,
65+
payload: {
66+
client: {
67+
id: client.id,
68+
frameType: client.frameType,
69+
},
70+
},
6271
})
6372
break
6473
}
@@ -151,6 +160,10 @@ async function handleRequest(event, requestId) {
151160
async function resolveMainClient(event) {
152161
const client = await self.clients.get(event.clientId)
153162

163+
if (activeClientIds.has(event.clientId)) {
164+
return client
165+
}
166+
154167
if (client?.frameType === 'top-level') {
155168
return client
156169
}
@@ -179,12 +192,14 @@ async function getResponse(event, client, requestId) {
179192
const requestClone = request.clone()
180193

181194
function passthrough() {
182-
const headers = Object.fromEntries(requestClone.headers.entries())
195+
// Cast the request headers to a new Headers instance
196+
// so the headers can be manipulated with.
197+
const headers = new Headers(requestClone.headers)
183198

184-
// Remove internal MSW request header so the passthrough request
185-
// complies with any potential CORS preflight checks on the server.
186-
// Some servers forbid unknown request headers.
187-
delete headers['x-msw-intention']
199+
// Remove the "accept" header value that marked this request as passthrough.
200+
// This prevents request alteration and also keeps it compliant with the
201+
// user-defined CORS policies.
202+
headers.delete('accept', 'msw/passthrough')
188203

189204
return fetch(requestClone, { headers })
190205
}
@@ -202,13 +217,6 @@ async function getResponse(event, client, requestId) {
202217
return passthrough()
203218
}
204219

205-
// Bypass requests with the explicit bypass header.
206-
// Such requests can be issued by "ctx.fetch()".
207-
const mswIntention = request.headers.get('x-msw-intention')
208-
if (['bypass', 'passthrough'].includes(mswIntention)) {
209-
return passthrough()
210-
}
211-
212220
// Notify the client that a request has been intercepted.
213221
const requestBuffer = await request.arrayBuffer()
214222
const clientMessage = await sendToClient(
@@ -240,7 +248,7 @@ async function getResponse(event, client, requestId) {
240248
return respondWithMock(clientMessage.data)
241249
}
242250

243-
case 'MOCK_NOT_FOUND': {
251+
case 'PASSTHROUGH': {
244252
return passthrough()
245253
}
246254
}

examples/with-jest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@types/jest": "^29.5.1",
1010
"@types/node": "^18",
1111
"jest": "^29.5.0",
12-
"msw": "2.2.2",
12+
"msw": "2.6.4",
1313
"ts-node": "^10.9.2",
1414
"typescript": "^5.0.4",
1515
"with-angular": "workspace:*",

examples/with-karma/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"karma-mocha-reporter": "^2.2.5",
1313
"karma-webpack": "^5.0.0",
1414
"mocha": "^10.2.0",
15-
"msw": "2.2.2",
15+
"msw": "2.6.4",
1616
"webpack": "^5.81.0"
1717
},
1818
"msw": {

examples/with-karma/test/mockServiceWorker.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
/* tslint:disable */
33

44
/**
5-
* Mock Service Worker (2.2.2).
5+
* Mock Service Worker.
66
* @see https://github.com/mswjs/msw
77
* - Please do NOT modify this file.
88
* - Please do NOT serve this file on production.
99
*/
1010

11-
const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911'
11+
const PACKAGE_VERSION = '2.6.4'
12+
const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074'
1213
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
1314
const activeClientIds = new Set()
1415

@@ -48,7 +49,10 @@ self.addEventListener('message', async function (event) {
4849
case 'INTEGRITY_CHECK_REQUEST': {
4950
sendToClient(client, {
5051
type: 'INTEGRITY_CHECK_RESPONSE',
51-
payload: INTEGRITY_CHECKSUM,
52+
payload: {
53+
packageVersion: PACKAGE_VERSION,
54+
checksum: INTEGRITY_CHECKSUM,
55+
},
5256
})
5357
break
5458
}
@@ -58,7 +62,12 @@ self.addEventListener('message', async function (event) {
5862

5963
sendToClient(client, {
6064
type: 'MOCKING_ENABLED',
61-
payload: true,
65+
payload: {
66+
client: {
67+
id: client.id,
68+
frameType: client.frameType,
69+
},
70+
},
6271
})
6372
break
6473
}
@@ -151,6 +160,10 @@ async function handleRequest(event, requestId) {
151160
async function resolveMainClient(event) {
152161
const client = await self.clients.get(event.clientId)
153162

163+
if (activeClientIds.has(event.clientId)) {
164+
return client
165+
}
166+
154167
if (client?.frameType === 'top-level') {
155168
return client
156169
}
@@ -179,12 +192,14 @@ async function getResponse(event, client, requestId) {
179192
const requestClone = request.clone()
180193

181194
function passthrough() {
182-
const headers = Object.fromEntries(requestClone.headers.entries())
195+
// Cast the request headers to a new Headers instance
196+
// so the headers can be manipulated with.
197+
const headers = new Headers(requestClone.headers)
183198

184-
// Remove internal MSW request header so the passthrough request
185-
// complies with any potential CORS preflight checks on the server.
186-
// Some servers forbid unknown request headers.
187-
delete headers['x-msw-intention']
199+
// Remove the "accept" header value that marked this request as passthrough.
200+
// This prevents request alteration and also keeps it compliant with the
201+
// user-defined CORS policies.
202+
headers.delete('accept', 'msw/passthrough')
188203

189204
return fetch(requestClone, { headers })
190205
}
@@ -202,13 +217,6 @@ async function getResponse(event, client, requestId) {
202217
return passthrough()
203218
}
204219

205-
// Bypass requests with the explicit bypass header.
206-
// Such requests can be issued by "ctx.fetch()".
207-
const mswIntention = request.headers.get('x-msw-intention')
208-
if (['bypass', 'passthrough'].includes(mswIntention)) {
209-
return passthrough()
210-
}
211-
212220
// Notify the client that a request has been intercepted.
213221
const requestBuffer = await request.arrayBuffer()
214222
const clientMessage = await sendToClient(
@@ -240,7 +248,7 @@ async function getResponse(event, client, requestId) {
240248
return respondWithMock(clientMessage.data)
241249
}
242250

243-
case 'MOCK_NOT_FOUND': {
251+
case 'PASSTHROUGH': {
244252
return passthrough()
245253
}
246254
}

examples/with-playwright/app/mockServiceWorker.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
/* tslint:disable */
33

44
/**
5-
* Mock Service Worker (2.2.2).
5+
* Mock Service Worker.
66
* @see https://github.com/mswjs/msw
77
* - Please do NOT modify this file.
88
* - Please do NOT serve this file on production.
99
*/
1010

11-
const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911'
11+
const PACKAGE_VERSION = '2.6.4'
12+
const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074'
1213
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
1314
const activeClientIds = new Set()
1415

@@ -48,7 +49,10 @@ self.addEventListener('message', async function (event) {
4849
case 'INTEGRITY_CHECK_REQUEST': {
4950
sendToClient(client, {
5051
type: 'INTEGRITY_CHECK_RESPONSE',
51-
payload: INTEGRITY_CHECKSUM,
52+
payload: {
53+
packageVersion: PACKAGE_VERSION,
54+
checksum: INTEGRITY_CHECKSUM,
55+
},
5256
})
5357
break
5458
}
@@ -58,7 +62,12 @@ self.addEventListener('message', async function (event) {
5862

5963
sendToClient(client, {
6064
type: 'MOCKING_ENABLED',
61-
payload: true,
65+
payload: {
66+
client: {
67+
id: client.id,
68+
frameType: client.frameType,
69+
},
70+
},
6271
})
6372
break
6473
}
@@ -151,6 +160,10 @@ async function handleRequest(event, requestId) {
151160
async function resolveMainClient(event) {
152161
const client = await self.clients.get(event.clientId)
153162

163+
if (activeClientIds.has(event.clientId)) {
164+
return client
165+
}
166+
154167
if (client?.frameType === 'top-level') {
155168
return client
156169
}
@@ -179,12 +192,14 @@ async function getResponse(event, client, requestId) {
179192
const requestClone = request.clone()
180193

181194
function passthrough() {
182-
const headers = Object.fromEntries(requestClone.headers.entries())
195+
// Cast the request headers to a new Headers instance
196+
// so the headers can be manipulated with.
197+
const headers = new Headers(requestClone.headers)
183198

184-
// Remove internal MSW request header so the passthrough request
185-
// complies with any potential CORS preflight checks on the server.
186-
// Some servers forbid unknown request headers.
187-
delete headers['x-msw-intention']
199+
// Remove the "accept" header value that marked this request as passthrough.
200+
// This prevents request alteration and also keeps it compliant with the
201+
// user-defined CORS policies.
202+
headers.delete('accept', 'msw/passthrough')
188203

189204
return fetch(requestClone, { headers })
190205
}
@@ -202,13 +217,6 @@ async function getResponse(event, client, requestId) {
202217
return passthrough()
203218
}
204219

205-
// Bypass requests with the explicit bypass header.
206-
// Such requests can be issued by "ctx.fetch()".
207-
const mswIntention = request.headers.get('x-msw-intention')
208-
if (['bypass', 'passthrough'].includes(mswIntention)) {
209-
return passthrough()
210-
}
211-
212220
// Notify the client that a request has been intercepted.
213221
const requestBuffer = await request.arrayBuffer()
214222
const clientMessage = await sendToClient(
@@ -240,7 +248,7 @@ async function getResponse(event, client, requestId) {
240248
return respondWithMock(clientMessage.data)
241249
}
242250

243-
case 'MOCK_NOT_FOUND': {
251+
case 'PASSTHROUGH': {
244252
return passthrough()
245253
}
246254
}

examples/with-playwright/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"devDependencies": {
99
"@playwright/test": "^1.40.1",
1010
"@web/dev-server": "^0.2.1",
11-
"msw": "2.2.2",
11+
"msw": "2.6.4",
1212
"playwright": "^1.33.0",
1313
"typescript": "^5.0.4"
1414
},

examples/with-remix/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@types/react": "^18.0.35",
2727
"@types/react-dom": "^18.0.11",
2828
"eslint": "^8.38.0",
29-
"msw": "2.2.2",
29+
"msw": "2.6.4",
3030
"playwright": "^1.33.0",
3131
"typescript": "^4.9.5"
3232
},

0 commit comments

Comments
 (0)