Skip to content

Commit 68676c1

Browse files
committed
feat: adjust test usage
1 parent 06a3af1 commit 68676c1

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

src/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const customDefu = createDefu((obj, key, value) => {
143143
export default () => {
144144
return [
145145
${layerFilePathsNode.map((_, i) => `mswOptions${i}()`).join(',')}
146-
].reduce((acc, cur) => customDefu(cur, acc))
146+
].reduce((acc, cur) => customDefu(cur, acc), [])
147147
}`)
148148
_nuxt.options.build.transpile.push(composablePathTest)
149149
addImports([{
@@ -163,7 +163,7 @@ const customDefu = createDefu((obj, key, value) => {
163163
export default () => {
164164
return [
165165
${layerFilePathsServer.map((_, i) => `mswOptions${i}()`).join(',')}
166-
].reduce((acc, cur) => customDefu(cur, acc))
166+
].reduce((acc, cur) => customDefu(cur, acc), [])
167167
}`)
168168
_nuxt.options.build.transpile.push(newComposablePathServer)
169169
addServerImports([{
@@ -189,7 +189,7 @@ const customDefu = createDefu((obj, key, value) => {
189189
export default () => {
190190
return [
191191
${layerFilePathsWorker.map((_, i) => `mswOptions${i}()`).join(',')}
192-
].reduce((acc, cur) => customDefu(cur, acc))
192+
].reduce((acc, cur) => customDefu(cur, acc), [])
193193
}`)
194194
_nuxt.options.build.transpile.push(newComposablePathWorker)
195195
addImports([{

src/runtime/composables/useDefineOptions.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ import type { NuxtApp } from '#app'
66

77
export type TNuxtMswWorkerOptions = {
88
/**
9-
* Indicate baseURL of Nuxt server. e.g: `http://localhost:3000`
10-
* Required if you use `useFetch` or `$fetch` with relative URL in your app.
11-
*/
12-
baseURL?: string
13-
/**
14-
* Define the handlers passed to `setupWorker()` and `setupServer()`.
15-
*
16-
* You can pass different handlers for client-side and server-side.
9+
* Define the handlers passed to `setupWorker()`.
1710
*
1811
* - See [Dynamic Mocking](https://mswjs.io/docs/best-practices/dynamic-mock-scenarios)
1912
* - See [setupWorker](https://mswjs.io/docs/api/setup-worker)
@@ -41,13 +34,10 @@ export type TNuxtMswServerOptions = {
4134
* Indicate baseURL of Nuxt server. e.g: `http://localhost:3000`
4235
* Required if you use `useFetch` or `$fetch` with relative URL in your app.
4336
*/
44-
baseURL?: string
37+
baseURL: string
4538
/**
46-
* Define the handlers passed to `setupWorker()` and `setupServer()`.
47-
*
48-
* You can pass different handlers for client-side and server-side.
39+
* Define the handlers passed to `setupServer()`.
4940
*
50-
* - See [Dynamic Mocking](https://mswjs.io/docs/best-practices/dynamic-mock-scenarios)
5141
* - See [setupServer](https://mswjs.io/docs/api/setup-server)
5242
*/
5343
handlers: HttpHandler[]
@@ -72,7 +62,7 @@ export type TNuxtMswServerOptions = {
7262
* @param event H3Event. See [H3](https://h3.unjs.io)
7363
* @returns
7464
*/
75-
afterResponse?: (server: SetupServerApi, event: H3Event) => void | Promise<void>
65+
// afterResponse?: (server: SetupServerApi, event: H3Event) => void | Promise<void>
7666
}
7767

7868
/**
@@ -102,7 +92,25 @@ export const defineNuxtMswServerOption = (
10292
: () => options
10393
}
10494

105-
export type TNuxtMswTestOptions = Pick<TNuxtMswServerOptions, 'handlers' | 'serverOptions' | 'baseURL'>
95+
export type TNuxtMswTestOptions = {
96+
/**
97+
* Any baseURL, for mocking $fetch and useFetch. e.g: `http://localhost:3000`
98+
* Required if you use `useFetch` or `$fetch` with relative URL in your app.
99+
*/
100+
baseURL?: string
101+
102+
/**
103+
* Define the handlers passed to `setupServer()`.
104+
*/
105+
handlers?: HttpHandler[]
106+
107+
/**
108+
* Options for the `server.listen()`.
109+
*
110+
* See https://mswjs.io/docs/api/setup-server/listen
111+
*/
112+
serverOptions?: Partial<SharedOptions>
113+
}
106114
/**
107115
*
108116
* Defines the Nuxt MSW Server option when working with `@nuxt/test-utils`.

src/runtime/test-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { _mswTestOptions } from '#imports'
66
import type { TNuxtMswTestOptions } from '#imports'
77

88
export const setupNuxtMswServer = async (options: TNuxtMswTestOptions = {}) => {
9-
const _mswOptions = defu(options, _mswTestOptions())
9+
const _mswOptions = defu(defu(options, _mswTestOptions()), {
10+
handlers: [],
11+
serverOptions: {},
12+
})
1013
const server = setupServer(
1114
...(
1215
typeof _mswOptions.handlers === 'function'

test/component.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { http, HttpResponse } from 'msw'
55
import { setupNuxtMswServer } from '../src/runtime/test-utils'
66
import UserName from './fixtures/basic/components/UserName.vue'
77

8-
setupNuxtMswServer({
8+
await setupNuxtMswServer({
99
baseURL: 'http://localhost:3001',
1010
handlers: [
1111
http.get('http://localhost:3001/api/user', () => {

0 commit comments

Comments
 (0)