-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathcleanup.ts
39 lines (32 loc) · 1023 Bytes
/
cleanup.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
import { CleanupCallback } from '../types'
let cleanupCallbacks: Array<CleanupCallback> = []
async function cleanup() {
for (const callback of cleanupCallbacks) {
await callback()
}
cleanupCallbacks = []
}
function addCleanup(callback: CleanupCallback) {
cleanupCallbacks = [callback, ...cleanupCallbacks]
return () => removeCleanup(callback)
}
function removeCleanup(callback: CleanupCallback) {
cleanupCallbacks = cleanupCallbacks.filter((cb) => cb !== callback)
}
function skipAutoCleanup() {
try {
return !!process.env.RHTL_SKIP_AUTO_CLEANUP
} catch {
// falling back in the case that process.env.RHTL_SKIP_AUTO_CLEANUP cannot be accessed (e.g. browser environment)
return false
}
}
function autoRegisterCleanup() {
// Automatically registers cleanup in supported testing frameworks
if (typeof afterEach === 'function' && !skipAutoCleanup()) {
afterEach(async () => {
await cleanup()
})
}
}
export { cleanup, addCleanup, removeCleanup, autoRegisterCleanup }