Skip to content

Commit f341e36

Browse files
authored
Merge pull request #32 from tkrotoff/fastify4
Upgrade to Fastify 4 + other npm packages
2 parents 0e9eabc + b63a276 commit f341e36

28 files changed

+4472
-3659
lines changed

.eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ const config = {
9696
'@typescript-eslint/no-non-null-assertion': 'off',
9797
'unicorn/prefer-add-event-listener': 'off'
9898
}
99+
},
100+
{
101+
files: ['**/*.test.ts'],
102+
rules: {
103+
'playwright/no-skipped-test': 'off',
104+
'playwright/no-conditional-in-test': 'off'
105+
}
99106
}
100107
]
101108
};

.github/workflows/node.js.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
node-version: [14.x, 16.x]
14+
node-version: [14.x, 16.x, 18.x]
1515

1616
steps:
1717
- uses: actions/checkout@v2

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.14.2 (2022/12/01)
2+
3+
- Update npm packages
4+
- Test with Node.js 18 (#29)
5+
16
## 0.14.1 (2022/07/08)
27

38
- Accept URL as input

README.md

+56-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Check [examples/web](examples/web)
137137

138138
Instead of writing HTTP statuses as numbers `201`, `403`, `503`... you can replace them with [`HttpStatus`](src/HttpStatus.ts) and write more explicit code:
139139

140-
```JavaScript
140+
```TypeScript
141141
import { HttpStatus } from '@tkrotoff/fetch';
142142

143143
console.log(HttpStatus._201_Created);
@@ -152,7 +152,7 @@ const status: HttpStatusEnum = HttpStatus._200_OK;
152152

153153
@tkrotoff/fetch exposes `defaults.init` that will be applied to every request.
154154

155-
```JavaScript
155+
```TypeScript
156156
import { defaults } from '@tkrotoff/fetch';
157157

158158
defaults.init.mode = 'cors';
@@ -163,9 +163,16 @@ defaults.init.credentials = 'include';
163163

164164
When testing your code, use `createResponsePromise()` and `createJSONResponsePromise()`:
165165

166-
```JavaScript
166+
```TypeScript
167167
import * as Http from '@tkrotoff/fetch';
168168

169+
// https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-968853688
170+
// https://github.com/swc-project/swc/issues/5059
171+
jest.mock('@tkrotoff/fetch', () => ({
172+
__esModule: true,
173+
...jest.requireActual('@tkrotoff/fetch')
174+
}));
175+
169176
test('OK', async () => {
170177
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
171178
Http.createResponsePromise('test')
@@ -197,4 +204,50 @@ test('fail', async () => {
197204
});
198205
```
199206

207+
Other possible syntax with `jest.mock` instead of `jest.spyOn`:
208+
209+
```TypeScript
210+
import { createResponsePromise, get } from '@tkrotoff/fetch';
211+
212+
beforeEach(() => jest.resetAllMocks());
213+
214+
jest.mock('@tkrotoff/fetch', () => ({
215+
...jest.requireActual('@tkrotoff/fetch'),
216+
get: jest.fn(),
217+
post: jest.fn(),
218+
postJSON: jest.fn(),
219+
put: jest.fn(),
220+
putJSON: jest.fn(),
221+
patch: jest.fn(),
222+
patchJSON: jest.fn(),
223+
del: jest.fn()
224+
}));
225+
226+
test('OK', async () => {
227+
jest.mocked(get).mockImplementation(() =>
228+
createResponsePromise('test')
229+
);
230+
231+
const response = await get(url).text();
232+
expect(response).toEqual('test');
233+
234+
expect(get).toHaveBeenCalledTimes(1);
235+
expect(get).toHaveBeenCalledWith(url);
236+
});
237+
238+
test('fail', async () => {
239+
jest.mocked(get).mockImplementation(() =>
240+
createResponsePromise(
241+
'<!DOCTYPE html><title>404</title>',
242+
{ status: 404, statusText: 'Not Found' }
243+
)
244+
);
245+
246+
await expect(get(url).text()).rejects.toThrow('Not Found');
247+
248+
expect(get).toHaveBeenCalledTimes(1);
249+
expect(get).toHaveBeenCalledWith(url);
250+
});
251+
```
252+
200253
Check [examples/node](examples/node) and [examples/web](examples/web).

examples/node/jest.config.js

+6
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/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"dependencies": {
1717
"@tkrotoff/fetch": "file:../..",
1818
"abort-controller": "^3.0.0",
19-
"node-fetch": "^3.2.6",
20-
"ts-node": "^10.8.1",
21-
"typescript": "^4.7.4"
19+
"node-fetch": "^3.3.0",
20+
"ts-node": "^10.9.1",
21+
"typescript": "^4.9.3"
2222
},
2323
"devDependencies": {
24-
"@babel/preset-typescript": "^7.17.12",
25-
"jest": "^28.1.1"
24+
"@babel/preset-typescript": "^7.18.6",
25+
"jest": "^29.3.1"
2626
}
2727
}

examples/node/requests.test.ts

+22-29
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 () => {
@@ -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
});

examples/node/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
"noFallthroughCasesInSwitch": true,
1616
"forceConsistentCasingInFileNames": true
1717
},
18-
"files": ["index.ts"]
18+
"include": ["**/*"]
1919
}

examples/web/jest.config.js

+6
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/package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
},
1616
"dependencies": {
1717
"@tkrotoff/fetch": "file:../..",
18-
"core-js": "^3.23.2",
18+
"core-js": "^3.26.1",
1919
"expect": "^25.5.0",
20-
"ua-parser-js": "^1.0.2",
20+
"ua-parser-js": "^1.0.32",
2121
"whatwg-fetch": "^3.6.2"
2222
},
2323
"devDependencies": {
24-
"@babel/core": "^7.18.5",
25-
"@babel/preset-env": "^7.18.2",
26-
"@babel/preset-typescript": "^7.17.12",
24+
"@babel/core": "^7.20.5",
25+
"@babel/preset-env": "^7.20.2",
26+
"@babel/preset-typescript": "^7.18.6",
2727
"@types/ua-parser-js": "^0.7.36",
28-
"babel-loader": "^8.2.5",
29-
"jest": "^28.1.1",
30-
"typescript": "^4.7.4",
31-
"webpack": "^5.73.0",
32-
"webpack-cli": "^4.10.0",
33-
"webpack-dev-server": "^4.9.2"
28+
"babel-loader": "^9.1.0",
29+
"jest": "^29.3.1",
30+
"typescript": "^4.9.3",
31+
"webpack": "^5.75.0",
32+
"webpack-cli": "^5.0.0",
33+
"webpack-dev-server": "^4.11.1"
3434
}
3535
}

0 commit comments

Comments
 (0)