Skip to content

Commit 74748aa

Browse files
committed
Convert a bunch more O# tests that use options to jest
1 parent 031eadb commit 74748aa

File tree

15 files changed

+289
-601
lines changed

15 files changed

+289
-601
lines changed

omnisharptest/omnisharpJestTests/features/reportIssue.test.ts

Lines changed: 54 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,33 @@ import reportIssue from '../../../src/shared/reportIssue';
88
import { FakeMonoResolver, fakeMonoInfo } from '../../omnisharpUnitTests/fakes/fakeMonoResolver';
99
import { FakeDotnetResolver } from '../../omnisharpUnitTests/fakes/fakeDotnetResolver';
1010
import { DotnetInfo } from '../../../src/shared/utils/dotnetInfo';
11-
import { getFakeVsCode } from '../../../test/unitTests/fakes';
12-
13-
jest.resetAllMocks();
14-
jest.mock('vscode', () => ({
15-
workspace: {
16-
getConfiguration: jest.fn((_) => {
17-
return {
18-
get: jest.fn((_) => {
19-
console.log('test1');
20-
return undefined;
21-
}),
22-
};
23-
}),
24-
executeCommand: jest.fn((command: string, ...rest: any[]) => {
25-
console.log('test2');
26-
return undefined;
27-
}),
28-
},
29-
extensions: {
30-
all: [
31-
{
32-
packageJSON: {
33-
name: 'name1',
34-
publisher: 'publisher1',
35-
version: 'version1',
36-
isBuiltin: true,
37-
},
38-
id: 'id1',
39-
extensionPath: 'c:/extensions/abc-x64',
40-
},
41-
{
42-
packageJSON: {
43-
name: 'name2',
44-
publisher: 'publisher2',
45-
version: 'version2',
46-
isBuiltin: false,
47-
},
48-
id: 'id2',
49-
extensionPath: 'c:/extensions/xyz-x64',
50-
},
51-
],
52-
},
53-
}));
11+
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
5412

