Skip to content

Commit b9288c3

Browse files
authored
Merge pull request #33 from tkrotoff/undici
Test with Undici fetch
2 parents 193b8ab + 37d4d24 commit b9288c3

File tree

11 files changed

+288
-84
lines changed

11 files changed

+288
-84
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.15.0 (2022/12/01)
2+
3+
- Breaking change: createHttpError & createJSONHttpError params are no longer optional
4+
- Test with Undici fetch
5+
16
## 0.14.2 (2022/12/01)
27

38
- Update npm packages

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ Check [examples/web](examples/web)
130130
- `createResponsePromise(body?:` [`BodyInit`](https://fetch.spec.whatwg.org/#bodyinit)`, init?:` [`ResponseInit`](https://fetch.spec.whatwg.org/#responseinit)`): ResponsePromiseWithBodyMethods`
131131
- `createJSONResponsePromise(body: object, init?: ResponseInit): ResponsePromiseWithBodyMethods`
132132

133-
- `createHttpError(body?: BodyInit, status = 0, statusText?: string): HttpError`
134-
- `createJSONHttpError(body: object, status = 0, statusText?: string): HttpError`
133+
- `createHttpError(body: BodyInit, status: number, statusText?: string): HttpError`
134+
- `createJSONHttpError(body: object, status: number, statusText?: string): HttpError`
135135

136136
### HttpStatus
137137

jest.setup.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ switch (fetchPolyfill) {
1010
case 'whatwg-fetch': {
1111
const whatwgFetch = require('whatwg-fetch');
1212
globalThis.fetch = whatwgFetch.fetch;
13-
// Tests are very slow with whatwg-fetch (150s) vs node-fetch (2s)
14-
jest.setTimeout(10_000);
13+
globalThis.Request = whatwgFetch.Request;
14+
globalThis.Response = whatwgFetch.Response;
15+
globalThis.Headers = whatwgFetch.Headers;
1516
break;
1617
}
1718
case 'node-fetch': {
1819
const nodeFetch = require('node-fetch');
1920
globalThis.fetch = nodeFetch.default;
21+
globalThis.Request = nodeFetch.Request;
2022
globalThis.Response = nodeFetch.Response;
2123
globalThis.Headers = nodeFetch.Headers;
2224
break;
2325
}
26+
case 'undici': {
27+
const undici = require('undici');
28+
globalThis.fetch = undici.fetch;
29+
globalThis.Request = undici.Request;
30+
globalThis.Response = undici.Response;
31+
globalThis.Headers = undici.Headers;
32+
break;
33+
}
2434
default: {
2535
assert(false, `Invalid fetch polyfill: '${fetchPolyfill}'`);
2636
}

package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"test:node-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=node-fetch jest --verbose",
3838
"test:coverage:node-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=node-fetch jest --coverage",
3939
"test:whatwg-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=whatwg-fetch jest --env=jsdom --verbose",
40+
"test:undici": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=undici jest --verbose",
4041
"test:coverage:whatwg-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=whatwg-fetch jest --env=jsdom --coverage",
4142
"test:e2e": "playwright test",
4243
"test:e2e:debug": "playwright test --debug",
@@ -89,6 +90,7 @@
8990
"tslib": "^2.4.1",
9091
"typescript": "^4.9.3",
9192
"ua-parser-js": "^1.0.32",
93+
"undici": "^5.13.0",
9294
"whatwg-fetch": "^3.6.2"
9395
}
9496
}

src/Http.test.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'node:assert';
22

33
import { createTestServer } from './createTestServer/createTestServer';
44
import { entriesToObject } from './utils/entriesToObject';
5-
import { isWhatwgFetch } from './utils/isWhatwgFetch';
65
import { defaults, del, get, patch, patchJSON, post, postJSON, put, putJSON } from './Http';
76

