Skip to content

Commit a2afc9d

Browse files
authored
fix(runtime): remove redefinition of $fetch.create (#1471)
1 parent dc29ca9 commit a2afc9d

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

examples/app-vitest-full/tests/nuxt/server.spec.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { describe, expect, it, vi } from 'vitest'
22

33
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
44

@@ -176,4 +176,53 @@ describe('server mocks and data fetching', () => {
176176
expect(await fetch(new URL('http://localhost:3000/with-url')).then(res => res.json())).toMatchObject({ title: 'with-url', data: {} })
177177
expect(await fetch(new URL('http://localhost:3000/with-url?q=1')).then(res => res.json())).toMatchObject({ title: 'with-url', data: { q: '1' } })
178178
})
179+
180+
it('can mock fetch requests with fetch.create', async () => {
181+
registerEndpoint('/fetch-create/1', event => ({
182+
title: 'title from mocked api1',
183+
headers: getHeaders(event),
184+
}))
185+
registerEndpoint('/fetch-create/2', event => ({
186+
title: 'title from mocked api2',
187+
headers: getHeaders(event),
188+
}))
189+
registerEndpoint('/fetch-create/error', () =>
190+
new Response(undefined, { status: 500, statusText: 'Mock Server Error' }),
191+
)
192+
193+
const onRequest = vi.fn()
194+
const onResponse = vi.fn()
195+
const onRequestError = vi.fn()
196+
const onResponseError = vi.fn()
197+
198+
const fetch = $fetch.create({
199+
baseURL: '/fetch-create',
200+
retry: false,
201+
onRequest,
202+
onResponse,
203+
onRequestError,
204+
onResponseError,
205+
headers: {
206+
authorization: 'Bearer <access_token>',
207+
},
208+
})
209+
210+
expect(await fetch<unknown>('/1')).toMatchObject({
211+
title: 'title from mocked api1',
212+
headers: { authorization: 'Bearer <access_token>' },
213+
})
214+
215+
expect(await fetch<unknown>('/2')).toMatchObject({
216+
title: 'title from mocked api2',
217+
headers: { authorization: 'Bearer <access_token>' },
218+
})
219+
220+
await expect(fetch<unknown>('/error')).rejects.toMatchObject({ status: 500, statusText: 'Mock Server Error' })
221+
await expect(fetch<unknown>('/error', { baseURL: '"INVALID"' })).rejects.toThrowError()
222+
223+
expect(onRequest).toBeCalledTimes(4)
224+
expect(onResponse).toBeCalledTimes(3)
225+
expect(onRequestError).toBeCalledTimes(1)
226+
expect(onResponseError).toBeCalledTimes(1)
227+
})
179228
})

examples/app-vitest/test/registerEndpoint.nuxt.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ describe('registerEndpoint tests', () => {
2121
registerEndpoint('/test2/', () => endpoint())
2222
const component = await mountSuspended(TestFetchComponent)
2323

24-
component
24+
await component
2525
.find<HTMLButtonElement>('#custom-fetcher')
26-
.element.click()
26+
.trigger('click')
2727

2828
expect(endpoint).toHaveBeenCalled()
2929
component.unmount()

src/runtime/shared/environment.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ export async function setupWindow(win: NuxtWindow, environmentOptions: { nuxt: N
101101
// @ts-expect-error fetch types differ slightly
102102
win.$fetch = createFetch({ fetch: win.fetch, Headers: win.Headers })
103103

104-
// @ts-expect-error fetch types differ slightly
105-
win.$fetch.create = (options = {}) => {
106-
return createFetch({ fetch: win.fetch, Headers: win.Headers, ...options })
107-
}
108-
109104
win.__registry = registry
110105
win.__app = h3App
111106

0 commit comments

Comments
 (0)