Repository navigation
Expand file tree
/
Copy pathjest.setup.localstorage.js
More file actions
60 lines (55 loc) · 1.84 KB
/
Copy pathjest.setup.localstorage.js
File metadata and controls
60 lines (55 loc) · 1.84 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
// Jest polyfills for global environments that rely on `window`/`localStorage`/`TextDecoder`.
// Shared between repository-level and validation-suite configurations.
/* global window */
(function ensureEnvironmentPolyfills() {
if (typeof window === "undefined") {
global.window = {}; // minimal window object if absent
}
if (typeof globalThis.localStorage === "undefined") {
const store = new Map();
globalThis.localStorage = {
getItem: (key) => (store.has(key) ? store.get(key) : null),
setItem: (key, value) => store.set(String(key), String(value)),
removeItem: (key) => store.delete(key),
clear: () => store.clear(),
key: (i) => Array.from(store.keys())[i] || null,
get length() {
return store.size;
},
};
}
if (
typeof global.TextDecoder === "undefined" ||
typeof global.TextEncoder === "undefined"
) {
try {
const { TextDecoder, TextEncoder } = require("util");
if (typeof global.TextDecoder === "undefined" && TextDecoder) {
global.TextDecoder = TextDecoder;
}
if (typeof global.TextEncoder === "undefined" && TextEncoder) {
global.TextEncoder = TextEncoder;
}
if (typeof window.TextDecoder === "undefined" && global.TextDecoder) {
window.TextDecoder = global.TextDecoder;
}
if (typeof window.TextEncoder === "undefined" && global.TextEncoder) {
window.TextEncoder = global.TextEncoder;
}
} catch (e) {
// util should exist; if it does not, tests will fail explicitly.
}
}
})();
try {
window.localStorage.setItem("__jest_localstorage_sanity__", "ok");
} catch (e) {
// swallow errors to avoid breaking test bootstrapping.
}
try {
if (typeof TextDecoder === "function") {
new TextDecoder("utf-8");
}
} catch (_) {
// no-op: this is a non-fatal availability check.
}