-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathcustom-environment.js
More file actions
62 lines (56 loc) · 2.23 KB
/
custom-environment.js
File metadata and controls
62 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//
import { TestEnvironment } from 'jest-environment-jsdom';
import { TextDecoder, TextEncoder } from 'util';
// This class registers various globals coming from node in test environments.
export default class CustomTestEnvironment extends TestEnvironment {
async setup() {
await super.setup();
// Register TextDecoder and TextEncoder with the global scope.
// These are now available globally in nodejs, but not when running with jsdom
// in jest apparently.
// Still let's double check that they're from the global scope as expected, so
// that this can be removed once it's implemented.
if ('TextDecoder' in this.global) {
throw new Error(
'TextDecoder is already present in the global scope, please update custom-environment.js.'
);
}
this.global.TextDecoder = TextDecoder;
this.global.TextEncoder = TextEncoder;
// Register Request and friends with the global scope.
// These are now available globally in nodejs, but not when running with jsdom
// in jest apparently.
// Still let's double check that they're from the global scope as expected, so
// that this can be removed once it's implemented.
if ('Request' in this.global) {
throw new Error(
'Request is already present in the global scope, please update custom-environment.js.'
);
}
this.global.fetch = fetch;
this.global.Request = Request;
this.global.Response = Response;
this.global.ReadableStream = ReadableStream;
this.global.CompressionStream = CompressionStream;
this.global.DecompressionStream = DecompressionStream;
// Mock window.matchMedia for dark mode tests
Object.defineProperty(this.global, 'matchMedia', {
writable: true,
value: (query) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => {
return true;
},
}),
});
}
}