1+ import { noop } from '@internal/listenerMiddleware/utils'
12import { configureStore } from '@reduxjs/toolkit'
2- import {
3- mockConsole ,
4- createConsole ,
5- getLog ,
6- } from 'console-testing-library/pure'
73import { createApi , fetchBaseQuery } from '@reduxjs/toolkit/query'
84
9- let restore : ( ) => void = ( ) => { }
10- let nodeEnv : string
11-
12- beforeEach ( ( ) => {
13- restore = mockConsole ( createConsole ( ) )
14- nodeEnv = process . env . NODE_ENV !
15- ; ( process . env as any ) . NODE_ENV = 'development'
16- } )
17-
18- afterEach ( ( ) => {
19- ; ( process . env as any ) . NODE_ENV = nodeEnv
20- restore ( )
21- } )
5+ const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( noop )
6+ const consoleErrorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( noop )
227
238const baseUrl = 'https://example.com'
249
@@ -49,9 +34,21 @@ function createApis() {
4934
5035let [ api1 , api1_2 , api2 ] = createApis ( )
5136beforeEach ( ( ) => {
37+ vi . stubEnv ( 'NODE_ENV' , 'development' )
5238 ; [ api1 , api1_2 , api2 ] = createApis ( )
5339} )
5440
41+ afterEach ( ( ) => {
42+ consoleWarnSpy . mockClear ( )
43+ consoleErrorSpy . mockClear ( )
44+ } )
45+
46+ afterAll ( ( ) => {
47+ consoleWarnSpy . mockRestore ( )
48+ consoleErrorSpy . mockRestore ( )
49+ vi . unstubAllEnvs ( )
50+ } )
51+
5552const reMatchMissingMiddlewareError =
5653 / W a r n i n g : M i d d l e w a r e f o r R T K - Q u e r y A P I a t r e d u c e r P a t h " a p i " h a s n o t b e e n a d d e d t o t h e s t o r e /
5754
@@ -60,6 +57,10 @@ describe('missing middleware', () => {
6057 [ 'development' , true ] ,
6158 [ 'production' , false ] ,
6259 ] ) ( '%s warns if middleware is missing: %s' , ( env , shouldWarn ) => {
60+ vi . stubEnv ( 'NODE_ENV' , env )
61+
62+ expect ( process . env . NODE_ENV ) . toBe ( env )
63+
6364 const store = configureStore ( {
6465 reducer : { [ api1 . reducerPath ] : api1 . reducer } ,
6566 } )
@@ -79,7 +80,10 @@ describe('missing middleware', () => {
7980 middleware : ( gdm ) => gdm ( ) . concat ( api1 . middleware ) ,
8081 } )
8182 store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
82- expect ( getLog ( ) . log ) . toBe ( `` )
83+
84+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
85+
86+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
8387 } )
8488
8589 test ( 'warns only once per api' , ( ) => {
@@ -119,13 +123,21 @@ describe('missing reducer', () => {
119123 [ 'development' , true ] ,
120124 [ 'production' , false ] ,
121125 ] ) ( '%s warns if reducer is missing: %s' , ( env , shouldWarn ) => {
126+ beforeEach ( ( ) => {
127+ vi . stubEnv ( 'NODE_ENV' , env )
128+
129+ return vi . unstubAllEnvs
130+ } )
131+
122132 test ( 'middleware not crashing if reducer is missing' , async ( ) => {
123133 const store = configureStore ( {
124134 reducer : { x : ( ) => 0 } ,
125135 // @ts -expect-error
126136 middleware : ( gdm ) => gdm ( ) . concat ( api1 . middleware ) ,
127137 } )
128138 await store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
139+
140+ expect ( process . env . NODE_ENV ) . toBe ( env )
129141 } )
130142
131143 test ( `warning behavior` , ( ) => {
@@ -136,11 +148,21 @@ describe('missing reducer', () => {
136148 } )
137149 // @ts -expect-error
138150 api1 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
139- expect ( getLog ( ) . log ) . toBe (
140- shouldWarn
141- ? 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?'
142- : '' ,
143- )
151+
152+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
153+
154+ expect ( process . env . NODE_ENV ) . toBe ( env )
155+
156+ if ( shouldWarn ) {
157+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
158+
159+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
160+ 1 ,
161+ 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?' ,
162+ )
163+ } else {
164+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
165+ }
144166 } )
145167 } )
146168
@@ -150,7 +172,10 @@ describe('missing reducer', () => {
150172 middleware : ( gdm ) => gdm ( ) . concat ( api1 . middleware ) ,
151173 } )
152174 api1 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
153- expect ( getLog ( ) . log ) . toBe ( `` )
175+
176+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
177+
178+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
154179 } )
155180
156181 test ( 'warns only once per api' , ( ) => {
@@ -163,7 +188,13 @@ describe('missing reducer', () => {
163188 api1 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
164189 // @ts -expect-error
165190 api1 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
166- expect ( getLog ( ) . log ) . toBe (
191+
192+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
193+
194+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
195+
196+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
197+ 1 ,
167198 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?' ,
168199 )
169200 } )
@@ -178,8 +209,19 @@ describe('missing reducer', () => {
178209 api1 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
179210 // @ts -expect-error
180211 api2 . endpoints . q1 . select ( undefined ) ( store . getState ( ) )
181- expect ( getLog ( ) . log ) . toBe (
182- 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?\nError: No data found at `state.api2`. Did you forget to add the reducer to the store?' ,
212+
213+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 2 )
214+
215+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
216+
217+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
218+ 1 ,
219+ 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?' ,
220+ )
221+
222+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
223+ 2 ,
224+ 'Error: No data found at `state.api2`. Did you forget to add the reducer to the store?' ,
183225 )
184226 } )
185227} )
@@ -194,12 +236,24 @@ test('warns for reducer and also throws error if everything is missing', async (
194236 store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
195237 }
196238 expect ( doDispatch ) . toThrowError ( reMatchMissingMiddlewareError )
197- expect ( getLog ( ) . log ) . toBe (
239+
240+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
241+
242+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
243+
244+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
245+ 1 ,
198246 'Error: No data found at `state.api`. Did you forget to add the reducer to the store?' ,
199247 )
200248} )
201249
202250describe ( 'warns on multiple apis using the same `reducerPath`' , ( ) => {
251+ beforeEach ( ( ) => {
252+ vi . stubEnv ( 'NODE_ENV' , 'development' )
253+
254+ return vi . unstubAllEnvs
255+ } )
256+
203257 test ( 'common: two apis, same order' , async ( ) => {
204258 const store = configureStore ( {
205259 reducer : {
@@ -212,8 +266,14 @@ describe('warns on multiple apis using the same `reducerPath`', () => {
212266 middleware : ( gDM ) => gDM ( ) . concat ( api1 . middleware , api1_2 . middleware ) ,
213267 } )
214268 await store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
269+
270+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
271+
272+ expect ( consoleWarnSpy ) . toHaveBeenCalledOnce ( )
273+
215274 // only second api prints
216- expect ( getLog ( ) . log ) . toBe (
275+ expect ( consoleWarnSpy ) . toHaveBeenNthCalledWith (
276+ 1 ,
217277 `There is a mismatch between slice and middleware for the reducerPath "api".
218278You can only have one api per reducer path, this will lead to crashes in various situations!
219279If you have multiple apis, you *have* to specify the reducerPath option when using createApi!` ,
@@ -231,13 +291,16 @@ If you have multiple apis, you *have* to specify the reducerPath option when usi
231291 middleware : ( gDM ) => gDM ( ) . concat ( api1_2 . middleware , api1 . middleware ) ,
232292 } )
233293 await store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
294+
295+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
296+
297+ expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 2 )
298+
234299 // both apis print
235- expect ( getLog ( ) . log ) . toBe (
300+ expect ( consoleWarnSpy ) . toHaveBeenNthCalledWith (
301+ 1 ,
236302 `There is a mismatch between slice and middleware for the reducerPath "api".
237303You can only have one api per reducer path, this will lead to crashes in various situations!
238- If you have multiple apis, you *have* to specify the reducerPath option when using createApi!
239- There is a mismatch between slice and middleware for the reducerPath "api".
240- You can only have one api per reducer path, this will lead to crashes in various situations!
241304If you have multiple apis, you *have* to specify the reducerPath option when using createApi!` ,
242305 )
243306 } )
@@ -254,7 +317,14 @@ If you have multiple apis, you *have* to specify the reducerPath option when usi
254317 } )
255318 await store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
256319
257- expect ( getLog ( ) . log ) . toBe (
320+ expect ( process . env . NODE_ENV ) . toBe ( 'development' )
321+
322+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
323+
324+ expect ( consoleWarnSpy ) . toHaveBeenCalledOnce ( )
325+
326+ expect ( consoleWarnSpy ) . toHaveBeenNthCalledWith (
327+ 1 ,
258328 `There is a mismatch between slice and middleware for the reducerPath "api".
259329You can only have one api per reducer path, this will lead to crashes in various situations!
260330If you have multiple apis, you *have* to specify the reducerPath option when using createApi!` ,
@@ -280,7 +350,12 @@ If you have multiple apis, you *have* to specify the reducerPath option when usi
280350 } )
281351 await store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
282352
283- expect ( getLog ( ) . log ) . toBe (
353+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
354+
355+ expect ( consoleWarnSpy ) . toHaveBeenCalledOnce ( )
356+
357+ expect ( consoleWarnSpy ) . toHaveBeenNthCalledWith (
358+ 1 ,
284359 `There is a mismatch between slice and middleware for the reducerPath "api".
285360You can only have one api per reducer path, this will lead to crashes in various situations!
286361If you have multiple apis, you *have* to specify the reducerPath option when using createApi!` ,
@@ -304,9 +379,16 @@ describe('`console.error` on unhandled errors during `initiate`', () => {
304379 } )
305380 await store . dispatch ( api . endpoints . baseQuery . initiate ( ) )
306381
307- expect ( getLog ( ) . log )
308- . toBe ( `An unhandled error occurred processing a request for the endpoint "baseQuery".
309- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
382+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
383+
384+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
385+
386+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
387+ 1 ,
388+ `An unhandled error occurred processing a request for the endpoint "baseQuery".
389+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
390+ Error ( 'this was kinda expected' ) ,
391+ )
310392 } )
311393
312394 test ( 'error thrown in `queryFn`' , async ( ) => {
@@ -328,9 +410,16 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".
328410 } )
329411 await store . dispatch ( api . endpoints . queryFn . initiate ( ) )
330412
331- expect ( getLog ( ) . log )
332- . toBe ( `An unhandled error occurred processing a request for the endpoint "queryFn".
333- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
413+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
414+
415+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
416+
417+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
418+ 1 ,
419+ `An unhandled error occurred processing a request for the endpoint "queryFn".
420+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
421+ Error ( 'this was kinda expected' ) ,
422+ )
334423 } )
335424
336425 test ( 'error thrown in `transformResponse`' , async ( ) => {
@@ -353,9 +442,16 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".
353442 } )
354443 await store . dispatch ( api . endpoints . transformRspn . initiate ( ) )
355444
356- expect ( getLog ( ) . log )
357- . toBe ( `An unhandled error occurred processing a request for the endpoint "transformRspn".
358- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
445+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
446+
447+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
448+
449+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
450+ 1 ,
451+ `An unhandled error occurred processing a request for the endpoint "transformRspn".
452+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
453+ Error ( 'this was kinda expected' ) ,
454+ )
359455 } )
360456
361457 test ( 'error thrown in `transformErrorResponse`' , async ( ) => {
@@ -381,9 +477,16 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".
381477 } )
382478 await store . dispatch ( api . endpoints . transformErRspn . initiate ( ) )
383479
384- expect ( getLog ( ) . log )
385- . toBe ( `An unhandled error occurred processing a request for the endpoint "transformErRspn".
386- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
480+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
481+
482+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
483+
484+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
485+ 1 ,
486+ `An unhandled error occurred processing a request for the endpoint "transformErRspn".
487+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
488+ Error ( 'this was kinda expected' ) ,
489+ )
387490 } )
388491
389492 test ( '`fetchBaseQuery`: error thrown in `prepareHeaders`' , async ( ) => {
@@ -408,9 +511,16 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".
408511 } )
409512 await store . dispatch ( api . endpoints . prep . initiate ( ) )
410513
411- expect ( getLog ( ) . log )
412- . toBe ( `An unhandled error occurred processing a request for the endpoint "prep".
413- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
514+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
515+
516+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
517+
518+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
519+ 1 ,
520+ `An unhandled error occurred processing a request for the endpoint "prep".
521+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
522+ Error ( 'this was kinda expected' ) ,
523+ )
414524 } )
415525
416526 test ( '`fetchBaseQuery`: error thrown in `validateStatus`' , async ( ) => {
@@ -438,8 +548,15 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".
438548 } )
439549 await store . dispatch ( api . endpoints . val . initiate ( ) )
440550
441- expect ( getLog ( ) . log )
442- . toBe ( `An unhandled error occurred processing a request for the endpoint "val".
443- In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: this was kinda expected]` )
551+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
552+
553+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( )
554+
555+ expect ( consoleErrorSpy ) . toHaveBeenNthCalledWith (
556+ 1 ,
557+ `An unhandled error occurred processing a request for the endpoint "val".
558+ In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
559+ Error ( 'this was kinda expected' ) ,
560+ )
444561 } )
445562} )
0 commit comments