Skip to content

Commit e140d4d

Browse files
committed
03/03: add missing worker.resetHandlers() to msw setup
1 parent 2bf9345 commit e140d4d

File tree

10 files changed

+48
-12
lines changed

10 files changed

+48
-12
lines changed

exercises/03.best-practices/03.problem.network-mocking/README.mdx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Right now, the `worker` property has an array of two elements: `fixture` and `op
116116

117117
🐨 Provide the following fixture for the `worker`:
118118

119-
```ts filename=test-extend.ts add=2,6-13
119+
```ts filename=test-extend.ts add=2,6-14
120120
import { test as testBase } from 'vitest'
121121
import { worker } from './src/mocks/browser.js'
122122

@@ -128,16 +128,46 @@ export const test = testBase.extend({
128128
onUnhandledRequest: 'error',
129129
})
130130
await use(worker)
131+
worker.resetHandlers()
131132
worker.stop()
132133
},
133134
options,
134135
],
135136
})
136137
```
137138

138-
In this fixture, you are staring the MSW'w worker by calling `await worker.start()`. Here, you're providing `quiet: true` to disable MSW logging intercepted requests in the console during the test run to have a less noisy output, and also the `onUnhandledRequest: 'error'` option so the library errors if there are any requests in tests that you haven't handled.
139+
Let's unwrap individual pieces of this fixture:
139140

140-
Then, you expose that `worker` reference so it can be accessed in tests (the `await use(worker)` part). And, finally, `worker.stop()` stops the mocking after the test has finished.
141+
```ts nonumber
142+
await worker.start({
143+
quiet: true,
144+
onUnhandledRequest: 'error',
145+
})
146+
```
147+
148+
Here, you are starting the worker that enables request mocking in MSW. You are referencing the same `worker` object you've configured previously in `src/mocks/browser.js`.
149+
150+
```ts nonumber
151+
await use(worker)
152+
```
153+
154+
This line exposes the `worker` object as a fixture in your tests. It makes it possible for you to reference the same worker object across different tests like so:
155+
156+
```ts nonumber
157+
test('...', ({ worker }) => {})
158+
```
159+
160+
```ts nonumber
161+
worker.resetHandlers()
162+
```
163+
164+
This line makes sure that any mocks you add in individual tests are _reset_ between them. This achieves scope isolation and prevents network overrides from leaking to irrelevant tests.
165+
166+
```ts
167+
worker.stop()
168+
```
169+
170+
And, finally, you're stopping the worker, effectively turning off the request interception.
141171

142172
🐨 To help Vitest understand the types of this `worker` fixture, annotate it explicitly by creating a new type called `TestContext`, describing the worker there, and providing it as a type argument to `test.extend()`:
143173

@@ -165,7 +195,7 @@ You want MSW up and running _in every test_ so you can take advantage of its req
165195
166196
🐨 To do that, configure the `worker` fixture to have the `auto: true` option:
167197
168-
```ts filename=test-extend.ts add=18
198+
```ts filename=test-extend.ts add=19
169199
import { test as testBase } from 'vitest'
170200
import { worker } from './src/mocks/browser.js'
171201

@@ -181,6 +211,7 @@ export const test = testBase.extend<TestContext>({
181211
onUnhandledRequest: 'error',
182212
})
183213
await use(worker)
214+
worker.resetHandlers()
184215
worker.stop()
185216
},
186217
{ auto: true },

exercises/03.best-practices/03.solution.network-mocking/test-extend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const test = testBase.extend<TestContext>({
1313
onUnhandledRequest: 'error',
1414
})
1515
await use(worker)
16+
worker.resetHandlers()
1617
worker.stop()
1718
},
1819
{ auto: true },

exercises/03.best-practices/04.problem.element-presence/src/discount-code-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export function DiscountCodeForm() {
155155
/>
156156
<button
157157
aria-label="Remove discount"
158-
className="size-8 rounded-md border text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
158+
className="size-8 rounded-md border border-gray-300 text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
159159
>
160-
160+
161161
</button>
162162
</form>
163163
) : (

exercises/03.best-practices/04.problem.element-presence/test-extend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const test = testBase.extend<TestContext>({
1313
onUnhandledRequest: 'error',
1414
})
1515
await use(worker)
16+
worker.resetHandlers()
1617
worker.stop()
1718
},
1819
{ auto: true },

exercises/03.best-practices/04.solution.element-presence/src/discount-code-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export function DiscountCodeForm() {
155155
/>
156156
<button
157157
aria-label="Remove discount"
158-
className="size-8 rounded-md border text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
158+
className="size-8 rounded-md border border-gray-300 text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
159159
>
160-
160+
161161
</button>
162162
</form>
163163
) : (

exercises/03.best-practices/04.solution.element-presence/test-extend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const test = testBase.extend<TestContext>({
1313
onUnhandledRequest: 'error',
1414
})
1515
await use(worker)
16+
worker.resetHandlers()
1617
worker.stop()
1718
},
1819
{ auto: true },

exercises/03.best-practices/05.problem.page-navigation/src/discount-code-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export function DiscountCodeForm() {
155155
/>
156156
<button
157157
aria-label="Remove discount"
158-
className="size-8 rounded-md border text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
158+
className="size-8 rounded-md border border-gray-300 text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
159159
>
160-
160+
161161
</button>
162162
</form>
163163
) : (

exercises/03.best-practices/05.problem.page-navigation/test-extend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const test = testBase.extend<TestContext>({
1313
onUnhandledRequest: 'error',
1414
})
1515
await use(worker)
16+
worker.resetHandlers()
1617
worker.stop()
1718
},
1819
{ auto: true },

exercises/03.best-practices/05.solution.page-navigation/src/discount-code-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export function DiscountCodeForm() {
155155
/>
156156
<button
157157
aria-label="Remove discount"
158-
className="size-8 rounded-md border text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
158+
className="size-8 rounded-md border border-gray-300 text-slate-500 hover:border-slate-300 hover:bg-slate-100 hover:text-slate-700 focus:ring-4"
159159
>
160-
160+
161161
</button>
162162
</form>
163163
) : (

exercises/03.best-practices/05.solution.page-navigation/test-extend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const test = testBase.extend<TestContext>({
1313
onUnhandledRequest: 'error',
1414
})
1515
await use(worker)
16+
worker.resetHandlers()
1617
worker.stop()
1718
},
1819
{ auto: true },

0 commit comments

Comments
 (0)