Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/mock.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { SinonStub, stub } from 'sinon';
7
8
export interface Ctor<T> {
9
new(): T;
10
}
11
12
export function mock<T>(): Ctor<T> {
13
return function () { } as any;
14
}
15
16
export type MockObject<T, ExceptProps = never> = { [K in keyof T]: K extends ExceptProps ? T[K] : SinonStub };
17
18
// Creates an object object that returns sinon mocks for every property. Optionally
19
// takes base properties.
20
export const mockObject = <T extends object>() => <TP extends Partial<T> = {}>(properties?: TP): MockObject<T, keyof TP> => {
21
return new Proxy({ ...properties } as any, {
22
get(target, key) {
23
if (!target.hasOwnProperty(key)) {
24
target[key] = stub();
25
}
26
27
return target[key];
28
},
29
set(target, key, value) {
30
target[key] = value;
31
return true;
32
},
33
});
34
};
35
36