-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjest.setup.ts
More file actions
51 lines (43 loc) · 1.6 KB
/
Copy pathjest.setup.ts
File metadata and controls
51 lines (43 loc) · 1.6 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
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom/extend-expect";
import { Crypto } from "@peculiar/webcrypto";
// https://github.com/zpao/qrcode.react/issues/134
jest.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation();
import { TextEncoder, TextDecoder } from "util";
Object.assign(global, {
TextDecoder,
TextEncoder,
});
// Add fetch polyfill for Headers support
import fetch, { Headers, Request, Response } from "node-fetch";
if (!globalThis.fetch) {
globalThis.fetch = fetch as any;
globalThis.Headers = Headers as any;
globalThis.Request = Request as any;
globalThis.Response = Response as any;
}
// Polyfill Web Crypto
const cryptoInstance = new Crypto();
(globalThis as any).crypto = cryptoInstance;
(global as any).crypto.subtle = cryptoInstance.subtle;
// Add setImmediate polyfill for Node.js compatibility
if (!globalThis.setImmediate) {
(globalThis as any).setImmediate = (callback: (...args: any[]) => void, ...args: any[]) => {
return setTimeout(callback, 0, ...args);
};
}
// Fix Uint8Array instanceof issues with Buffer in jsdom
// Override Uint8Array Symbol.hasInstance to recognize Buffers
Object.defineProperty(Uint8Array, Symbol.hasInstance, {
value: function (obj: any) {
return (
ArrayBuffer.isView(obj) &&
(obj.constructor === Uint8Array ||
Buffer.isBuffer(obj) ||
Object.prototype.toString.call(obj) === "[object Uint8Array]")
);
},
});