aboutsummaryrefslogtreecommitdiffstats
path: root/jest.polyfills.cjs
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-27 19:38:32 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-27 20:06:56 +0100
commitff79add1d59375817a10331a35458cca3c419cfe (patch)
tree5e678e2c3cd43ace5cb7fd27a22f6e8403ccd211 /jest.polyfills.cjs
parentf111685c5886f3e77edfd3621c98d8ac1b9bcce4 (diff)
test: add MSW to intercept GraphQL requests
Diffstat (limited to 'jest.polyfills.cjs')
-rw-r--r--jest.polyfills.cjs34
1 files changed, 34 insertions, 0 deletions
diff --git a/jest.polyfills.cjs b/jest.polyfills.cjs
new file mode 100644
index 0000000..4039ab5
--- /dev/null
+++ b/jest.polyfills.cjs
@@ -0,0 +1,34 @@
+/* eslint-disable */
+// jest.polyfills.js
+/**
+ * @note The block below contains polyfills for Node.js globals
+ * required for Jest to function when running JSDOM tests.
+ * These HAVE to be require's and HAVE to be in this exact
+ * order, since "undici" depends on the "TextEncoder" global API.
+ *
+ * Consider migrating to a more modern test runner if
+ * you don't want to deal with this.
+ *
+ * @see https://mswjs.io/docs/migrations/1.x-to-2.x#requestresponsetextencoder-is-not-defined-jest
+ */
+
+const { TextDecoder, TextEncoder } = require('node:util');
+
+Object.defineProperties(globalThis, {
+ TextDecoder: { value: TextDecoder },
+ TextEncoder: { value: TextEncoder },
+});
+
+const { Blob } = require('node:buffer');
+const { fetch, Headers, FormData, Request, Response } = require('undici');
+
+Object.defineProperties(globalThis, {
+ fetch: { value: fetch, writable: true },
+ Blob: { value: Blob },
+ Headers: { value: Headers },
+ FormData: { value: FormData },
+ Request: { value: Request },
+ Response: { value: Response },
+});
+
+// cSpell:ignore undici