Skip to content

Commit 782a8ed

Browse files
committed
extend jsdom testing environment with TextEncoder/TextDecoder
1 parent 8512e18 commit 782a8ed

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

.jest/dom-environment.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const JSDOMEnvironment = require('jest-environment-jsdom');
2+
3+
// TODO Node >= 8.3 includes the same TextEncoder and TextDecoder as exists in the browser, but they haven't yet been
4+
// added to jsdom. Until they are, we can do it ourselves. Once they do, this file can go away.
5+
6+
// see https://github.com/jsdom/jsdom/issues/2524 and https://nodejs.org/api/util.html#util_class_util_textencoder
7+
8+
module.exports = class DOMEnvironment extends JSDOMEnvironment {
9+
async setup() {
10+
await super.setup();
11+
if (typeof this.global.TextEncoder === 'undefined') {
12+
const { TextEncoder, TextDecoder } = require('util');
13+
this.global.TextEncoder = TextEncoder;
14+
this.global.TextDecoder = TextDecoder;
15+
}
16+
}
17+
};

packages/gatsby/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"ts",
7777
"tsx"
7878
],
79-
"testEnvironment": "jsdom",
79+
"testEnvironment": "../../.jest/dom-environment",
8080
"testMatch": [
8181
"**/*.test.ts",
8282
"**/*.test.tsx"

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"ts",
9191
"tsx"
9292
],
93-
"testEnvironment": "jsdom",
93+
"testEnvironment": "../../.jest/dom-environment",
9494
"testMatch": [
9595
"**/*.test.ts",
9696
"**/*.test.tsx"

0 commit comments

Comments
 (0)