24 lines
745 B
TypeScript
24 lines
745 B
TypeScript
/**
|
||
* frontend/vitest.setup.ts
|
||
* 用途:为 jsdom 测试环境补齐 Web Crypto(subtle / randomUUID),与浏览器行为对齐。
|
||
* 依赖:node:crypto webcrypto。
|
||
* 逻辑:vitest.config 通过 setupFiles 注入;供 XFA 路径哈希等单测使用。
|
||
*/
|
||
import { webcrypto } from 'node:crypto';
|
||
|
||
/**
|
||
* 将 Node webcrypto 挂到 globalThis.crypto,避免 jsdom 缺少 subtle 导致 async 哈希挂起或失败。
|
||
*/
|
||
function ensureWebCrypto(): void {
|
||
const target = globalThis as typeof globalThis & { crypto?: Crypto };
|
||
if (!target.crypto?.subtle?.digest) {
|
||
Object.defineProperty(target, 'crypto', {
|
||
value: webcrypto as Crypto,
|
||
configurable: true,
|
||
writable: true,
|
||
});
|
||
}
|
||
}
|
||
|
||
ensureWebCrypto();
|