-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathMockUtils.test.ts
303 lines (263 loc) · 10.4 KB
/
MockUtils.test.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import { expect } from "chai";
import { stub } from "sinon";
import * as vscode from "vscode";
import * as fs from "fs/promises";
import {
AsyncEventEmitter,
mockFn,
mockGlobalEvent,
mockGlobalModule,
mockGlobalObject,
mockGlobalValue,
mockObject,
waitForReturnedPromises,
} from "../MockUtils";
import { Version } from "../../src/utilities/version";
import contextKeys from "../../src/contextKeys";
import configuration from "../../src/configuration";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function emptyFunction(..._: any): any {
// Intentionally empty
}
// A test suite for test code? Crazy, right?
suite("MockUtils Test Suite", () => {
suite("waitForReturnedPromises()", () => {
test("waits for all promises to complete before resolving", async () => {
const values: number[] = [];
const stubbedFn = stub<[number], Promise<void>>().callsFake(async num => {
await new Promise<void>(resolve => {
setTimeout(resolve, 1);
});
values.push(num);
});
stubbedFn(1);
stubbedFn(2);
stubbedFn(3);
expect(values).to.deep.equal([]);
await waitForReturnedPromises(stubbedFn);
expect(values).to.deep.equal([1, 2, 3]);
});
});
suite("mockObject()", () => {
test("can mock properties of an interface", () => {
interface TestInterface {
a: number;
b: string;
c: Version;
d(): string;
e(_: string): string;
}
const sut = mockObject<TestInterface>({
a: 5,
b: "this is a string",
c: new Version(6, 0, 0),
d: emptyFunction,
e: emptyFunction,
});
sut.d.returns("this is another string");
sut.e.callsFake(input => input + "this is yet another string");
expect(sut.a).to.equal(5);
expect(sut.b).to.equal("this is a string");
expect(sut.c).to.containSubset({ major: 6, minor: 0, patch: 0 });
expect(sut.d()).to.equal("this is another string");
expect(sut.e("a: ")).to.equal("a: this is yet another string");
});
test("can mock an interface with readonly properties", () => {
interface TestInterface {
readonly a: number;
}
const sut = mockObject<TestInterface>({
a: 0,
});
sut.a = 17;
expect(sut.a).to.equal(17);
});
test("can ommit properties from being mocked", () => {
interface TestInterface {
a: number;
b: string;
c: Version;
d(): string;
}
const sut = mockObject<TestInterface>({
a: 5,
});
expect(() => sut.d()).to.throw(
"Attempted to access property 'd', but it was not mocked"
);
});
test("can pass a mocked interface as a function parameter", () => {
interface TestInterface {
readonly a: number;
}
function testFn(arg: TestInterface): number {
return arg.a;
}
const sut = mockObject<TestInterface>({
a: 0,
});
expect(testFn(sut)).to.equal(0);
});
test("can mock a class", () => {
class TestClass {
a: number = 5;
b: string = "hello";
private c: string = "there";
d(): number {
return 5;
}
}
const sut = mockObject<TestClass>({
a: 5,
b: "this is a string",
d: emptyFunction,
});
sut.d.returns(6);
expect(sut.a).to.equal(5);
expect(sut.b).to.equal("this is a string");
expect(sut.d()).to.equal(6);
});
test("re-throws errors that occurred during the mocking process", () => {
interface TestInterface {
a: number;
}
const sut = mockObject<TestInterface>(
new Proxy(
{ a: 4 },
{
get() {
throw new Error("Cannot access this property");
},
}
)
);
expect(() => sut.a).to.throw("Cannot access this property");
});
});
suite("mockFn()", () => {
test("can fully mock a function inline", () => {
const fn: () => number = mockFn(s => s.returns(5));
expect(fn()).to.equal(5);
});
test("can be used with mockObject() to fully mock a function inline", () => {
interface TestInterface {
fn1(): string;
fn2(_: string): string;
}
const sut = mockObject<TestInterface>({
fn1: mockFn(s => s.returns("this is another string")),
fn2: mockFn(s => s.callsFake(input => input + "this is yet another string")),
});
expect(sut.fn1()).to.equal("this is another string");
expect(sut.fn2("a: ")).to.equal("a: this is yet another string");
});
test("retains type information when mocking a function", () => {
mockFn<() => number>(s => {
s.returns(
// @ts-expect-error - string is not compatible with number
"compiler error"
);
});
});
});
suite("mockGlobalObject()", () => {
const mockedWorkspace = mockGlobalObject(vscode, "workspace");
test("can mock the workspace object from the VSCode API", async () => {
mockedWorkspace.asRelativePath.returns("relative");
expect(vscode.workspace.asRelativePath("absolute")).to.equal("relative");
expect(mockedWorkspace.asRelativePath).to.have.been.calledOnceWithExactly("absolute");
});
});
suite("mockGlobalModule()", () => {
const mockedFS = mockGlobalModule(fs);
const mockedContextKeys = mockGlobalModule(contextKeys);
const mockedConfiguration = mockGlobalModule(configuration);
test("can mock the fs/promises module", async () => {
mockedFS.readFile.resolves("file contents");
await expect(fs.readFile("some_file")).to.eventually.equal("file contents");
expect(mockedFS.readFile).to.have.been.calledOnceWithExactly("some_file");
});
test("can mock the contextKeys module", () => {
// Initial value should match default value in context keys
expect(contextKeys.isActivated).to.be.false;
// Make sure that you can set the value of contextKeys using the mock
mockedContextKeys.isActivated = true;
expect(contextKeys.isActivated).to.be.true;
// Make sure that setting isActivated via contextKeys is also possible
contextKeys.isActivated = false;
expect(contextKeys.isActivated).to.be.false;
});
test("can mock the configuration module", () => {
expect(configuration.sdk).to.equal("");
// Make sure you can set a value using the mock
mockedConfiguration.sdk = "macOS";
expect(configuration.sdk).to.equal("macOS");
// Make sure you can set a value using the real module
configuration.sdk = "watchOS";
expect(configuration.sdk).to.equal("watchOS");
// Mocking objects within the configuration requires separate MockedObjects
const mockedLspConfig = mockObject<(typeof configuration)["lsp"]>(configuration.lsp);
mockedConfiguration.lsp = mockedLspConfig;
mockedLspConfig.disable = true;
expect(configuration.lsp.disable).to.be.true;
});
});
suite("mockGlobalValue()", () => {
const platform = mockGlobalValue(process, "platform");
test("can set the value of a global variable", () => {
platform.setValue("android");
expect(process.platform).to.equal("android");
});
});
suite("mockGlobalEvent()", () => {
const didCreateFiles = mockGlobalEvent(vscode.workspace, "onDidCreateFiles");
test("can trigger events from the VSCode API", async () => {
const listener = stub();
vscode.workspace.onDidCreateFiles(listener);
await didCreateFiles.fire({ files: [] });
expect(listener).to.have.been.calledOnceWithExactly({ files: [] });
});
});
suite("AsyncEventEmitter", () => {
test("waits for listener's promise to resolve before resolving fire()", async () => {
const events: number[] = [];
const sut = new AsyncEventEmitter<number>();
sut.event(async num => {
await new Promise<void>(resolve => {
setTimeout(resolve, 1);
});
events.push(num);
});
await sut.fire(1);
await sut.fire(2);
await sut.fire(3);
expect(events).to.deep.equal([1, 2, 3]);
});
test("event listeners can stop listening using the provided Disposable", async () => {
const listener1 = stub();
const listener2 = stub();
const listener3 = stub();
const sut = new AsyncEventEmitter<void>();
sut.event(listener1);
sut.event(listener2).dispose();
sut.event(listener3);
await sut.fire();
expect(listener1).to.have.been.calledOnce;
expect(listener2).to.not.have.been.called;
expect(listener3).to.have.been.calledOnce;
});
});
});