/
mocks.ts
42 lines (37 loc) · 1.13 KB
/
mocks.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
import { testing } from 'bs-logger'
import { rootLogger } from '../util/logger'
export const logTargetMock = () => (rootLogger as testing.LoggerMock).target
export const mockObject = <T, M>(obj: T, newProps: M): T & M & { mockRestore: () => T } => {
const backup: any = Object.create(null)
Object.keys(newProps).forEach(key => {
const desc = (backup[key] = Object.getOwnPropertyDescriptor(obj, key))
const newDesc: any = { ...desc }
if (newDesc.get) {
newDesc.get = () => (newProps as any)[key]
} else {
newDesc.value = (newProps as any)[key]
}
Object.defineProperty(obj, key, newDesc)
})
if ((obj as any).mockRestore) backup.mockRestore = Object.getOwnPropertyDescriptor(obj, 'mockRestore')
return Object.defineProperty(obj, 'mockRestore', {
value() {
Object.keys(backup).forEach(key => {
Object.defineProperty(obj, key, backup[key])
})
return obj
},
configurable: true,
})
}
export const mockWriteStream = () => {
return {
written: [] as string[],
write(text: string) {
this.written.push(text)
},
clear() {
this.written = []
},
}
}