forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-vitest.ts
47 lines (37 loc) · 901 Bytes
/
setup-vitest.ts
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
import * as nodeCrypto from 'node:crypto'
import { vi } from 'vitest'
/**
* crypto
*/
if (!globalThis.crypto) {
vi.stubGlobal('crypto', nodeCrypto)
vi.stubGlobal('CryptoKey', nodeCrypto.webcrypto.CryptoKey)
}
/**
* Cache API
*/
type StoreMap = Map<string | Request, Response>
class MockCache {
name: string
store: StoreMap
constructor(name: string, store: StoreMap) {
this.name = name
this.store = store
}
async match(key: Request | string): Promise<Response | null> {
return this.store.get(key) || null
}
async keys() {
return this.store.keys()
}
async put(key: Request | string, response: Response): Promise<void> {
this.store.set(key, response)
}
}
const globalStore: Map<string | Request, Response> = new Map()
const caches = {
open: (name: string) => {
return new MockCache(name, globalStore)
},
}
vi.stubGlobal('caches', caches)