@@ -12,21 +12,14 @@ import type {
1212} from '@reduxjs/toolkit/query'
1313import { createApi , fetchBaseQuery } from '@reduxjs/toolkit/query'
1414import type { MockInstance } from 'vitest'
15- import { vi } from 'vitest'
16-
17- import { rest } from 'msw'
18- import {
19- ANY ,
20- getSerializedHeaders ,
21- setupApiStore ,
22- waitMs ,
23- } from '../../tests/utils/helpers'
24- import { expectExactType , expectType } from '../../tests/utils/typeTestHelpers'
15+
2516import type {
2617 DefinitionsFromApi ,
2718 OverrideResultType ,
2819 TagTypesFromApi ,
2920} from '@reduxjs/toolkit/dist/query/endpointDefinitions'
21+ import { HttpResponse , delay , http } from 'msw'
22+ import nodeFetch from 'node-fetch'
3023import { server } from './mocks/server'
3124
3225beforeAll ( ( ) => {
@@ -186,7 +179,7 @@ describe('wrong tagTypes log errors', () => {
186179 store . dispatch ( api . endpoints [ endpoint ] . initiate ( ) )
187180 let result : { status : string }
188181 do {
189- await waitMs ( 5 )
182+ await delay ( 5 )
190183 // @ts -ignore
191184 result = api . endpoints [ endpoint ] . select ( ) ( store . getState ( ) )
192185 } while ( result . status === 'pending' )
@@ -461,11 +454,11 @@ describe('endpoint definition typings', () => {
461454 } )
462455
463456 storeRef . store . dispatch ( api . endpoints . query1 . initiate ( 'in1' ) )
464- await waitMs ( 1 )
457+ await delay ( 1 )
465458 expect ( spy ) . not . toHaveBeenCalled ( )
466459
467460 storeRef . store . dispatch ( api . endpoints . query2 . initiate ( 'in2' ) )
468- await waitMs ( 1 )
461+ await delay ( 1 )
469462 expect ( spy ) . toHaveBeenCalledWith (
470463 "Tag type 'missing' was used, but not specified in `tagTypes`!"
471464 )
@@ -652,11 +645,12 @@ describe('additional transformResponse behaviors', () => {
652645 query : build . query < SuccessResponse & EchoResponseData , void > ( {
653646 query : ( ) => '/success' ,
654647 transformResponse : async ( response : SuccessResponse ) => {
655- const res = await fetch ( 'https://example.com/echo' , {
648+ const res : any = await nodeFetch ( 'https://example.com/echo' , {
656649 method : 'POST' ,
657650 body : JSON . stringify ( { banana : 'bread' } ) ,
658651 } ) . then ( ( res ) => res . json ( ) )
659- const additionalData = JSON . parse ( res . body ) as EchoResponseData
652+
653+ const additionalData = res . body as EchoResponseData
660654 return { ...response , ...additionalData }
661655 } ,
662656 } ) ,
@@ -717,7 +711,6 @@ describe('additional transformResponse behaviors', () => {
717711 response : {
718712 headers : {
719713 'content-type' : 'application/json' ,
720- 'x-powered-by' : 'msw' ,
721714 } ,
722715 } ,
723716 } ,
@@ -738,7 +731,6 @@ describe('additional transformResponse behaviors', () => {
738731 response : {
739732 headers : {
740733 'content-type' : 'application/json' ,
741- 'x-powered-by' : 'msw' ,
742734 } ,
743735 } ,
744736 } ,
@@ -796,32 +788,36 @@ describe('query endpoint lifecycles - onStart, onSuccess, onError', () => {
796788 test ( 'query lifecycle events fire properly' , async ( ) => {
797789 // We intentionally fail the first request so we can test all lifecycles
798790 server . use (
799- rest . get ( 'https://example.com/success' , ( _ , res , ctx ) =>
800- res . once ( ctx . status ( 500 ) , ctx . json ( { value : 'failed' } ) )
791+ http . get (
792+ 'https://example.com/success' ,
793+ ( ) => HttpResponse . json ( { value : 'failed' } , { status : 500 } ) ,
794+ { once : true }
801795 )
802796 )
803797
804798 expect ( storeRef . store . getState ( ) . testReducer . count ) . toBe ( null )
805799 const failAttempt = storeRef . store . dispatch ( api . endpoints . query . initiate ( ) )
806800 expect ( storeRef . store . getState ( ) . testReducer . count ) . toBe ( 0 )
807801 await failAttempt
808- await waitMs ( 10 )
802+ await delay ( 10 )
809803 expect ( storeRef . store . getState ( ) . testReducer . count ) . toBe ( - 1 )
810804
811805 const successAttempt = storeRef . store . dispatch (
812806 api . endpoints . query . initiate ( )
813807 )
814808 expect ( storeRef . store . getState ( ) . testReducer . count ) . toBe ( 0 )
815809 await successAttempt
816- await waitMs ( 10 )
810+ await delay ( 10 )
817811 expect ( storeRef . store . getState ( ) . testReducer . count ) . toBe ( 1 )
818812 } )
819813
820814 test ( 'mutation lifecycle events fire properly' , async ( ) => {
821815 // We intentionally fail the first request so we can test all lifecycles
822816 server . use (
823- rest . post ( 'https://example.com/success' , ( _ , res , ctx ) =>
824- res . once ( ctx . status ( 500 ) , ctx . json ( { value : 'failed' } ) )
817+ http . post (
818+ 'https://example.com/success' ,
819+ ( ) => HttpResponse . json ( { value : 'failed' } , { status : 500 } ) ,
820+ { once : true }
825821 )
826822 )
827823
@@ -945,7 +941,7 @@ describe('custom serializeQueryArgs per endpoint', () => {
945941 }
946942
947943 const dummyClient : MyApiClient = {
948- async fetchPost ( id ) {
944+ async fetchPost ( ) {
949945 return { value : 'success' }
950946 } ,
951947 }
@@ -1106,12 +1102,13 @@ describe('custom serializeQueryArgs per endpoint', () => {
11061102 const PAGE_SIZE = 3
11071103
11081104 server . use (
1109- rest . get ( 'https://example.com/listItems' , ( req , res , ctx ) => {
1110- const pageString = req . url . searchParams . get ( 'page' )
1105+ http . get ( 'https://example.com/listItems' , ( { request } ) => {
1106+ const url = new URL ( request . url )
1107+ const pageString = url . searchParams . get ( 'page' )
11111108 const pageNum = parseInt ( pageString || '0' )
11121109
11131110 const results = paginate ( allItems , PAGE_SIZE , pageNum )
1114- return res ( ctx . json ( results ) )
1111+ return HttpResponse . json ( results )
11151112 } )
11161113 )
11171114
@@ -1134,12 +1131,13 @@ describe('custom serializeQueryArgs per endpoint', () => {
11341131 const PAGE_SIZE = 3
11351132
11361133 server . use (
1137- rest . get ( 'https://example.com/listItems2' , ( req , res , ctx ) => {
1138- const pageString = req . url . searchParams . get ( 'page' )
1134+ http . get ( 'https://example.com/listItems2' , ( { request } ) => {
1135+ const url = new URL ( request . url )
1136+ const pageString = url . searchParams . get ( 'page' )
11391137 const pageNum = parseInt ( pageString || '0' )
11401138
11411139 const results = paginate ( allItems , PAGE_SIZE , pageNum )
1142- return res ( ctx . json ( results ) )
1140+ return HttpResponse . json ( results )
11431141 } )
11441142 )
11451143
0 commit comments