MCPcopy Index your code
hub / github.com/DeepNotesApp/DeepNotes / createSmartMock

Function createSmartMock

packages/@stdlib/testing/src/smart-mock.ts:1–76  ·  view source on GitHub ↗
(arg?: (() => any) | object)

Source from the content-addressed store, hash-verified

1export function createSmartMock(arg?: (() => any) | object): any {
2 let obj: any;
3
4 if (typeof arg === 'function') {
5 obj = function (...args: any[]) {
6 // @ts-ignore
7 return arg(...args);
8 };
9 } else if ((typeof arg === 'object' && arg !== null) || arg === undefined) {
10 let fnResult: any;
11
12 obj = function () {
13 fnResult ??= createSmartMock();
14 return fnResult;
15 };
16 } else {
17 return arg;
18 }
19
20 const proxy = new Proxy(obj, {
21 apply(_target, thisArg, args) {
22 return obj.apply(thisArg, args as any);
23 },
24 construct(target, args) {
25 return obj.apply(target, args as any);
26 },
27 defineProperty(target, propertyKey, attributes) {
28 return Reflect.defineProperty(target, propertyKey, attributes);
29 },
30 deleteProperty(target, propertyKey) {
31 return Reflect.deleteProperty(target, propertyKey);
32 },
33 get(target, propertyKey, receiver) {
34 if (!(propertyKey in target)) {
35 if (propertyKey === 'then') {
36 (target as any)[propertyKey] = (onfulfilled: any) =>
37 Promise.resolve().then(onfulfilled);
38 } else {
39 (target as any)[propertyKey] = createSmartMock();
40 }
41 }
42
43 return Reflect.get(target, propertyKey, receiver);
44 },
45 getOwnPropertyDescriptor(target, propertyKey) {
46 return Reflect.getOwnPropertyDescriptor(target, propertyKey);
47 },
48 getPrototypeOf(target) {
49 return Reflect.getPrototypeOf(target);
50 },
51 has(target, propertyKey) {
52 return Reflect.has(target, propertyKey);
53 },
54 isExtensible(target) {
55 return Reflect.isExtensible(target);
56 },
57 ownKeys(target) {
58 return Reflect.ownKeys(target);
59 },
60 preventExtensions(target) {

Callers 4

smart-mock.spec.tsFile · 0.90
createInlineMockFunction · 0.90
getFunction · 0.85
setFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected