-
Notifications
You must be signed in to change notification settings - Fork 3
/
jest.environment.ts
44 lines (35 loc) · 1.26 KB
/
jest.environment.ts
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
import { TestEnvironment as JsDomEnvironment } from "jest-environment-jsdom";
import nock from "nock";
export default class CustomEnvironment extends JsDomEnvironment {
async setup() {
await super.setup();
Object.assign(this.global, {
// These are available globally in the browser, but not in a default
// Jest/Node.js environment.
TextEncoder,
TextDecoder,
// jest@29 uses jsdom@20 which includes its own implementation of `AbortSignal`
// that does not implement `timeout`. This workaround can be removed when jest@30
// is released as it depends on jsdom@22 which _does_ implement `timeout`.
AbortSignal,
// JSDOM does not implement `fetch`, so we re-use Node's implementation.
fetch,
// Needed by Nock interceptors declared from within tests.
ReadableStream,
Request,
Response,
TransformStream,
});
// Make Node's `SubleCrypto` implementation available.
Object.assign(this.global.crypto, {
subtle: crypto.subtle,
});
// Start interceptring and disabling network requests now. This can't be done from
// within the JSDOM environment.
nock.disableNetConnect();
}
async teardown() {
await super.teardown();
nock.enableNetConnect();
}
}