-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup-jest.ts
More file actions
27 lines (24 loc) · 970 Bytes
/
setup-jest.ts
File metadata and controls
27 lines (24 loc) · 970 Bytes
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
// setup-jest.js
import {
TextDecoder as UtilTextDecoder,
TextEncoder as UtilTextEncoder,
} from "node:util";
import sanitizeHtml from "sanitize-html";
// Monkey-patch jsdom to make it support innerText
// see https://github.com/jsdom/jsdom/issues/1245#issuecomment-470192636
Object.defineProperty(Element.prototype, "innerText", {
get(this: Element) {
return sanitizeHtml(this.textContent ?? "", {
allowedTags: [], // remove all tags and return text content only
allowedAttributes: {}, // remove all tags and return text content only
});
},
set(this: Element, text: string) {
this.textContent = text;
},
configurable: true, // make it so that it doesn't blow chunks on re-running tests with things like --watch
});
// Monkey-patch jsdom to support TextEncoder/TextDecoder
// Node.js provides these globals; older jsdom/test setups may not.
globalThis.TextEncoder ??= UtilTextEncoder;
globalThis.TextDecoder ??= UtilTextDecoder;