87
const path = '/';
@@ -480,7 +479,7 @@ describe('body methods', () => {
480479
const response = await get(url).blob();
481480
expect(response.size).toEqual(3);
482481
// FIXME https://github.com/jsdom/jsdom/issues/2555
483-
if (!isWhatwgFetch) {
482+
if (process.env.FETCH !== 'whatwg-fetch') {
484483
// eslint-disable-next-line jest/no-conditional-expect
485484
expect(await response.text()).toEqual('*/*');
486485
}
@@ -598,11 +597,24 @@ describe('body methods', () => {
598597
});
599598

600599
// https://github.com/whatwg/fetch/issues/1147
601-
// eslint-disable-next-line unicorn/consistent-function-scoping
602-
const nodeFetchBodyAlreadyUsedError = (url: string) => `body used already for: ${url}`;
603-
const whatwgFetchBodyAlreadyUsedError = 'Already read';
604-
const bodyAlreadyUsedError = (url: string) =>
605-
isWhatwgFetch ? whatwgFetchBodyAlreadyUsedError : nodeFetchBodyAlreadyUsedError(url);
600+
let bodyAlreadyUsedError = '';
601+
switch (process.env.FETCH) {
602+
case 'node-fetch': {
603+
bodyAlreadyUsedError = 'body used already for: ';
604+
break;
605+
}
606+
case 'whatwg-fetch': {
607+
bodyAlreadyUsedError = 'Already read';
608+
break;
609+
}
610+
case 'undici': {
611+
bodyAlreadyUsedError = 'Body is unusable';
612+
break;
613+
}
614+
default: {
615+
assert(false, `Unknown FETCH env '${process.env.FETCH}'`);
616+
}
617+
}
606618

607619
test('multiple body calls using helpers', async () => {
608620
const server = createTestServer();
@@ -614,7 +626,7 @@ describe('body methods', () => {
614626

615627
const response = get(url);
616628
expect(await response.json()).toEqual({ accept: 'application/json' });
617-
await expect(response.text()).rejects.toThrow(bodyAlreadyUsedError(url));
629+
await expect(response.text()).rejects.toThrow(bodyAlreadyUsedError);
618630

619631
// FIXME await close() is too slow with Fastify 4.10.2
620632
server.close();
@@ -630,7 +642,7 @@ describe('body methods', () => {
630642

631643
const response = await get(url);
632644
expect(await response.json()).toEqual({ accept: '*/*' });
633-
await expect(response.text()).rejects.toThrow(bodyAlreadyUsedError(url));
645+
await expect(response.text()).rejects.toThrow(bodyAlreadyUsedError);
634646

635647
// FIXME await close() is too slow with Fastify 4.10.2
636648
server.close();
@@ -647,7 +659,7 @@ describe('body methods', () => {
647659
const response = get(url);
648660
expect(await response.json()).toEqual({ accept: 'application/json' });
649661
// eslint-disable-next-line unicorn/no-await-expression-member
650-
await expect((await response).text()).rejects.toThrow(bodyAlreadyUsedError(url));
662+
await expect((await response).text()).rejects.toThrow(bodyAlreadyUsedError);
651663

652664
// FIXME await close() is too slow with Fastify 4.10.2
653665
server.close();
@@ -657,11 +669,24 @@ describe('body methods', () => {
657669
test('cannot connect', async () => {
658670
const url = 'http://localhost/';
659671

660-
const nodeFetchRequestFailedError = `request to ${url} failed, reason: connect ECONNREFUSED 127.0.0.1:80`;
661-
const whatwgFetchRequestFailedError = 'Network request failed';
662-
const requestFailedError = isWhatwgFetch
663-
? whatwgFetchRequestFailedError
664-
: nodeFetchRequestFailedError;
672+
let requestFailedError = '';
673+
switch (process.env.FETCH) {
674+
case 'node-fetch': {
675+
requestFailedError = `request to ${url} failed, reason: connect ECONNREFUSED 127.0.0.1:80`;
676+
break;
677+
}
678+
case 'whatwg-fetch': {
679+
requestFailedError = 'Network request failed';
680+
break;
681+
}
682+
case 'undici': {
683+
requestFailedError = 'fetch failed';
684+
break;
685+
}
686+
default: {
687+
assert(false, `Unknown FETCH env '${process.env.FETCH}'`);
688+
}
689+
}
665690

666691
// Avoid console to be polluted with whatwg-fetch "Error: connect ECONNREFUSED"
667692
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
@@ -671,12 +696,12 @@ test('cannot connect', async () => {
671696
} catch (e) {
672697
assert(e instanceof Error);
673698
/* eslint-disable jest/no-conditional-expect */
674-
expect(e.name).toEqual(isWhatwgFetch ? 'TypeError' : 'FetchError');
699+
expect(e.name).toEqual(process.env.FETCH === 'node-fetch' ? 'FetchError' : 'TypeError');
675700
expect(e.message).toEqual(requestFailedError);
676701
/* eslint-enable jest/no-conditional-expect */
677702
}
678703

679-
expect(consoleSpy).toHaveBeenCalledTimes(isWhatwgFetch ? 1 : 0);
704+
expect(consoleSpy).toHaveBeenCalledTimes(process.env.FETCH === 'whatwg-fetch' ? 1 : 0);
680705

681706
consoleSpy.mockRestore();
682707
});

0 commit comments

Comments
 (0)