@@ -10,6 +10,15 @@ import {
1010 postJSON201CreatedExample
1111} from './requests' ;
1212
13+ // https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-968853688
14+ // https://github.com/swc-project/swc/issues/5059
15+ jest . mock ( '@tkrotoff/fetch' , ( ) => ( {
16+ __esModule : true ,
17+ ...jest . requireActual ( '@tkrotoff/fetch' )
18+ } ) ) ;
19+
20+ beforeEach ( jest . restoreAllMocks ) ;
21+
1322test ( 'get200OKExample()' , async ( ) => {
1423 const mock = jest . spyOn ( Http , 'get' ) . mockImplementation ( ( ) =>
1524 Http . createJSONResponsePromise ( {
@@ -23,8 +32,6 @@ test('get200OKExample()', async () => {
2332 await get200OKExample ( ) ;
2433 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
2534 expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
26-
27- mock . mockRestore ( ) ;
2835} ) ;
2936
3037test ( 'postJSON201CreatedExample()' , async ( ) => {
@@ -44,8 +51,6 @@ test('postJSON201CreatedExample()', async () => {
4451 title : 'foo' ,
4552 userId : 1
4653 } ) ;
47-
48- mock . mockRestore ( ) ;
4954} ) ;
5055
5156test ( 'del200OKExample()' , async ( ) => {
@@ -54,8 +59,6 @@ test('del200OKExample()', async () => {
5459 await del200OKExample ( ) ;
5560 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
5661 expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
57-
58- mock . mockRestore ( ) ;
5962} ) ;
6063
6164test ( 'get404NotFoundExample()' , async ( ) => {
@@ -69,8 +72,6 @@ test('get404NotFoundExample()', async () => {
6972 await get404NotFoundExample ( ) ;
7073 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
7174 expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/404/cors' ) ;
72-
73- mock . mockRestore ( ) ;
7475} ) ;
7576
7677test ( 'get500InternalServerErrorExample()' , async ( ) => {
@@ -84,8 +85,6 @@ test('get500InternalServerErrorExample()', async () => {
8485 await get500InternalServerErrorExample ( ) ;
8586 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
8687 expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/500/cors' ) ;
87-
88- mock . mockRestore ( ) ;
8988} ) ;
9089
9190test ( 'getCorsBlockedExample()' , async ( ) => {
@@ -94,8 +93,6 @@ test('getCorsBlockedExample()', async () => {
9493 await getCorsBlockedExample ( ) ;
9594 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
9695 expect ( mock ) . toHaveBeenCalledWith ( 'https://postman-echo.com/get?foo1=bar1&foo2=bar2' ) ;
97-
98- mock . mockRestore ( ) ;
9996} ) ;
10097
10198test ( 'abortRequestExample()' , async ( ) => {
@@ -104,23 +101,21 @@ test('abortRequestExample()', async () => {
104101 const abortError = new Error ( 'The operation was aborted.' ) ;
105102 abortError . name = 'AbortError' ;
106103
107- const mock = jest
108- . spyOn ( Http , 'get' )
109- . mockImplementation ( ( _input : RequestInfo , init : Http . Init ) => {
110- // Mock aborted request
111- // https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
112- const response = new Promise ( ( resolve , reject ) => {
113- setTimeout ( ( ) => {
114- if ( init . signal && init . signal . aborted ) {
115- reject ( abortError ) ;
116- }
117- resolve ( '**********' ) ;
118- } , 600 ) ;
119- } ) ;
120-
121- return response as Http . ResponsePromiseWithBodyMethods ;
104+ const mock = jest . spyOn ( Http , 'get' ) . mockImplementation ( ( _input , init ) => {
105+ // Mock aborted request
106+ // https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
107+ const response = new Promise ( ( resolve , reject ) => {
108+ setTimeout ( ( ) => {
109+ if ( init ! . signal && init ! . signal . aborted ) {
110+ reject ( abortError ) ;
111+ }
112+ resolve ( '**********' ) ;
113+ } , 600 ) ;
122114 } ) ;
123115
116+ return response as Http . ResponsePromiseWithBodyMethods ;
117+ } ) ;
118+
124119 await abortRequestExample ( ) ;
125120 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
126121 expect ( mock ) . toHaveBeenCalledWith (
@@ -129,6 +124,4 @@ test('abortRequestExample()', async () => {
129124 signal : expect . any ( AbortSignal )
130125 }
131126 ) ;
132-
133- mock . mockRestore ( ) ;
134127} ) ;
0 commit comments