Skip to content

Commit 1b8a8b4

Browse files
committed
Revert "Fix example tests with Node.js 18 thx to jest.mock"
1 parent 0f50c05 commit 1b8a8b4

File tree

2 files changed

+98
-112
lines changed

2 files changed

+98
-112
lines changed

examples/node/requests.test.ts

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
createJSONResponsePromise,
3-
createResponsePromise,
4-
del,
5-
get,
6-
HttpStatus,
7-
Init,
8-
postJSON,
9-
ResponsePromiseWithBodyMethods
10-
} from '@tkrotoff/fetch';
1+
import * as Http from '@tkrotoff/fetch';
112

123
import {
134
abortRequestExample,
@@ -19,23 +10,9 @@ import {
1910
postJSON201CreatedExample
2011
} from './requests';
2112

22-
beforeEach(() => jest.resetAllMocks());
23-
24-
jest.mock('@tkrotoff/fetch', () => ({
25-
...jest.requireActual('@tkrotoff/fetch'),
26-
get: jest.fn(),
27-
//post: jest.fn(),
28-
postJSON: jest.fn(),
29-
//put: jest.fn(),
30-
//putJSON: jest.fn(),
31-
//patch: jest.fn(),
32-
//patchJSON: jest.fn(),
33-
del: jest.fn()
34-
}));
35-
3613
test('get200OKExample()', async () => {
37-
jest.mocked(get).mockImplementation(() =>
38-
createJSONResponsePromise({
14+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
15+
Http.createJSONResponsePromise({
3916
userId: 1,
4017
id: 1,
4118
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
@@ -44,13 +21,15 @@ test('get200OKExample()', async () => {
4421
);
4522

4623
await get200OKExample();
47-
expect(get).toHaveBeenCalledTimes(1);
48-
expect(get).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
24+
expect(mock).toHaveBeenCalledTimes(1);
25+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
26+
27+
mock.mockRestore();
4928
});
5029

5130
test('postJSON201CreatedExample()', async () => {
52-
jest.mocked(postJSON).mockImplementation(() =>
53-
createJSONResponsePromise({
31+
const mock = jest.spyOn(Http, 'postJSON').mockImplementation(() =>
32+
Http.createJSONResponsePromise({
5433
id: 101,
5534
title: 'foo',
5635
body: 'bar',
@@ -59,54 +38,64 @@ test('postJSON201CreatedExample()', async () => {
5938
);
6039

6140
await postJSON201CreatedExample();
62-
expect(postJSON).toHaveBeenCalledTimes(1);
63-
expect(postJSON).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts', {
41+
expect(mock).toHaveBeenCalledTimes(1);
42+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts', {
6443
body: 'bar',
6544
title: 'foo',
6645
userId: 1
6746
});
47+
48+
mock.mockRestore();
6849
});
6950

7051
test('del200OKExample()', async () => {
71-
jest.mocked(del).mockImplementation(() => createJSONResponsePromise({}));
52+
const mock = jest.spyOn(Http, 'del').mockImplementation(() => Http.createJSONResponsePromise({}));
7253

7354
await del200OKExample();
74-
expect(del).toHaveBeenCalledTimes(1);
75-
expect(del).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
55+
expect(mock).toHaveBeenCalledTimes(1);
56+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
57+
58+
mock.mockRestore();
7659
});
7760

7861
test('get404NotFoundExample()', async () => {
79-
jest.mocked(get).mockImplementation(() =>
80-
createResponsePromise('404 Not Found', {
81-
status: HttpStatus._404_NotFound,
62+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
63+
Http.createResponsePromise('404 Not Found', {
64+
status: Http.HttpStatus._404_NotFound,
8265
statusText: 'Not Found'
8366
})
8467
);
8568

8669
await get404NotFoundExample();
87-
expect(get).toHaveBeenCalledTimes(1);
88-
expect(get).toHaveBeenCalledWith('https://httpstat.us/404/cors');
70+
expect(mock).toHaveBeenCalledTimes(1);
71+
expect(mock).toHaveBeenCalledWith('https://httpstat.us/404/cors');
72+
73+
mock.mockRestore();
8974
});
9075

9176
test('get500InternalServerErrorExample()', async () => {
92-
jest.mocked(get).mockImplementation(() =>
93-
createResponsePromise('500 Internal Server Error', {
94-
status: HttpStatus._500_InternalServerError,
77+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
78+
Http.createResponsePromise('500 Internal Server Error', {
79+
status: Http.HttpStatus._500_InternalServerError,
9580
statusText: 'Internal Server Error'
9681
})
9782
);
9883

9984
await get500InternalServerErrorExample();
100-
expect(get).toHaveBeenCalledTimes(1);
101-
expect(get).toHaveBeenCalledWith('https://httpstat.us/500/cors');
85+
expect(mock).toHaveBeenCalledTimes(1);
86+
expect(mock).toHaveBeenCalledWith('https://httpstat.us/500/cors');
87+
88+
mock.mockRestore();
10289
});
10390

10491
test('getCorsBlockedExample()', async () => {
105-
jest.mocked(get).mockRejectedValue(new TypeError('Failed to fetch'));
92+
const mock = jest.spyOn(Http, 'get').mockRejectedValue(new TypeError('Failed to fetch'));
10693

10794
await getCorsBlockedExample();
108-
expect(get).toHaveBeenCalledTimes(1);
109-
expect(get).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
95+
expect(mock).toHaveBeenCalledTimes(1);
96+
expect(mock).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
97+
98+
mock.mockRestore();
11099
});
111100

112101
test('abortRequestExample()', async () => {
@@ -115,7 +104,7 @@ test('abortRequestExample()', async () => {
115104
const abortError = new Error('The operation was aborted.');
116105
abortError.name = 'AbortError';
117106

118-
jest.mocked(get).mockImplementation((_input, init) => {
107+
const mock = jest.spyOn(Http, 'get').mockImplementation((_input, init) => {
119108
// Mock aborted request
120109
// https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
121110
const response = new Promise((resolve, reject) => {
@@ -127,15 +116,17 @@ test('abortRequestExample()', async () => {
127116
}, 600);
128117
});
129118

130-
return response as ResponsePromiseWithBodyMethods;
119+
return response as Http.ResponsePromiseWithBodyMethods;
131120
});
132121

133122
await abortRequestExample();
134-
expect(get).toHaveBeenCalledTimes(1);
135-
expect(get).toHaveBeenCalledWith(
123+
expect(mock).toHaveBeenCalledTimes(1);
124+
expect(mock).toHaveBeenCalledWith(
136125
'https://httpbin.org/drip?duration=2&numbytes=10&code=200&delay=2',
137126
{
138127
signal: expect.any(AbortSignal)
139128
}
140129
);
130+
131+
mock.mockRestore();
141132
});

examples/web/requests.test.ts

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
createJSONResponsePromise,
3-
createResponsePromise,
4-
del,
5-
get,
6-
HttpStatus,
7-
post,
8-
postJSON,
9-
ResponsePromiseWithBodyMethods
10-
} from '@tkrotoff/fetch';
1+
import * as Http from '@tkrotoff/fetch';
112

123
import {
134
abortRequestExample,
@@ -21,23 +12,9 @@ import {
2112
uploadFilesExample
2213
} from './requests';
2314

24-
beforeEach(() => jest.resetAllMocks());
25-
26-
jest.mock('@tkrotoff/fetch', () => ({
27-
...jest.requireActual('@tkrotoff/fetch'),
28-
get: jest.fn(),
29-
post: jest.fn(),
30-
postJSON: jest.fn(),
31-
//put: jest.fn(),
32-
//putJSON: jest.fn(),
33-
//patch: jest.fn(),
34-
//patchJSON: jest.fn(),
35-
del: jest.fn()
36-
}));
37-
3815
test('get200OKExample()', async () => {
39-
jest.mocked(get).mockImplementation(() =>
40-
createJSONResponsePromise({
16+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
17+
Http.createJSONResponsePromise({
4118
userId: 1,
4219
id: 1,
4320
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
@@ -46,13 +23,15 @@ test('get200OKExample()', async () => {
4623
);
4724

4825
await get200OKExample();
49-
expect(get).toHaveBeenCalledTimes(1);
50-
expect(get).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
26+
expect(mock).toHaveBeenCalledTimes(1);
27+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
28+
29+
mock.mockRestore();
5130
});
5231

5332
test('postJSON201CreatedExample()', async () => {
54-
jest.mocked(postJSON).mockImplementation(() =>
55-
createJSONResponsePromise({
33+
const mock = jest.spyOn(Http, 'postJSON').mockImplementation(() =>
34+
Http.createJSONResponsePromise({
5635
id: 101,
5736
title: 'foo',
5837
body: 'bar',
@@ -61,59 +40,69 @@ test('postJSON201CreatedExample()', async () => {
6140
);
6241

6342
await postJSON201CreatedExample();
64-
expect(postJSON).toHaveBeenCalledTimes(1);
65-
expect(postJSON).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts', {
43+
expect(mock).toHaveBeenCalledTimes(1);
44+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts', {
6645
body: 'bar',
6746
title: 'foo',
6847
userId: 1
6948
});
49+
50+
mock.mockRestore();
7051
});
7152

7253
test('del200OKExample()', async () => {
73-
jest.mocked(del).mockImplementation(() => createJSONResponsePromise({}));
54+
const mock = jest.spyOn(Http, 'del').mockImplementation(() => Http.createJSONResponsePromise({}));
7455

7556
await del200OKExample();
76-
expect(del).toHaveBeenCalledTimes(1);
77-
expect(del).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
57+
expect(mock).toHaveBeenCalledTimes(1);
58+
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
59+
60+
mock.mockRestore();
7861
});
7962

8063
test('get404NotFoundExample()', async () => {
81-
jest.mocked(get).mockImplementation(() =>
82-
createResponsePromise('404 Not Found', {
83-
status: HttpStatus._404_NotFound,
64+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
65+
Http.createResponsePromise('404 Not Found', {
66+
status: Http.HttpStatus._404_NotFound,
8467
statusText: 'Not Found'
8568
})
8669
);
8770

8871
await get404NotFoundExample();
89-
expect(get).toHaveBeenCalledTimes(1);
90-
expect(get).toHaveBeenCalledWith('https://httpstat.us/404/cors');
72+
expect(mock).toHaveBeenCalledTimes(1);
73+
expect(mock).toHaveBeenCalledWith('https://httpstat.us/404/cors');
74+
75+
mock.mockRestore();
9176
});
9277

9378
test('get500InternalServerErrorExample()', async () => {
94-
jest.mocked(get).mockImplementation(() =>
95-
createResponsePromise('500 Internal Server Error', {
96-
status: HttpStatus._500_InternalServerError,
79+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
80+
Http.createResponsePromise('500 Internal Server Error', {
81+
status: Http.HttpStatus._500_InternalServerError,
9782
statusText: 'Internal Server Error'
9883
})
9984
);
10085

10186
await get500InternalServerErrorExample();
102-
expect(get).toHaveBeenCalledTimes(1);
103-
expect(get).toHaveBeenCalledWith('https://httpstat.us/500/cors');
87+
expect(mock).toHaveBeenCalledTimes(1);
88+
expect(mock).toHaveBeenCalledWith('https://httpstat.us/500/cors');
89+
90+
mock.mockRestore();
10491
});
10592

10693
test('getCorsBlockedExample()', async () => {
107-
jest.mocked(get).mockRejectedValue(new TypeError('Failed to fetch'));
94+
const mock = jest.spyOn(Http, 'get').mockRejectedValue(new TypeError('Failed to fetch'));
10895

10996
await getCorsBlockedExample();
110-
expect(get).toHaveBeenCalledTimes(1);
111-
expect(get).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
97+
expect(mock).toHaveBeenCalledTimes(1);
98+
expect(mock).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
99+
100+
mock.mockRestore();
112101
});
113102

114103
test('uploadFilesExample()', async () => {
115-
jest.mocked(post).mockImplementation(() =>
116-
createJSONResponsePromise({
104+
const mock = jest.spyOn(Http, 'post').mockImplementation(() =>
105+
Http.createJSONResponsePromise({
117106
files: { file0: 'file0Content', file1: 'file1Content' }
118107
})
119108
);
@@ -122,14 +111,16 @@ test('uploadFilesExample()', async () => {
122111
const file1 = new File(['file1Content'], 'file1', { type: 'text/plain' });
123112

124113
await uploadFilesExample([file0, file1] as unknown as FileList);
125-
expect(post).toHaveBeenCalledTimes(1);
126-
expect(post).toHaveBeenCalledWith('https://httpbin.org/anything', expect.any(FormData));
114+
expect(mock).toHaveBeenCalledTimes(1);
115+
expect(mock).toHaveBeenCalledWith('https://httpbin.org/anything', expect.any(FormData));
116+
117+
mock.mockRestore();
127118
});
128119

129120
test('abortRequestExample()', async () => {
130121
const abortError = new DOMException('The user aborted a request.', 'AbortError');
131122

132-
jest.mocked(get).mockImplementation((_input, init) => {
123+
const mock = jest.spyOn(Http, 'get').mockImplementation((_input, init) => {
133124
// Mock aborted request
134125
// https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
135126
const response = new Promise((resolve, reject) => {
@@ -141,17 +132,19 @@ test('abortRequestExample()', async () => {
141132
}, 600);
142133
});
143134

144-
return response as ResponsePromiseWithBodyMethods;
135+
return response as Http.ResponsePromiseWithBodyMethods;
145136
});
146137

147138
await abortRequestExample();
148-
expect(get).toHaveBeenCalledTimes(1);
149-
expect(get).toHaveBeenCalledWith(
139+
expect(mock).toHaveBeenCalledTimes(1);
140+
expect(mock).toHaveBeenCalledWith(
150141
'https://httpbin.org/drip?duration=2&numbytes=10&code=200&delay=2',
151142
{
152143
signal: expect.any(AbortSignal)
153144
}
154145
);
146+
147+
mock.mockRestore();
155148
});
156149

157150
// FIXME jsdom does not support Blob.stream https://github.com/jsdom/jsdom/issues/2555
@@ -169,17 +162,19 @@ test.skip('downloadProgressExample()', async () => {
169162
const blob = new Blob([content.buffer]);
170163
const stream = blob.stream();
171164

172-
jest.mocked(get).mockImplementation(() =>
173-
createResponsePromise(stream, {
165+
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
166+
Http.createResponsePromise(stream, {
174167
headers: {
175168
'content-length': blob.size.toString()
176169
}
177170
})
178171
);
179172

180173
await downloadProgressExample();
181-
expect(get).toHaveBeenCalledTimes(1);
182-
expect(get).toHaveBeenCalledWith(
174+
expect(mock).toHaveBeenCalledTimes(1);
175+
expect(mock).toHaveBeenCalledWith(
183176
'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'
184177
);
178+
179+
mock.mockRestore();
185180
});

0 commit comments

Comments
 (0)