Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/mock.ts
5237 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
import { DeepPartial } from '../../common/types.js';
8
9
export interface Ctor<T> {
10
new(): T;
11
}
12
13
export function mock<T>(): Ctor<T> {
14
// eslint-disable-next-line local/code-no-any-casts
15
return function () { } as any;
16
}
17
18
export type MockObject<T, ExceptProps = never> = { [K in keyof T]: K extends ExceptProps ? T[K] : SinonStub };
19
20
// Creates an object object that returns sinon mocks for every property. Optionally
21
// takes base properties.
22
export const mockObject = <T extends object>() => <TP extends Partial<T> = {}>(properties?: TP): MockObject<T, keyof TP> => {
23
// eslint-disable-next-line local/code-no-any-casts
24
return new Proxy({ ...properties } as any, {
25
get(target, key) {
26
if (!target.hasOwnProperty(key)) {
27
target[key] = stub();
28
}
29
30
return target[key];
31
},
32
set(target, key, value) {
33
target[key] = value;
34
return true;
35
},
36
});
37
};
38
39
/**
40
* Shortcut for type-safe partials in mocks. A shortcut for `obj as Partial<T> as T`.
41
*/
42
export function upcastPartial<T>(partial: Partial<T>): T {
43
return partial as T;
44
}
45
export function upcastDeepPartial<T>(partial: DeepPartial<T>): T {
46
return partial as T;
47
}
48
49