Skip to content

Commit 402b647

Browse files
committed
doc: update doc
1 parent 90477ec commit 402b647

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

README.md

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# nuxt-ms
1+
# nuxt-msw
22
<!-- [![npm version][npm-version-src]][npm-version-href]
33
[![npm downloads][npm-downloads-src]][npm-downloads-href]
44
[![License][license-src]][license-href]
@@ -11,14 +11,42 @@
1111
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/nuxt-msw?file=playground%2Fapp.vue) -->
1212
<!-- - [📖 &nbsp;Documentation](https://example.com) -->
1313

14-
## Features
14+
- [nuxt-msw](#nuxt-msw)
15+
- [Features](#features)
16+
- [Migrate: `0.x` -\> `1.x` users](#migrate-0x---1x-users)
17+
- [Setup](#setup)
18+
- [Usage](#usage)
19+
- [worker](#worker)
20+
- [server](#server)
21+
- [Module options](#module-options)
22+
- [How to Use in Tests](#how-to-use-in-tests)
23+
- [vitest](#vitest)
24+
- [`@nuxt/test-utils`](#nuxttest-utils)
25+
- [Unit Tests](#unit-tests)
26+
- [Note: `baseURL` in unit test](#note-baseurl-in-unit-test)
27+
- [E2E Tests](#e2e-tests)
28+
- [Note: `$fetch` and `fetch` in `@nuxt/test-utils/e2e`](#note-fetch-and-fetch-in-nuxttest-utilse2e)
29+
- [Nuxt Layer](#nuxt-layer)
30+
- [nuxt layer and unit test](#nuxt-layer-and-unit-test)
31+
- [Contribution](#contribution)
32+
1533

34+
## Features
1635
<!-- Highlight some of the features your module provide here -->
1736
- 🌲 Write once, use everywhere
1837
- 🚀 Use MSW powerful mocking features in Nuxt development
1938
- ⛰ Intercept both server-side and client-side request, including `$fetch`, `useFetch` and any other api requests.
2039
- 🥧 Support Nuxt layer.
2140

41+
## Migrate: `0.x` -> `1.x` users
42+
Thank you to all the users of version 0.x. I believe you won’t need to put in too much effort to upgrade to version 1.x.
43+
44+
The differences between 0.x and 1.x are as follows:
45+
46+
optionPath in nuxt.config has been changed to folderPath. Originally, it pointed to a file, but now it points to a folder.
47+
The settings for worker and server were previously written in the same file. To better utilize the environments they run in, worker and server will now run in separate files. Please refer to the instructions below.
48+
Added support for unit testing in the Nuxt environment.
49+
Added support for Nuxt layers.
2250

2351
## Setup
2452
To install the module to your Nuxt application:
@@ -43,6 +71,11 @@ The setup location is in the `~/msw` directory (by default), and you configure i
4371
### worker
4472
To set up the worker, you need follow the [MSW documentation](https://mswjs.io/docs/integrations/browser#generating-the-worker-script) to create a worker file.
4573

74+
```sh
75+
# For example
76+
npx msw init public --save
77+
```
78+
4679
Next, you need to create a `worker.{ts|js|mjs|cjs}` file in the `~/msw` directory. The worker file will be run in the Nuxt client plugin, which in browser context. Means you can use browser api and Nuxt composable in this file.
4780

4881
```ts
@@ -72,9 +105,10 @@ export default defineNuxtMswWorkerOption(() => {
72105
// Module will setup worker when nuxt run client plugin
73106
// Means this function will be called after plugin call worker.start()
74107

75-
// nuxtApp.hook('app:mounted', () => {
76-
// console.log(worker.listHandlers())
77-
// })
108+
nuxtApp.hook('app:mounted', () => {
109+
// const route = useRoute()
110+
// console.log(worker.listHandlers())
111+
})
78112
},
79113

80114
}
@@ -100,14 +134,18 @@ The way to set up the server is similar to the worker. You need to create a `ser
100134

101135
The server file will be run in Node.js `Nitro` context. Because it is before NuxtApp created, you can not access NuxtApp and composable which access it. But you can access msw server and request (h3Event).
102136

103-
One more important thing is that you need to set `baseURL` in the server option if you are using `$fetch` or `useFetch` with no `baseURL` set, and use path with no domain like `/some/path`. The baseURL must be same as your server listening address.
137+
One more important thing is that, for your mock and nitro server handler work properly, you need to set `baseURL` in the server option. The baseURL must be same as your server listening address.
138+
139+
And, when mocking the server side request, you need include the baseURL in your handler's path.
104140
```ts
105141
// ~/msw/server.ts
106142
import { http, HttpResponse } from 'msw'
107143

108144
export default defineNuxtMswServerOption(() => {
109145
// assume your server listening at http://localhost:3000
110-
const baseURL = "http://localhost:3000" // or use useRuntimeConfig() to get your baseURL setting
146+
const baseURL = "http://localhost:3000"
147+
148+
// composables that not related to NuxtApp can be used here, like: useRuntimeConfig
111149

112150
const handlers = [
113151
// Intercept "GET http://localhost:3000/user" requests...
@@ -119,7 +157,7 @@ export default defineNuxtMswServerOption(() => {
119157
}),
120158
]
121159
return {
122-
baseURL, // baseURL is required when using $fetch or useFetch with no baseURL and relative path
160+
baseURL, // baseURL is required
123161
handlers,
124162
serverOptions: {
125163
onUnhandledRequest: 'bypass',
@@ -265,19 +303,19 @@ const { data } = await useFetch(
265303

266304
```ts
267305
// @vitest-environment nuxt
268-
import { it, expect } from 'vitest'
306+
import { it, expect, afterEach } from 'vitest'
269307
import { mountSuspended } from '@nuxt/test-utils/runtime'
270308
import UserName from './UserName.vue'
271309

272310
import { setupNuxtMswServer } from '@crazydos/nuxt-msw/test-utils'
273311
import { http, HttpResponse } from "msw"
274312

275-
const mswServer = setupNuxtMswServer({
313+
const mswServer = await setupNuxtMswServer({
276314
handlers: [
277315
http.get(
278316
'http://localhost:3000/api/user',
279317
() => HttpResponse.json({ name: 'msw server unit' })
280-
})
318+
)
281319
],
282320
serverOptions: {
283321
// onUnhandledRequest: 'bypass',
@@ -302,6 +340,13 @@ it('displays message', async () => {
302340
expect(component.find('#name').text()).toBe('msw server unit')
303341
})
304342
```
343+
344+
Then you can run the test with `vitest`:
345+
346+
```bash
347+
npx vitest
348+
```
349+
305350
##### Note: `baseURL` in unit test
306351
If you are using `$fetch` or `useFetch` with no `baseURL` set, and use path with no domain like `/some/path`. You can mock like following:
307352

@@ -311,17 +356,18 @@ await useFetch('/api/user')
311356

312357
// mock with baseURL
313358
const baseURL = 'http://localhost:3000'
314-
setupNuxtMswServer({
359+
await setupNuxtMswServer({
315360
baseURL,
316361
handlers: [
317362
http.get(baseURL + '/api/user', () => {
318363
return HttpResponse.json({ name: 'msw server unit' })
319364
})
320365
]
321366
})
322-
323367
```
324368

369+
> [!NOTE] `baseURL` here is just a valid domain, it is not necessary to match nuxt server address. But if you run a real server and want msw fallback to there, you need to set baseURL to the real server address.
370+
325371

326372
#### E2E Tests
327373
`@nuxt/test-utils` uses Playwright as the E2E test driver, and it actually runs the Nuxt server and browser in a separate process. This means we can't directly modify mock in test.
@@ -431,18 +477,11 @@ export default defineNuxtMswTestOptions(() => {
431477
import { setupNuxtMswServer } from '@crazydos/nuxt-msw/test-utils'
432478
import { http, HttpResponse } from "msw"
433479

434-
const mswServer = setupNuxtMswServer({
435-
handlers: [
436-
http.get(
437-
// TODO: need baseURL ?
438-
'/api/user',
439-
() => HttpResponse.json({ name: 'msw server unit' })
440-
})
441-
],
442-
serverOptions: {
443-
// onUnhandledRequest: 'bypass',
444-
},
445-
})
480+
const mswServer = await setupNuxtMswServer(
481+
/**
482+
* You can still pass options to overwrite the options in unit.ts
483+
*/
484+
)
446485

447486
```
448487

0 commit comments

Comments
 (0)