1- import { rest } from 'msw' ;
1+ import { http , HttpResponse } from 'msw' ;
22import { createClient } from '../src/createClient' ;
33import { testBaseUrl } from './mocks/handlers' ;
44import { server } from './mocks/server' ;
@@ -23,12 +23,12 @@ describe('createClient', () => {
2323 test ( 'Throws an error if `serviceDomain` or `apiKey` is missing' , ( ) => {
2424 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2525 // @ts -expect-error
26- expect ( ( ) => createClient ( { serviceDomain : 'foo' } ) ) . toThrowError (
26+ expect ( ( ) => createClient ( { serviceDomain : 'foo' } ) ) . toThrow (
2727 new Error ( 'parameter is required (check serviceDomain and apiKey)' ) ,
2828 ) ;
2929 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3030 // @ts -expect-error
31- expect ( ( ) => createClient ( { apiKey : 'foo' } ) ) . toThrowError (
31+ expect ( ( ) => createClient ( { apiKey : 'foo' } ) ) . toThrow (
3232 new Error ( 'parameter is required (check serviceDomain and apiKey)' ) ,
3333 ) ;
3434 } ) ;
@@ -37,21 +37,21 @@ describe('createClient', () => {
3737 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3838 // @ts -expect-error
3939 createClient ( { serviceDomain : 10 , apiKey : 'foo' } ) ,
40- ) . toThrowError ( new Error ( 'parameter is not string' ) ) ;
40+ ) . toThrow ( new Error ( 'parameter is not string' ) ) ;
4141 expect ( ( ) =>
4242 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4343 // @ts -expect-error
4444 createClient ( { serviceDomain : 'foo' , apiKey : 10 } ) ,
45- ) . toThrowError ( new Error ( 'parameter is not string' ) ) ;
45+ ) . toThrow ( new Error ( 'parameter is not string' ) ) ;
4646 } ) ;
4747
4848 describe ( 'Throws an error when response.ok is false' , ( ) => {
4949 test ( 'If there is a message' , ( ) => {
5050 server . use (
51- rest . get ( `${ testBaseUrl } /list-type` , async ( _ , res , ctx ) => {
52- return res (
53- ctx . status ( 401 ) ,
54- ctx . json ( { message : 'X-MICROCMS-KEY header is invalid.' } ) ,
51+ http . get ( `${ testBaseUrl } /list-type` , async ( ) => {
52+ return HttpResponse . json (
53+ { message : 'X-MICROCMS-KEY header is invalid.' } ,
54+ { status : 401 } ,
5555 ) ;
5656 } ) ,
5757 ) ;
@@ -60,41 +60,41 @@ describe('createClient', () => {
6060 apiKey : 'apiKey' ,
6161 } ) ;
6262
63- expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrowError (
63+ expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrow (
6464 new Error (
6565 'fetch API response status: 401\n message is `X-MICROCMS-KEY header is invalid.`' ,
6666 ) ,
6767 ) ;
6868 } ) ;
6969 test ( 'If there is no message' , ( ) => {
7070 server . use (
71- rest . get ( `${ testBaseUrl } /list-type` , async ( _ , res , ctx ) => {
72- return res ( ctx . status ( 404 ) ) ;
71+ http . get ( `${ testBaseUrl } /list-type` , async ( ) => {
72+ return new HttpResponse ( null , { status : 404 } ) ;
7373 } ) ,
7474 ) ;
7575 const client = createClient ( {
7676 serviceDomain : 'serviceDomain' ,
7777 apiKey : 'apiKey' ,
7878 } ) ;
7979
80- expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrowError (
80+ expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrow (
8181 new Error ( 'fetch API response status: 404' ) ,
8282 ) ;
8383 } ) ;
8484 } ) ;
8585
8686 test ( 'Throws an error in the event of a network error.' , ( ) => {
8787 server . use (
88- rest . get ( `${ testBaseUrl } /list-type` , async ( _ , res ) => {
89- return res . networkError ( 'Failed to fetch' ) ;
88+ http . get ( `${ testBaseUrl } /list-type` , async ( ) => {
89+ return HttpResponse . error ( ) ;
9090 } ) ,
9191 ) ;
9292 const client = createClient ( {
9393 serviceDomain : 'serviceDomain' ,
9494 apiKey : 'apiKey' ,
9595 } ) ;
9696
97- expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrowError (
97+ expect ( client . get ( { endpoint : 'list-type' } ) ) . rejects . toThrow (
9898 new Error ( 'Network Error.\n Details: Failed to fetch' ) ,
9999 ) ;
100100 } ) ;
@@ -110,45 +110,46 @@ describe('createClient', () => {
110110 let apiCallCount = 0 ;
111111
112112 server . use (
113- rest . get ( `${ testBaseUrl } /500` , async ( _ , res , ctx ) => {
113+ http . get ( `${ testBaseUrl } /500` , async ( ) => {
114114 apiCallCount ++ ;
115- return res ( ctx . status ( 500 ) ) ;
115+ return new HttpResponse ( null , {
116+ status : 500 ,
117+ } ) ;
116118 } ) ,
117119 ) ;
118120
119- await expect (
120- retryableClient . get ( { endpoint : ' 500' } ) ,
121- ) . rejects . toThrowError ( new Error ( 'fetch API response status: 500' ) ) ;
121+ await expect ( retryableClient . get ( { endpoint : '500' } ) ) . rejects . toThrow (
122+ new Error ( 'fetch API response status: 500') ,
123+ ) ;
122124 expect ( apiCallCount ) . toBe ( 3 ) ;
123125 } , 30000 ) ;
124126
125127 test ( 'Returns an error message if 4xx error(excluding 429)' , async ( ) => {
126128 let apiCallCount = 0 ;
127129
128130 server . use (
129- rest . get ( `${ testBaseUrl } /400` , async ( _ , res , ctx ) => {
131+ http . get ( `${ testBaseUrl } /400` , async ( ) => {
130132 apiCallCount ++ ;
131- return res ( ctx . status ( 400 ) ) ;
133+ return new HttpResponse ( null , { status : 400 } ) ;
132134 } ) ,
133135 ) ;
134136
135- await expect (
136- retryableClient . get ( { endpoint : ' 400' } ) ,
137- ) . rejects . toThrowError ( new Error ( 'fetch API response status: 400' ) ) ;
137+ await expect ( retryableClient . get ( { endpoint : '400' } ) ) . rejects . toThrow (
138+ new Error ( 'fetch API response status: 400') ,
139+ ) ;
138140 expect ( apiCallCount ) . toBe ( 1 ) ;
139141 } ) ;
140142
141143 test ( 'List format contents can be retrieved if failed twice and succeeded once' , async ( ) => {
142144 let failedRequestCount = 0 ;
143145 server . use (
144- rest . get ( `${ testBaseUrl } /two-times-fail` , ( _ , res , ctx ) => {
146+ http . get ( `${ testBaseUrl } /two-times-fail` , ( ) => {
145147 if ( failedRequestCount < 2 ) {
146148 failedRequestCount ++ ;
147- return res ( ctx . status ( 500 ) ) ;
149+ return new HttpResponse ( null , { status : 500 } ) ;
148150 } else {
149- return res (
150- ctx . status ( 200 ) ,
151- ctx . json ( {
151+ return HttpResponse . json (
152+ {
152153 contents : [
153154 {
154155 id : 'foo' ,
@@ -162,7 +163,8 @@ describe('createClient', () => {
162163 totalCount : 1 ,
163164 limit : 10 ,
164165 offset : 0 ,
165- } ) ,
166+ } ,
167+ { status : 200 } ,
166168 ) ;
167169 }
168170 } ) ,
@@ -189,12 +191,11 @@ describe('createClient', () => {
189191 test ( 'List format contents can be retrieved if succeeded once and failed twice' , async ( ) => {
190192 let apiCallCount = 0 ;
191193 server . use (
192- rest . get ( `${ testBaseUrl } /only-first-time-success` , ( _ , res , ctx ) => {
194+ http . get ( `${ testBaseUrl } /only-first-time-success` , ( ) => {
193195 apiCallCount ++ ;
194196 if ( apiCallCount === 1 ) {
195- return res (
196- ctx . status ( 200 ) ,
197- ctx . json ( {
197+ return HttpResponse . json (
198+ {
198199 contents : [
199200 {
200201 id : 'foo' ,
@@ -208,10 +209,11 @@ describe('createClient', () => {
208209 totalCount : 1 ,
209210 limit : 10 ,
210211 offset : 0 ,
211- } ) ,
212+ } ,
213+ { status : 200 } ,
212214 ) ;
213215 } else {
214- return res ( ctx . status ( 500 ) ) ;
216+ return new HttpResponse ( null , { status : 500 } ) ;
215217 }
216218 } ) ,
217219 ) ;
0 commit comments