5513
describe(`${reportIssue.name}`, () => {
5614
const vscodeVersion = 'myVersion';
5715
const csharpExtVersion = 'csharpExtVersion';
5816
const isValidForMono = true;
17+
const extension1 = {
18+
packageJSON: {
19+
name: 'name1',
20+
publisher: 'publisher1',
21+
version: 'version1',
22+
isBuiltin: true,
23+
},
24+
id: 'id1',
25+
extensionPath: 'c:/extensions/abc-x64',
26+
} as vscode.Extension<any>;
27+
28+
const extension2 = {
29+
packageJSON: {
30+
name: 'name2',
31+
publisher: 'publisher2',
32+
version: 'version2',
33+
isBuiltin: false,
34+
},
35+
id: 'id2',
36+
extensionPath: 'c:/extensions/xyz-x64',
37+
} as vscode.Extension<any>;
5938

6039
const fakeDotnetInfo: DotnetInfo = {
6140
FullInfo: 'myDotnetInfo',
@@ -70,134 +49,69 @@ describe(`${reportIssue.name}`, () => {
7049
let issueBody: string;
7150

7251
beforeEach(() => {
73-
//jest.resetAllMocks();
74-
// jest.mock('vscode', () => ({
75-
// workspace: {
76-
// getConfiguration: jest.fn((_) => {
77-
// return {
78-
// get: jest.fn((_) => {
79-
// console.log('test1');
80-
// return undefined;
81-
// }),
82-
// };
83-
// }),
84-
// executeCommand: jest.fn((command: string, ...rest: any[]) => {
85-
// issueBody = rest[0].issueBody;
86-
// return undefined;
87-
// }),
88-
// },
89-
// extensions: {
90-
// all: [
91-
// {
92-
// packageJSON: {
93-
// name: 'name1',
94-
// publisher: 'publisher1',
95-
// version: 'version1',
96-
// isBuiltin: true,
97-
// },
98-
// id: 'id1',
99-
// extensionPath: 'c:/extensions/abc-x64',
100-
// },
101-
// {
102-
// packageJSON: {
103-
// name: 'name2',
104-
// publisher: 'publisher2',
105-
// version: 'version2',
106-
// isBuiltin: false,
107-
// },
108-
// id: 'id2',
109-
// extensionPath: 'c:/extensions/xyz-x64',
110-
// },
111-
// ],
112-
// },
113-
// }));
52+
jest.spyOn(vscode.workspace, 'getConfiguration').mockReturnValue({
53+
get: jest.fn((_config: string) => {
54+
return undefined;
55+
}),
56+
has: jest.fn(),
57+
inspect: jest.fn(),
58+
update: jest.fn(),
59+
} as vscode.WorkspaceConfiguration);
60+
61+
jest.spyOn(vscode.commands, 'executeCommand').mockImplementation(async (command: string, ...rest: any[]) => {
62+
issueBody = rest[0].issueBody;
63+
return {} as any;
64+
});
65+
jest.replaceProperty(vscode, 'extensions', {
66+
all: [extension1, extension2],
67+
getExtension: jest.fn(),
68+
onDidChange: jest.fn(),
69+
} as typeof vscode.extensions);
11470

11571
fakeMonoResolver = new FakeMonoResolver();
11672
fakeDotnetResolver = new FakeDotnetResolver();
11773
});
11874

11975
describe('The body is passed to the vscode clipboard and', () => {
12076
test('it contains the vscode version', async () => {
121-
await reportIssue(
122-
vscode,
123-
csharpExtVersion,
124-
getDotnetInfo,
125-
isValidForMono,
126-
fakeDotnetResolver,
127-
fakeMonoResolver
128-
);
77+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
12978
expect(issueBody).toContain(`**VSCode version**: ${vscodeVersion}`);
13079
});
13180

13281
test('it contains the csharp extension version', async () => {
133-
await reportIssue(
134-
vscode,
135-
csharpExtVersion,
136-
getDotnetInfo,
137-
isValidForMono,
138-
fakeDotnetResolver,
139-
fakeMonoResolver
140-
);
82+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
14183
expect(issueBody).toContain(`**C# Extension**: ${csharpExtVersion}`);
14284
});
14385

14486
test('it contains dotnet info', async () => {
145-
await reportIssue(
146-
vscode,
147-
csharpExtVersion,
148-
getDotnetInfo,
149-
isValidForMono,
150-
fakeDotnetResolver,
151-
fakeMonoResolver
152-
);
87+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
15388
expect(issueBody).toContain(fakeDotnetInfo.FullInfo);
15489
});
15590

15691
test('mono information is obtained when it is a valid mono platform', async () => {
157-
await reportIssue(
158-
vscode,
159-
csharpExtVersion,
160-
getDotnetInfo,
161-
isValidForMono,
162-
fakeDotnetResolver,
163-
fakeMonoResolver
164-
);
92+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
16593
expect(fakeMonoResolver.getMonoCalled).toEqual(true);
16694
});
16795

16896
test('mono version is put in the body when it is a valid mono platform', async () => {
169-
await reportIssue(
170-
vscode,
171-
csharpExtVersion,
172-
getDotnetInfo,
173-
isValidForMono,
174-
fakeDotnetResolver,
175-
fakeMonoResolver
176-
);
97+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
17798
expect(fakeMonoResolver.getMonoCalled).toEqual(true);
178-
expect(issueBody).toEqual(fakeMonoInfo.version);
99+
expect(issueBody).toContain(fakeMonoInfo.version);
179100
});
180101

181102
test('mono information is not obtained when it is not a valid mono platform', async () => {
182-
await reportIssue(vscode, csharpExtVersion, getDotnetInfo, false, fakeDotnetResolver, fakeMonoResolver);
103+
await reportIssue(csharpExtVersion, getDotnetInfo, false, fakeDotnetResolver, fakeMonoResolver);
183104
expect(fakeMonoResolver.getMonoCalled).toEqual(false);
184105
});
185106

186107
test('The url contains the name, publisher and version for all the extensions that are not builtin', async () => {
187-
await reportIssue(
188-
vscode,
189-
csharpExtVersion,
190-
getDotnetInfo,
191-
isValidForMono,
192-
fakeDotnetResolver,
193-
fakeMonoResolver
194-
);
195-
expect(issueBody).toContain(getFakeVsCode().extensions.all[2].packageJSON.name);
196-
expect(issueBody).toContain(getFakeVsCode().extensions.all[2].packageJSON.publisher);
197-
expect(issueBody).toContain(getFakeVsCode().extensions.all[2].packageJSON.version);
198-
expect(issueBody).toContain(getFakeVsCode().extensions.all[1].packageJSON.name);
199-
expect(issueBody).toContain(getFakeVsCode().extensions.all[1].packageJSON.publisher);
200-
expect(issueBody).toContain(getFakeVsCode().extensions.all[1].packageJSON.version);
108+
await reportIssue(csharpExtVersion, getDotnetInfo, isValidForMono, fakeDotnetResolver, fakeMonoResolver);
109+
expect(issueBody).toContain(extension2.packageJSON.name);
110+
expect(issueBody).toContain(extension2.packageJSON.publisher);
111+
expect(issueBody).toContain(extension2.packageJSON.version);
112+
expect(issueBody).not.toContain(extension1.packageJSON.name);
113+
expect(issueBody).not.toContain(extension1.packageJSON.publisher);
114+
expect(issueBody).not.toContain(extension1.packageJSON.version);
201115
});
202116
});
203117
});

omnisharptest/omnisharpUnitTests/logging/informationMessageObserver.test.ts renamed to omnisharptest/omnisharpJestTests/informationMessageObserver.test.ts

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,56 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { InformationMessageObserver } from '../../../src/observers/informationMessageObserver';
7-
import { expect, should } from 'chai';
8-
import { getUnresolvedDependenices, updateConfig, getVSCodeWithConfig } from '../../../test/unitTests/fakes';
6+
import * as vscode from 'vscode';
7+
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
8+
import { InformationMessageObserver } from '../../src/observers/informationMessageObserver';
9+
import { getUnresolvedDependenices, getWorkspaceConfiguration } from '../../test/unitTests/fakes';
910
import { Subject, from as observableFrom } from 'rxjs';
1011
import { timeout } from 'rxjs/operators';
1112

12-
suite('InformationMessageObserver', () => {
13-
suiteSetup(() => should());
14-
13+
describe('InformationMessageObserver', () => {
1514
let doClickOk: () => void;
1615
let doClickCancel: () => void;
1716
let signalCommandDone: () => void;
1817
let commandDone: Promise<void> | undefined;
19-
const vscode = getVsCode();
2018
const optionObservable = new Subject<void>();
2119
let infoMessage: string | undefined;
2220
let invokedCommand: string | undefined;
2321
const observer: InformationMessageObserver = new InformationMessageObserver(vscode);
2422

25-
setup(() => {
23+
beforeEach(() => {
2624
infoMessage = undefined;
2725
invokedCommand = undefined;
2826
commandDone = new Promise<void>((resolve) => {
2927
signalCommandDone = () => {
3028
resolve();
3129
};
3230
});
31+
32+
jest.spyOn(vscode.workspace, 'getConfiguration').mockReturnValue(getWorkspaceConfiguration());
33+
jest.spyOn(vscode.window, 'showInformationMessage').mockImplementation(
34+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
35+
//@ts-ignore
36+
async <T>(message: string, ...items: T[]) => {
37+
infoMessage = message;
38+
return new Promise<T | undefined>((resolve) => {
39+
doClickCancel = () => {
40+
resolve(undefined);
41+
};
42+
43+
doClickOk = () => {
44+
resolve(items[0]);
45+
};
46+
47+
return undefined;
48+
});
49+
}
50+
);
51+
jest.spyOn(vscode.commands, 'executeCommand').mockImplementation(async (command: string, ..._: any[]) => {
52+
invokedCommand = command;
53+
signalCommandDone();
54+
return undefined;
55+
});
3356
});
3457

3558
[
@@ -38,75 +61,51 @@ suite('InformationMessageObserver', () => {
3861
expectedCommand: 'dotnet.restore.all',
3962
},
4063
].forEach((elem) => {
41-
suite(elem.event.constructor.name, () => {
42-
suite('Suppress Dotnet Restore Notification is true', () => {
43-
setup(() => {
44-
updateConfig(vscode, 'csharp', 'suppressDotnetRestoreNotification', true);
64+
describe(elem.event.constructor.name, () => {
65+
describe('Suppress Dotnet Restore Notification is true', () => {
66+
beforeEach(() => {
67+
vscode.workspace.getConfiguration().update('csharp.suppressDotnetRestoreNotification', true);
4568
optionObservable.next();
4669
});
4770

4871
test('The information message is not shown', () => {
4972
observer.post(elem.event);
50-
expect(infoMessage).to.be.undefined;
73+
expect(infoMessage).toBeUndefined();
5174
});
5275
});
5376

54-
suite('Suppress Dotnet Restore Notification is false', () => {
55-
setup(() => {
56-
updateConfig(vscode, 'csharp', 'suppressDotnetRestoreNotification', false);
77+
describe('Suppress Dotnet Restore Notification is false', () => {
78+
beforeEach(() => {
79+
vscode.workspace.getConfiguration().update('csharp.suppressDotnetRestoreNotification', false);
5780
optionObservable.next();
5881
});
5982

6083
test('The information message is shown', async () => {
6184
observer.post(elem.event);
62-
expect(infoMessage).to.not.be.empty;
85+
expect(infoMessage?.length).toBeGreaterThan(0);
6386
doClickOk();
6487
await commandDone;
65-
expect(invokedCommand).to.be.equal(elem.expectedCommand);
88+
expect(invokedCommand).toEqual(elem.expectedCommand);
6689
});
6790

6891
test('Given an information message if the user clicks Restore, the command is executed', async () => {
6992
observer.post(elem.event);
7093
doClickOk();
7194
await commandDone;
72-
expect(invokedCommand).to.be.equal(elem.expectedCommand);
95+
expect(invokedCommand).toEqual(elem.expectedCommand);
7396
});
7497

7598
test('Given an information message if the user clicks cancel, the command is not executed', async () => {
7699
observer.post(elem.event);
77100
doClickCancel();
78-
await expect(observableFrom(commandDone!).pipe(timeout(1)).toPromise()).to.be.rejected;
79-
expect(invokedCommand).to.be.undefined;
101+
await expect(observableFrom(commandDone!).pipe(timeout(1)).toPromise()).rejects.toThrow();
102+
expect(invokedCommand).toBeUndefined();
80103
});
81104
});
82105
});
83106
});
84107

85-
teardown(() => {
108+
afterEach(() => {
86109
commandDone = undefined;
87110
});
88-
89-
function getVsCode() {
90-
const vscode = getVSCodeWithConfig();
91-
vscode.window.showInformationMessage = async <T>(message: string, ...items: T[]) => {
92-
infoMessage = message;
93-
return new Promise<T | undefined>((resolve) => {
94-
doClickCancel = () => {
95-
resolve(undefined);
96-
};
97-
98-
doClickOk = () => {
99-
resolve(items[0]);
100-
};
101-
});
102-
};
103-
104-
vscode.commands.executeCommand = async (command: string, ..._: any[]) => {
105-
invokedCommand = command;
106-
signalCommandDone();
107-
return undefined;
108-
};
109-
110-
return vscode;
111-
}
112111
});

0 commit comments

Comments
 (0)