Skip to content

Commit b05e112

Browse files
committed
build(readme): update MSW bypass override instructions under "test" entry in README
1 parent 4dd48e0 commit b05e112

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ testing a handler that expects `/api/user/:id` requires
161161
be passed using the `params` shorthand, e.g. `params: { id: 'test-id', ... }`.
162162
Due to its simplicity, favor the `params` shorthand over `paramsPatcher`. If
163163
both `paramsPatcher` and the `params` shorthand are used, `paramsPatcher` will
164-
receive an object like `{ ...queryStringURLParams, ...params }`.
164+
receive an object like `{ ...queryStringURLParams, ...params }`.
165165

166166
> Route parameters should not be confused with [query string parameters][14],
167167
> which are automatically parsed out from the url and added to the params object
@@ -252,8 +252,16 @@ test.
252252
[`fetch(...)`][8]_, is omitted.**
253253

254254
As of version `3.1.0`, NTARH adds the [`x-msw-bypass: true`][4] header to all
255-
requests by default. You can use `fetch`'s `customInit` parameter to override
256-
this behavior if desired.
255+
requests by default. You can override this behavior by unsetting the header via
256+
`requestPatcher`:
257+
258+
```TypeScript
259+
await testApiHandler({
260+
handler,
261+
requestPatcher: (req) => delete req.headers['x-msw-bypass'], // <==
262+
test: async ({ fetch }) => expect((await fetch()).status).toBe(200)
263+
});
264+
```
257265

258266
##### `response.cookies`
259267

test/unit-index.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('::testApiHandler', () => {
134134
});
135135
});
136136

137-
it('automatically inserts x-msw-bypass when other headers are set through request patcher', async () => {
137+
it('automatically inserts x-msw-bypass when other headers are set via request patcher', async () => {
138138
expect.hasAssertions();
139139

140140
await testApiHandler({
@@ -177,6 +177,34 @@ describe('::testApiHandler', () => {
177177
});
178178
});
179179

180+
it('allows unsetting x-msw-bypass via request patcher', async () => {
181+
expect.hasAssertions();
182+
183+
await testApiHandler({
184+
requestPatcher: (req) => (req.headers['x-msw-bypass'] = undefined),
185+
handler: async (req, res) => {
186+
res.status(200).send({ mswBypass: req.headers['x-msw-bypass'] });
187+
},
188+
test: async ({ fetch }) => {
189+
const res = await fetch();
190+
expect(res.status).toBe(200);
191+
await expect(res.json()).resolves.toStrictEqual({});
192+
}
193+
});
194+
195+
await testApiHandler({
196+
requestPatcher: (req) => delete req.headers['x-msw-bypass'],
197+
handler: async (req, res) => {
198+
res.status(200).send({ mswBypass: req.headers['x-msw-bypass'] });
199+
},
200+
test: async ({ fetch }) => {
201+
const res = await fetch();
202+
expect(res.status).toBe(200);
203+
await expect(res.json()).resolves.toStrictEqual({});
204+
}
205+
});
206+
});
207+
180208
it('respects changes introduced through response patcher', async () => {
181209
expect.hasAssertions();
182210

0 commit comments

Comments
 (0)