Skip to content

Commit

Permalink
fix!: ignore query params when checking if an endpoint is mocked
Browse files Browse the repository at this point in the history
Co-authored-by: Tim King <kingtimm@gmail.com>
  • Loading branch information
danielroe and kingtimm committed Sep 29, 2023
1 parent 03ba07c commit c1f8890
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/vitest-environment-nuxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ export default <Environment>{
const registry = new Set<string>()

win.fetch = (init: string, options?: any) => {
if (typeof init === 'string' && registry.has(init)) {
init = '/_' + init
if (typeof init === 'string') {
const base = init.split('?')[0]
if (registry.has(base) || registry.has(init)) {
init = '/_' + init
}
}
return localFetch(init, options)
}
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ describe('test utils', () => {
)
})

it('can mock fetch requests', async () => {
registerEndpoint('/with-query', () => ({
title: 'mocked',
}))
expect(
await $fetch<unknown>('/with-query', { query: { test: true } })
).toMatchObject({
title: 'mocked',
})
})

it('can mock fetch requests with explicit methods', async () => {
registerEndpoint('/method', {
method: 'POST',
Expand Down

0 comments on commit c1f8890

Please sign in to comment.