1+ import type { SerializedError } from '@reduxjs/toolkit'
12import { configureStore } from '@reduxjs/toolkit'
23import type { BaseQueryFn , FetchBaseQueryError } from '@reduxjs/toolkit/query'
34import { createApi , fetchBaseQuery } from '@reduxjs/toolkit/query'
45import type { Post } from './mocks/server'
56import { posts } from './mocks/server'
67import { actionsReducer , setupApiStore } from './helpers'
8+ import type { QuerySubState } from '@reduxjs/toolkit/dist/query/core/apiState'
79
810describe ( 'queryFn base implementation tests' , ( ) => {
911 const baseQuery : BaseQueryFn < string , { wrappedByBaseQuery : string } , string > =
@@ -172,7 +174,15 @@ describe('queryFn base implementation tests', () => {
172174 [ 'withAsyncThrowingQueryFn' , withAsyncThrowingQueryFn , 'throw' ] ,
173175 ] ) ( '%s1' , async ( endpointName , endpoint , expectedResult ) => {
174176 const thunk = endpoint . initiate ( endpointName )
175- const result = await store . dispatch ( thunk )
177+ let result : undefined | QuerySubState < any > = undefined
178+ await expect ( async ( ) => {
179+ result = await store . dispatch ( thunk )
180+ } ) . toHaveConsoleOutput (
181+ endpointName . includes ( 'Throw' )
182+ ? `An unhandled error occured processing a request for the endpoint "${ endpointName } ".
183+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${ endpointName } )]`
184+ : ''
185+ )
176186 if ( expectedResult === 'data' ) {
177187 expect ( result ) . toEqual (
178188 expect . objectContaining ( {
@@ -209,7 +219,19 @@ describe('queryFn base implementation tests', () => {
209219 ] ,
210220 ] ) ( '%s' , async ( endpointName , endpoint , expectedResult ) => {
211221 const thunk = endpoint . initiate ( endpointName )
212- const result = await store . dispatch ( thunk )
222+ let result :
223+ | undefined
224+ | { data : string }
225+ | { error : string | SerializedError } = undefined
226+ await expect ( async ( ) => {
227+ result = await store . dispatch ( thunk )
228+ } ) . toHaveConsoleOutput (
229+ endpointName . includes ( 'Throw' )
230+ ? `An unhandled error occured processing a request for the endpoint "${ endpointName } ".
231+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${ endpointName } )]`
232+ : ''
233+ )
234+
213235 if ( expectedResult === 'data' ) {
214236 expect ( result ) . toEqual (
215237 expect . objectContaining ( {
@@ -236,17 +258,32 @@ describe('queryFn base implementation tests', () => {
236258 test ( 'neither provided' , async ( ) => {
237259 {
238260 const thunk = withNeither . initiate ( 'withNeither' )
239- const result = await store . dispatch ( thunk )
240- expect ( result . error ) . toEqual (
261+ let result : QuerySubState < any >
262+ await expect ( async ( ) => {
263+ result = await store . dispatch ( thunk )
264+ } ) . toHaveConsoleOutput (
265+ `An unhandled error occured processing a request for the endpoint "withNeither".
266+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`
267+ )
268+ expect ( result ! . error ) . toEqual (
241269 expect . objectContaining ( {
242270 message : 'endpointDefinition.queryFn is not a function' ,
243271 } )
244272 )
245273 }
246274 {
275+ let result :
276+ | undefined
277+ | { data : string }
278+ | { error : string | SerializedError } = undefined
247279 const thunk = mutationWithNeither . initiate ( 'mutationWithNeither' )
248- const result = await store . dispatch ( thunk )
249- expect ( 'error' in result && result . error ) . toEqual (
280+ await expect ( async ( ) => {
281+ result = await store . dispatch ( thunk )
282+ } ) . toHaveConsoleOutput (
283+ `An unhandled error occured processing a request for the endpoint "mutationWithNeither".
284+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`
285+ )
286+ expect ( ( result as any ) . error ) . toEqual (
250287 expect . objectContaining ( {
251288 message : 'endpointDefinition.queryFn is not a function' ,
252289 } )
@@ -336,11 +373,17 @@ describe('usage scenario tests', () => {
336373 } )
337374
338375 it ( 'can wrap a service like Firebase and handle errors' , async ( ) => {
339- const result = await storeRef . store . dispatch (
340- api . endpoints . getMissingFirebaseUser . initiate ( 1 )
341- )
342- expect ( result . data ) . toBeUndefined ( )
343- expect ( result . error ) . toEqual (
376+ let result : QuerySubState < any >
377+ await expect ( async ( ) => {
378+ result = await storeRef . store . dispatch (
379+ api . endpoints . getMissingFirebaseUser . initiate ( 1 )
380+ )
381+ } )
382+ . toHaveConsoleOutput ( `An unhandled error occured processing a request for the endpoint "getMissingFirebaseUser".
383+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: Missing user]` )
384+
385+ expect ( result ! . data ) . toBeUndefined ( )
386+ expect ( result ! . error ) . toEqual (
344387 expect . objectContaining ( {
345388 message : 'Missing user' ,
346389 name : 'Error' ,
0 commit comments