Skip to content

Commit 40826a5

Browse files
committed
Fix console spy related issues in queryFn.test.tsx
1 parent 41b9a50 commit 40826a5

File tree

1 file changed

+83
-45
lines changed

1 file changed

+83
-45
lines changed

packages/toolkit/src/query/tests/queryFn.test.tsx

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { noop } from '@internal/listenerMiddleware/utils'
12
import type { QuerySubState } from '@internal/query/core/apiState'
23
import type { Post } from '@internal/query/tests/mocks/handlers'
34
import { posts } from '@internal/query/tests/mocks/handlers'
@@ -185,16 +186,25 @@ describe('queryFn base implementation tests', () => {
185186
['withAsyncErrorQueryFn', withAsyncErrorQueryFn, 'error'],
186187
['withAsyncThrowingQueryFn', withAsyncThrowingQueryFn, 'throw'],
187188
])('%s', async (endpointName, endpoint, expectedResult) => {
189+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
190+
188191
const thunk = endpoint.initiate(endpointName)
189-
let result: undefined | QuerySubState<any> = undefined
190-
await expect(async () => {
191-
result = await store.dispatch(thunk)
192-
}).toHaveConsoleOutput(
193-
endpointName.includes('Throw')
194-
? `An unhandled error occurred processing a request for the endpoint "${endpointName}".
195-
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${endpointName})]`
196-
: '',
197-
)
192+
193+
const result = (await store.dispatch(thunk)) satisfies
194+
| undefined
195+
| QuerySubState<any>
196+
197+
if (endpointName.includes('Throw')) {
198+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
199+
200+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
201+
`An unhandled error occurred processing a request for the endpoint "${endpointName}".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
202+
Error(`resultFrom(${endpointName})`),
203+
)
204+
} else {
205+
expect(consoleErrorSpy).not.toHaveBeenCalled()
206+
}
207+
198208
if (expectedResult === 'data') {
199209
expect(result).toEqual(
200210
expect.objectContaining({
@@ -216,6 +226,8 @@ describe('queryFn base implementation tests', () => {
216226
}),
217227
)
218228
}
229+
230+
consoleErrorSpy.mockRestore()
219231
})
220232

221233
test.each([
@@ -230,19 +242,25 @@ describe('queryFn base implementation tests', () => {
230242
'throw',
231243
],
232244
])('%s', async (endpointName, endpoint, expectedResult) => {
245+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
246+
233247
const thunk = endpoint.initiate(endpointName)
234-
let result:
248+
249+
const result = (await store.dispatch(thunk)) satisfies
235250
| undefined
236251
| { data: string }
237-
| { error: string | SerializedError } = undefined
238-
await expect(async () => {
239-
result = await store.dispatch(thunk)
240-
}).toHaveConsoleOutput(
241-
endpointName.includes('Throw')
242-
? `An unhandled error occurred processing a request for the endpoint "${endpointName}".
243-
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${endpointName})]`
244-
: '',
245-
)
252+
| { error: string | SerializedError }
253+
254+
if (endpointName.includes('Throw')) {
255+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
256+
257+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
258+
`An unhandled error occurred processing a request for the endpoint "${endpointName}".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
259+
Error(`resultFrom(${endpointName})`),
260+
)
261+
} else {
262+
expect(consoleErrorSpy).not.toHaveBeenCalled()
263+
}
246264

247265
if (expectedResult === 'data') {
248266
expect(result).toEqual(
@@ -265,42 +283,56 @@ describe('queryFn base implementation tests', () => {
265283
}),
266284
)
267285
}
286+
287+
consoleErrorSpy.mockRestore()
268288
})
269289

270290
test('neither provided', async () => {
291+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
292+
271293
{
272294
const thunk = withNeither.initiate('withNeither')
273-
let result: QuerySubState<any>
274-
await expect(async () => {
275-
result = await store.dispatch(thunk)
276-
}).toHaveConsoleOutput(
277-
`An unhandled error occurred processing a request for the endpoint "withNeither".
278-
In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`,
295+
296+
const result = (await store.dispatch(thunk)) satisfies QuerySubState<any>
297+
298+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
299+
300+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
301+
`An unhandled error occurred processing a request for the endpoint "withNeither".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
302+
TypeError('endpointDefinition.queryFn is not a function'),
279303
)
280-
expect(result!.error).toEqual(
304+
305+
expect(result.error).toEqual(
281306
expect.objectContaining({
282307
message: 'endpointDefinition.queryFn is not a function',
283308
}),
284309
)
310+
311+
consoleErrorSpy.mockClear()
285312
}
286313
{
287-
let result:
314+
const thunk = mutationWithNeither.initiate('mutationWithNeither')
315+
316+
const result = (await store.dispatch(thunk)) satisfies
288317
| undefined
289318
| { data: string }
290-
| { error: string | SerializedError } = undefined
291-
const thunk = mutationWithNeither.initiate('mutationWithNeither')
292-
await expect(async () => {
293-
result = await store.dispatch(thunk)
294-
}).toHaveConsoleOutput(
295-
`An unhandled error occurred processing a request for the endpoint "mutationWithNeither".
296-
In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`,
319+
| { error: string | SerializedError }
320+
321+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
322+
323+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
324+
`An unhandled error occurred processing a request for the endpoint "mutationWithNeither".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
325+
TypeError('endpointDefinition.queryFn is not a function'),
297326
)
298-
expect((result as any).error).toEqual(
327+
328+
expect(result.error).toEqual(
299329
expect.objectContaining({
300330
message: 'endpointDefinition.queryFn is not a function',
301331
}),
302332
)
303333
}
334+
335+
consoleErrorSpy.mockRestore()
304336
})
305337
})
306338

@@ -385,21 +417,27 @@ describe('usage scenario tests', () => {
385417
})
386418

387419
it('can wrap a service like Firebase and handle errors', async () => {
388-
let result: QuerySubState<any>
389-
await expect(async () => {
390-
result = await storeRef.store.dispatch(
391-
api.endpoints.getMissingFirebaseUser.initiate(1),
392-
)
393-
})
394-
.toHaveConsoleOutput(`An unhandled error occurred processing a request for the endpoint "getMissingFirebaseUser".
395-
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: Missing user]`)
420+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
421+
422+
const result = (await storeRef.store.dispatch(
423+
api.endpoints.getMissingFirebaseUser.initiate(1),
424+
)) satisfies QuerySubState<any>
396425

397-
expect(result!.data).toBeUndefined()
398-
expect(result!.error).toEqual(
426+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
427+
428+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
429+
`An unhandled error occurred processing a request for the endpoint "getMissingFirebaseUser".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
430+
Error('Missing user'),
431+
)
432+
433+
expect(result.data).toBeUndefined()
434+
expect(result.error).toEqual(
399435
expect.objectContaining({
400436
message: 'Missing user',
401437
name: 'Error',
402438
}),
403439
)
440+
441+
consoleErrorSpy.mockRestore()
404442
})
405443
})

0 commit comments

Comments
 (0)