Skip to content

Commit d70e06f

Browse files
committed
Use standard APIs available in modern Node.js and remove dev dependencies.
1 parent 8209803 commit d70e06f

15 files changed

+68
-109
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Major
66

77
- Updated Node.js support to `^18.18.0 || ^20.9.0 || >=21.1.0`.
8+
- Refactored tests to use the standard `AbortController`, `AbortSignal`, `Event`, `EventTarget`, `File`, `FormData`, and `Response` APIs available in modern Node.js and removed the dev dependencies [`abort-controller`](https://npm.im/abort-controller), [`event-target-shim`](https://npm.im/event-target-shim), and [`node-fetch`](https://npm.im/node-fetch).
89

910
### Patch
1011

fetchGraphQL.test.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-check
22

33
import { deepStrictEqual, strictEqual } from "node:assert";
4-
import { Response } from "node-fetch";
54
import revertableGlobals from "revertable-globals";
65

76
import fetchGraphQL from "./fetchGraphQL.mjs";

fetchOptionsGraphQL.test.mjs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// @ts-check
22

33
import { deepStrictEqual, strictEqual } from "node:assert";
4-
import { File, FormData } from "node-fetch";
5-
import revertableGlobals from "revertable-globals";
64

75
import fetchOptionsGraphQL from "./fetchOptionsGraphQL.mjs";
86
import assertBundleSize from "./test/assertBundleSize.mjs";
@@ -32,35 +30,29 @@ export default (tests) => {
3230
});
3331

3432
tests.add("`fetchOptionsGraphQL` with files.", () => {
35-
const revertGlobals = revertableGlobals({ File, FormData });
36-
37-
try {
38-
const fileName = "a.txt";
39-
const options = fetchOptionsGraphQL({
40-
query: "",
41-
variables: { a: new File(["a"], fileName) },
42-
});
33+
const fileName = "a.txt";
34+
const options = fetchOptionsGraphQL({
35+
query: "",
36+
variables: { a: new File(["a"], fileName) },
37+
});
4338

44-
// See the GraphQL multipart request spec:
45-
// https://github.com/jaydenseric/graphql-multipart-request-spec
39+
// See the GraphQL multipart request spec:
40+
// https://github.com/jaydenseric/graphql-multipart-request-spec
4641

47-
strictEqual(options.method, "POST");
48-
deepStrictEqual(options.headers, { Accept: "application/json" });
49-
assertInstanceOf(options.body, FormData);
42+
strictEqual(options.method, "POST");
43+
deepStrictEqual(options.headers, { Accept: "application/json" });
44+
assertInstanceOf(options.body, FormData);
5045

51-
const formDataEntries = Array.from(options.body.entries());
46+
const formDataEntries = Array.from(options.body.entries());
5247

53-
strictEqual(formDataEntries.length, 3);
54-
deepStrictEqual(formDataEntries[0], [
55-
"operations",
56-
'{"query":"","variables":{"a":null}}',
57-
]);
58-
deepStrictEqual(formDataEntries[1], ["map", '{"1":["variables.a"]}']);
59-
strictEqual(formDataEntries[2][0], "1");
60-
assertInstanceOf(formDataEntries[2][1], File);
61-
strictEqual(formDataEntries[2][1].name, fileName);
62-
} finally {
63-
revertGlobals();
64-
}
48+
strictEqual(formDataEntries.length, 3);
49+
deepStrictEqual(formDataEntries[0], [
50+
"operations",
51+
'{"query":"","variables":{"a":null}}',
52+
]);
53+
deepStrictEqual(formDataEntries[1], ["map", '{"1":["variables.a"]}']);
54+
strictEqual(formDataEntries[2][0], "1");
55+
assertInstanceOf(formDataEntries[2][1], File);
56+
strictEqual(formDataEntries[2][1].name, fileName);
6557
});
6658
};

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,13 @@
109109
"@types/react": "^18.0.17",
110110
"@types/react-dom": "^18.0.6",
111111
"@types/react-test-renderer": "^18.0.0",
112-
"abort-controller": "^3.0.0",
113112
"coverage-node": "^8.0.0",
114113
"esbuild": "^0.15.5",
115114
"eslint": "^8.23.0",
116115
"eslint-plugin-react-hooks": "^4.6.0",
117116
"eslint-plugin-simple-import-sort": "^7.0.0",
118-
"event-target-shim": "^6.0.2",
119117
"filter-console": "^1.0.0",
120118
"gzip-size": "^7.0.0",
121-
"node-fetch": "^3.2.10",
122119
"prettier": "^2.7.1",
123120
"react": "^18.2.0",
124121
"react-dom": "^18.2.0",

test.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

3-
import "./test/polyfills.mjs";
3+
import "./test/polyfillCustomEvent.mjs";
4+
import "./test/polyfillFile.mjs";
45

56
import TestDirector from "test-director";
67

test/polyfillCustomEvent.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-check
2+
3+
// TODO: Delete this polyfill once all supported Node.js versions have the
4+
// global `CustomEvent`:
5+
// https://nodejs.org/api/globals.html#customevent
6+
globalThis.CustomEvent ??=
7+
/**
8+
* `CustomEvent` polyfill.
9+
* @template [T=unknown]
10+
* @type {globalThis.CustomEvent<T>}
11+
*/
12+
class CustomEvent extends Event {
13+
/**
14+
* @param {string} type Event type.
15+
* @param {CustomEventInit<T>} [options] Custom event options.
16+
*/
17+
constructor(type, options = {}) {
18+
// Workaround a TypeScript bug:
19+
// https://github.com/microsoft/TypeScript/issues/50286
20+
const { detail, ...eventOptions } = options;
21+
super(type, eventOptions);
22+
if (detail) this.detail = detail;
23+
}
24+
25+
/** @deprecated */
26+
initCustomEvent() {}
27+
};

test/polyfillFile.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-check
2+
3+
import { File as NodeFile } from "node:buffer";
4+
5+
// TODO: Delete this polyfill once all supported Node.js versions have the
6+
// global `File`:
7+
// https://nodejs.org/api/globals.html#class-file
8+
// @ts-expect-error It’s not a perfect polyfill, but works for the tests.
9+
globalThis.File ??= NodeFile;

test/polyfills.mjs

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/polyfills/AbortController.mjs

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/polyfills/AbortSignal.mjs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)