-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathproxy.ts
38 lines (36 loc) · 938 Bytes
/
proxy.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
const fn = function () {};
function createMock(name: string, overrides: any = {}): any {
fn.prototype.name = name;
const props: any = {};
return new Proxy(fn, {
get(_target, prop) {
if (prop === "caller") {
return null;
}
if (prop === "__createMock__") {
return createMock;
}
if (prop === "__unenv__") {
return true;
}
if (prop in overrides) {
return overrides[prop];
}
// @ts-ignore
return (props[prop] =
props[prop] || createMock(`${name}.${prop.toString()}`));
},
apply(_target, _this, _args) {
return createMock(`${name}()`);
},
construct(_target, _args, _newT) {
return createMock(`[${name}]`) as object;
},
// @ts-ignore (ES6-only - removed in ES7)
// https://github.com/tc39/ecma262/issues/161
enumerate() {
return [];
},
});
}
export default createMock("mock");