Skip to content

Commit 28bd0fc

Browse files
committed
Merge branch 'main' into beta
2 parents 46bc300 + 5c52a48 commit 28bd0fc

4 files changed

Lines changed: 83 additions & 10 deletions

File tree

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# `@msw/cloudflare`
2+
3+
Develop and test Cloudflare applications with Mock Service Worker.
4+
5+
## Getting started
6+
7+
### Install
8+
9+
```sh
10+
npm i pkg.pr.new/mswjs/cloudflare/@msw/cloudflare@beta msw
11+
```
12+
13+
> This package requires `msw` as a peer dependency.
14+
15+
### Usage
16+
17+
Let's say your worker performs a GET request to `https://example.com/user`.
18+
19+
```ts
20+
// worker.ts
21+
export default {
22+
async fetch(req, env, ctx) {
23+
const response = await fetch('https://api.example.com/user')
24+
const user = await response.json()
25+
26+
return new Response(user.id, {
27+
headers: { 'content-type': 'text/plain' },
28+
})
29+
},
30+
}
31+
```
32+
33+
Here's how you can intercept and mock that third-party request in order to reliably test your worker in Vitest.
34+
35+
```ts
36+
import { env } from 'cloudflare:workers'
37+
import { createExecutionContext } from 'cloudflare:test'
38+
import { http, HttpResponse } from 'msw'
39+
import { setupNetwork } from '@msw/cloudflare'
40+
import worker from './worker'
41+
42+
const network = setupNetwork()
43+
44+
beforeAll(() => {
45+
network.enable()
46+
})
47+
48+
afterEach(() => {
49+
network.resetHandlers()
50+
})
51+
52+
afterAll(() => {
53+
network.disable()
54+
})
55+
56+
it('', async () => {
57+
network.use(
58+
http.get('https://api.example.com/user', () => {
59+
return HttpResponse.json({ id: 1, name: 'John Maverick' })
60+
}),
61+
)
62+
63+
const ctx = createExecutionContext()
64+
const response = await worker.fetch(
65+
new Request('http://localhost/'),
66+
env,
67+
ctx,
68+
)
69+
70+
expect.soft(response.status).toBe(200)
71+
await expect.soft(response.text()).resolves.toBe('1')
72+
})
73+
```
74+
75+
## Related materials
76+
77+
- [**Write your first test in Cloudflare Docs**](https://developers.cloudflare.com/workers/testing/vitest-integration/write-your-first-test/)
78+
- [Mocking HTTP with MSW](https://mswjs.io/docs/http/)
79+
- [Mocking GraphQL with MSW](https://mswjs.io/docs/graphql/)
80+
- [Mocking WebSocket with MSW](https://mswjs.io/docs/websocket/)

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export function setupNetwork() {
6363
}),
6464
],
6565
handlers: handlersController,
66+
context: {
67+
quiet: true,
68+
},
6669
})
6770

6871
return network

tests/fetch/fetch.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import worker from './worker'
77
const network = setupNetwork()
88

99
beforeAll(() => {
10-
network.configure({
11-
context: {
12-
quiet: true,
13-
},
14-
})
1510
network.enable()
1611
})
1712

tests/websocket/websocket.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import worker from './worker'
88
const network = setupNetwork()
99

1010
beforeAll(() => {
11-
network.configure({
12-
context: {
13-
quiet: true,
14-
},
15-
})
1611
network.enable()
1712
})
1813

0 commit comments

Comments
 (0)