Skip to content

Commit 7c93729

Browse files
committed
jest.spyOn can be used with jest.mock and Node.js 18
1 parent 1b8a8b4 commit 7c93729

File tree

6 files changed

+38
-62
lines changed

6 files changed

+38
-62
lines changed

examples/node/jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const config = {
2424
lines: 100
2525
}
2626
}
27+
28+
// https://github.com/facebook/jest/issues/9047
29+
// https://github.com/facebook/jest/issues/10419#issuecomment-731176514
30+
// clearMocks: true,
31+
// resetMocks: true,
32+
// restoreMocks: true
2733
};
2834

2935
export default config;

examples/node/requests.test.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1322
test('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

3037
test('postJSON201CreatedExample()', async () => {
@@ -44,8 +51,6 @@ test('postJSON201CreatedExample()', async () => {
4451
title: 'foo',
4552
userId: 1
4653
});
47-
48-
mock.mockRestore();
4954
});
5055

5156
test('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

6164
test('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

7677
test('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

9190
test('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

10198
test('abortRequestExample()', async () => {
@@ -127,6 +124,4 @@ test('abortRequestExample()', async () => {
127124
signal: expect.any(AbortSignal)
128125
}
129126
);
130-
131-
mock.mockRestore();
132127
});

examples/web/jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const config = {
1919
lines: 100
2020
}
2121
}
22+
23+
// https://github.com/facebook/jest/issues/9047
24+
// https://github.com/facebook/jest/issues/10419#issuecomment-731176514
25+
// clearMocks: true,
26+
// resetMocks: true,
27+
// restoreMocks: true
2228
};
2329

2430
module.exports = config;

examples/web/requests.test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import {
1212
uploadFilesExample
1313
} from './requests';
1414

15+
// https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-968853688
16+
// https://github.com/swc-project/swc/issues/5059
17+
jest.mock('@tkrotoff/fetch', () => ({
18+
__esModule: true,
19+
...jest.requireActual('@tkrotoff/fetch')
20+
}));
21+
22+
beforeEach(jest.restoreAllMocks);
23+
1524
test('get200OKExample()', async () => {
1625
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
1726
Http.createJSONResponsePromise({
@@ -25,8 +34,6 @@ test('get200OKExample()', async () => {
2534
await get200OKExample();
2635
expect(mock).toHaveBeenCalledTimes(1);
2736
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
28-
29-
mock.mockRestore();
3037
});
3138

3239
test('postJSON201CreatedExample()', async () => {
@@ -46,8 +53,6 @@ test('postJSON201CreatedExample()', async () => {
4653
title: 'foo',
4754
userId: 1
4855
});
49-
50-
mock.mockRestore();
5156
});
5257

5358
test('del200OKExample()', async () => {
@@ -56,8 +61,6 @@ test('del200OKExample()', async () => {
5661
await del200OKExample();
5762
expect(mock).toHaveBeenCalledTimes(1);
5863
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
59-
60-
mock.mockRestore();
6164
});
6265

6366
test('get404NotFoundExample()', async () => {
@@ -71,8 +74,6 @@ test('get404NotFoundExample()', async () => {
7174
await get404NotFoundExample();
7275
expect(mock).toHaveBeenCalledTimes(1);
7376
expect(mock).toHaveBeenCalledWith('https://httpstat.us/404/cors');
74-
75-
mock.mockRestore();
7677
});
7778

7879
test('get500InternalServerErrorExample()', async () => {
@@ -86,8 +87,6 @@ test('get500InternalServerErrorExample()', async () => {
8687
await get500InternalServerErrorExample();
8788
expect(mock).toHaveBeenCalledTimes(1);
8889
expect(mock).toHaveBeenCalledWith('https://httpstat.us/500/cors');
89-
90-
mock.mockRestore();
9190
});
9291

9392
test('getCorsBlockedExample()', async () => {
@@ -96,8 +95,6 @@ test('getCorsBlockedExample()', async () => {
9695
await getCorsBlockedExample();
9796
expect(mock).toHaveBeenCalledTimes(1);
9897
expect(mock).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
99-
100-
mock.mockRestore();
10198
});
10299

103100
test('uploadFilesExample()', async () => {
@@ -113,8 +110,6 @@ test('uploadFilesExample()', async () => {
113110
await uploadFilesExample([file0, file1] as unknown as FileList);
114111
expect(mock).toHaveBeenCalledTimes(1);
115112
expect(mock).toHaveBeenCalledWith('https://httpbin.org/anything', expect.any(FormData));
116-
117-
mock.mockRestore();
118113
});
119114

120115
test('abortRequestExample()', async () => {
@@ -143,8 +138,6 @@ test('abortRequestExample()', async () => {
143138
signal: expect.any(AbortSignal)
144139
}
145140
);
146-
147-
mock.mockRestore();
148141
});
149142

150143
// FIXME jsdom does not support Blob.stream https://github.com/jsdom/jsdom/issues/2555
@@ -175,6 +168,4 @@ test.skip('downloadProgressExample()', async () => {
175168
expect(mock).toHaveBeenCalledWith(
176169
'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'
177170
);
178-
179-
mock.mockRestore();
180171
});

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ const config = {
3131
lines: 100
3232
}
3333
}
34+
35+
// https://github.com/facebook/jest/issues/9047
36+
// https://github.com/facebook/jest/issues/10419#issuecomment-731176514
37+
// clearMocks: true,
38+
// resetMocks: true,
39+
// restoreMocks: true
3440
};
3541

3642
module.exports = config;

src/createResponsePromise.test.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { createJSONResponsePromise, createResponsePromise } from './createRespon
55
import * as Http from './Http';
66
import { HttpStatus } from './HttpStatus';
77

8+
beforeEach(jest.restoreAllMocks);
9+
810
test('default Response object', async () => {
911
const responsePromise = createResponsePromise('text');
1012
const response = await responsePromise;
@@ -106,8 +108,6 @@ describe('get()', () => {
106108

107109
expect(mock).toHaveBeenCalledTimes(1);
108110
expect(mock).toHaveBeenCalledWith('url');
109-
110-
mock.mockRestore();
111111
});
112112

113113
test('OK .json()', async () => {
@@ -120,8 +120,6 @@ describe('get()', () => {
120120

121121
expect(mock).toHaveBeenCalledTimes(1);
122122
expect(mock).toHaveBeenCalledWith('url');
123-
124-
mock.mockRestore();
125123
});
126124

127125
test('fail', async () => {
@@ -136,8 +134,6 @@ describe('get()', () => {
136134

137135
expect(mock).toHaveBeenCalledTimes(1);
138136
expect(mock).toHaveBeenCalledWith('url');
139-
140-
mock.mockRestore();
141137
});
142138

143139
describe('get().text()', () => {
@@ -149,8 +145,6 @@ describe('get()', () => {
149145

150146
expect(mock).toHaveBeenCalledTimes(1);
151147
expect(mock).toHaveBeenCalledWith('url');
152-
153-
mock.mockRestore();
154148
});
155149

156150
test('fail', async () => {
@@ -165,8 +159,6 @@ describe('get()', () => {
165159

166160
expect(mock).toHaveBeenCalledTimes(1);
167161
expect(mock).toHaveBeenCalledWith('url');
168-
169-
mock.mockRestore();
170162
});
171163
});
172164

@@ -181,8 +173,6 @@ describe('get()', () => {
181173

182174
expect(mock).toHaveBeenCalledTimes(1);
183175
expect(mock).toHaveBeenCalledWith('url');
184-
185-
mock.mockRestore();
186176
});
187177

188178
test('fail', async () => {
@@ -197,8 +187,6 @@ describe('get()', () => {
197187

198188
expect(mock).toHaveBeenCalledTimes(1);
199189
expect(mock).toHaveBeenCalledWith('url');
200-
201-
mock.mockRestore();
202190
});
203191
});
204192
});
@@ -212,8 +200,6 @@ describe('post()', () => {
212200

213201
expect(mock).toHaveBeenCalledTimes(1);
214202
expect(mock).toHaveBeenCalledWith('url', 'body');
215-
216-
mock.mockRestore();
217203
});
218204

219205
test('OK .json()', async () => {
@@ -226,8 +212,6 @@ describe('post()', () => {
226212

227213
expect(mock).toHaveBeenCalledTimes(1);
228214
expect(mock).toHaveBeenCalledWith('url', 'body');
229-
230-
mock.mockRestore();
231215
});
232216

233217
test('fail', async () => {
@@ -242,8 +226,6 @@ describe('post()', () => {
242226

243227
expect(mock).toHaveBeenCalledTimes(1);
244228
expect(mock).toHaveBeenCalledWith('url', 'body');
245-
246-
mock.mockRestore();
247229
});
248230

249231
describe('post().text()', () => {
@@ -255,8 +237,6 @@ describe('post()', () => {
255237

256238
expect(mock).toHaveBeenCalledTimes(1);
257239
expect(mock).toHaveBeenCalledWith('url', 'body');
258-
259-
mock.mockRestore();
260240
});
261241

262242
test('fail', async () => {
@@ -271,8 +251,6 @@ describe('post()', () => {
271251

272252
expect(mock).toHaveBeenCalledTimes(1);
273253
expect(mock).toHaveBeenCalledWith('url', 'body');
274-
275-
mock.mockRestore();
276254
});
277255
});
278256

@@ -287,8 +265,6 @@ describe('post()', () => {
287265

288266
expect(mock).toHaveBeenCalledTimes(1);
289267
expect(mock).toHaveBeenCalledWith('url', 'body');
290-
291-
mock.mockRestore();
292268
});
293269

294270
test('fail', async () => {
@@ -303,8 +279,6 @@ describe('post()', () => {
303279

304280
expect(mock).toHaveBeenCalledTimes(1);
305281
expect(mock).toHaveBeenCalledWith('url', 'body');
306-
307-
mock.mockRestore();
308282
});
309283
});
310284
});
@@ -336,7 +310,5 @@ describe('flushPromises()', () => {
336310

337311
expect(mock).toHaveBeenCalledTimes(1);
338312
expect(mock).toHaveBeenCalledWith('url');
339-
340-
mock.mockRestore();
341313
});
342314
});

0 commit comments

Comments
 (0)