@@ -10,6 +10,15 @@ import {
10
10
postJSON201CreatedExample
11
11
} from './requests' ;
12
12
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
+
13
22
test ( 'get200OKExample()' , async ( ) => {
14
23
const mock = jest . spyOn ( Http , 'get' ) . mockImplementation ( ( ) =>
15
24
Http . createJSONResponsePromise ( {
@@ -23,8 +32,6 @@ test('get200OKExample()', async () => {
23
32
await get200OKExample ( ) ;
24
33
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
25
34
expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
26
-
27
- mock . mockRestore ( ) ;
28
35
} ) ;
29
36
30
37
test ( 'postJSON201CreatedExample()' , async ( ) => {
@@ -44,8 +51,6 @@ test('postJSON201CreatedExample()', async () => {
44
51
title : 'foo' ,
45
52
userId : 1
46
53
} ) ;
47
-
48
- mock . mockRestore ( ) ;
49
54
} ) ;
50
55
51
56
test ( 'del200OKExample()' , async ( ) => {
@@ -54,8 +59,6 @@ test('del200OKExample()', async () => {
54
59
await del200OKExample ( ) ;
55
60
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
56
61
expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
57
-
58
- mock . mockRestore ( ) ;
59
62
} ) ;
60
63
61
64
test ( 'get404NotFoundExample()' , async ( ) => {
@@ -69,8 +72,6 @@ test('get404NotFoundExample()', async () => {
69
72
await get404NotFoundExample ( ) ;
70
73
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
71
74
expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/404/cors' ) ;
72
-
73
- mock . mockRestore ( ) ;
74
75
} ) ;
75
76
76
77
test ( 'get500InternalServerErrorExample()' , async ( ) => {
@@ -84,8 +85,6 @@ test('get500InternalServerErrorExample()', async () => {
84
85
await get500InternalServerErrorExample ( ) ;
85
86
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
86
87
expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/500/cors' ) ;
87
-
88
- mock . mockRestore ( ) ;
89
88
} ) ;
90
89
91
90
test ( 'getCorsBlockedExample()' , async ( ) => {
@@ -94,8 +93,6 @@ test('getCorsBlockedExample()', async () => {
94
93
await getCorsBlockedExample ( ) ;
95
94
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
96
95
expect ( mock ) . toHaveBeenCalledWith ( 'https://postman-echo.com/get?foo1=bar1&foo2=bar2' ) ;
97
-
98
- mock . mockRestore ( ) ;
99
96
} ) ;
100
97
101
98
test ( 'abortRequestExample()' , async ( ) => {
@@ -104,23 +101,21 @@ test('abortRequestExample()', async () => {
104
101
const abortError = new Error ( 'The operation was aborted.' ) ;
105
102
abortError . name = 'AbortError' ;
106
103
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 ) ;
122
114
} ) ;
123
115
116
+ return response as Http . ResponsePromiseWithBodyMethods ;
117
+ } ) ;
118
+
124
119
await abortRequestExample ( ) ;
125
120
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
126
121
expect ( mock ) . toHaveBeenCalledWith (
@@ -129,6 +124,4 @@ test('abortRequestExample()', async () => {
129
124
signal : expect . any ( AbortSignal )
130
125
}
131
126
) ;
132
-
133
- mock . mockRestore ( ) ;
134
127
} ) ;
0 commit comments