Skip to content

Commit cdc550a

Browse files
committed
03/01: use custom text context for the worker
1 parent 3849bec commit cdc550a

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { test as testBase } from 'vitest'
2+
import { worker } from './src/mocks/browser.js'
3+
4+
interface TestContext {
5+
worker: typeof worker
6+
}
7+
8+
export const test = testBase.extend<TestContext>({
9+
worker: [
10+
async ({}, use) => {
11+
await worker.start({
12+
quiet: true,
13+
onUnhandledRequest(request, print) {
14+
if (/(\.woff2?)$/.test(request.url)) {
15+
return
16+
}
17+
18+
print.error()
19+
},
20+
})
21+
await use(worker)
22+
worker.stop()
23+
},
24+
{ auto: true },
25+
],
26+
})

exercises/03.best-practices/01.solution.network-mocking/src/discount-code-form.browser.test.tsx

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,7 @@ import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
44
import { DiscountCodeForm, type Discount } from './discount-code-form.js'
55

6-
import { worker } from './mocks/browser.js'
7-
8-
beforeAll(async () => {
9-
await worker.start({
10-
quiet: true,
11-
/**
12-
* @fixme @todo Better Vitest integration.
13-
* This has to be defined once for all tests.
14-
* Global setup can use `provide` but runs in Node.js.
15-
* Scoped setup can NOT use `provide` but runs in the browser.
16-
*/
17-
onUnhandledRequest(request, print) {
18-
if (/(\.woff2?)$/.test(request.url)) {
19-
return
20-
}
21-
print.warning()
22-
},
23-
})
24-
})
25-
26-
afterEach(() => {
27-
worker.resetHandlers()
28-
})
29-
30-
afterAll(async () => {
31-
worker.stop()
32-
})
6+
import { test } from '../my-test.js'
337

348
test('applies a discount code', async () => {
359
render(<DiscountCodeForm />)
@@ -47,11 +21,11 @@ test('applies a discount code', async () => {
4721
.toBeVisible()
4822
})
4923

50-
test('displays a warning on legacy discount codes', async () => {
24+
test('displays a warning on legacy discount codes', async ({ worker }) => {
5125
worker.use(
5226
http.post<never, string, Discount>(
5327
'https://api.example.com/discount/code',
54-
async ({ request }) => {
28+
async function override({ request }) {
5529
const code = await request.text()
5630

5731
return HttpResponse.json({
@@ -90,7 +64,9 @@ test('displays a warning on legacy discount codes', async () => {
9064
.toBeVisible()
9165
})
9266

93-
test('displays an error when fetching the discount code', async () => {
67+
test('displays an error when fetching the discount code', async ({
68+
worker,
69+
}) => {
9470
worker.use(
9571
http.post('https://api.example.com/discount/code', () => {
9672
return new HttpResponse(null, { status: 500 })

exercises/03.best-practices/01.solution.network-mocking/tsconfig.node.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"lib": ["ES2023"],
55
"module": "ESNext",
66
"skipLibCheck": true,
7+
"types": ["vitest/globals"],
78

89
/* Bundler mode */
910
"moduleResolution": "bundler",

0 commit comments

Comments
 (0)