forked from 0xMiden/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
39 lines (35 loc) · 1.45 KB
/
Copy pathjest.setup.js
File metadata and controls
39 lines (35 loc) · 1.45 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
require('@testing-library/jest-dom');
const { Crypto, CryptoKey } = require('@peculiar/webcrypto');
const { TextEncoder, TextDecoder } = require('util');
// jsdom doesn't ship `TextEncoder`/`TextDecoder` on `global` so anything that
// calls `new TextEncoder()` at module scope blows up. Node's `util` has them.
if (typeof globalThis.TextEncoder === 'undefined') {
globalThis.TextEncoder = TextEncoder;
}
if (typeof globalThis.TextDecoder === 'undefined') {
globalThis.TextDecoder = TextDecoder;
}
let { db } = require('lib/miden/repo');
// jsdom installs its own `crypto` as a non-configurable getter on `globalThis`
// which only exposes `getRandomValues` / `randomUUID` — no `subtle`. We
// forcibly replace it with `@peculiar/webcrypto` so tests that exercise
// AES-GCM / PBKDF2 / SHA-256 can run. `Object.assign` silently no-ops against
// the jsdom getter, so we have to `defineProperty` with `configurable: true`.
const peculiarCrypto = new Crypto();
Object.defineProperty(globalThis, 'crypto', {
value: peculiarCrypto,
writable: true,
configurable: true
});
Object.defineProperty(globalThis, 'CryptoKey', {
value: CryptoKey,
writable: true,
configurable: true
});
global.afterEach(async () => {
// Some suites leave fake timers enabled. Dexie's IndexedDB cleanup relies on
// real scheduling and otherwise waits until Jest's hook timeout.
jest.useRealTimers();
// clear fake indexeddb database
await Promise.all(db.tables.map(t => t.clear()));
});