Skip to content

Commit 07bb048

Browse files
committed
Some example tests added and basic unit tests architecture setup
added unit tests for JDK Downloader view
1 parent 3042721 commit 07bb048

File tree

13 files changed

+335
-83
lines changed

13 files changed

+335
-83
lines changed

vscode/.nycrc.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
{
22
"extends": "@istanbuljs/nyc-config-typescript",
3-
"reporter":[
3+
"reporter": [
44
"text",
55
"html",
66
"text-summary",
77
"cobertura"
88
],
9-
"all": true,
10-
"cache": false,
9+
"all": false,
1110
"include": [
12-
"src/**/*.ts", "out/**/*.js"
11+
"src/**/*.ts",
12+
"out/**/*.js"
1313
],
14-
"exclude": [ "src/test/**/*.ts", "out/test/**/*.js"]
14+
"exclude": [
15+
"src/test/**/*.ts",
16+
"out/test/**/*.js",
17+
"out/utils.js",
18+
"out/logger.js",
19+
"out/constants.js"
20+
]
1521
}

vscode/src/test/unit/mocks/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Important Note:
2+
Every entity exported from this directory should be prefixed with `Mock` or `Mocked` so that any developer doesn't import mocked VS Code or mocked elements in the extension main logic code.

vscode/src/test/unit/mocks/init.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { initMockedLocaliser } from "./mockLocaliser";
2+
import { initMockedVSCode } from "./vscode/mockVscode";
3+
4+
const Module = require('module');
5+
const originalLoad = Module._load;
6+
7+
export const initMocks = () => {
8+
const mockedVSCode = initMockedVSCode();
9+
const mockedLocaliser = initMockedLocaliser();
10+
11+
const mocks = {
12+
vscode: mockedVSCode,
13+
localiser: mockedLocaliser
14+
};
15+
16+
replaceImportsWithMocks(mocks);
17+
}
18+
19+
20+
const replaceImportsWithMocks = (mocks: any) => {
21+
Module._load = function (request: any, _parent: any) {
22+
if (request === 'vscode') {
23+
return mocks.vscode;
24+
} else if (request.includes('localiser')) {
25+
return mocks.localiser;
26+
}
27+
28+
if (/\.less$/.test(request)) {
29+
return;
30+
}
31+
return originalLoad.apply(this, arguments);
32+
};
33+
}
34+
35+
export function restore() {
36+
Module._load = originalLoad;
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const mockedL10n = {
2+
l10n: {
3+
value(key: string) {
4+
return key;
5+
},
6+
nbLocaleCode() {
7+
return 'en';
8+
},
9+
}
10+
};
11+
12+
export const initMockedLocaliser = () => {
13+
return mockedL10n;
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode';
2+
import { URI } from './uri';
3+
import { mockWindowNamespace } from './namespaces/window';
4+
import { mockedEnums } from './vscodeHostedTypes';
5+
6+
type VSCode = typeof vscode;
7+
const mockedVSCode: Partial<VSCode> = {};
8+
9+
const mockedVscodeClassesAndTypes = () => {
10+
mockedVSCode.Uri = URI as any;
11+
mockedVSCode.ViewColumn = mockedEnums.viewColumn;
12+
}
13+
14+
const mockNamespaces = () => {
15+
mockWindowNamespace(mockedVSCode);
16+
}
17+
18+
export const initMockedVSCode = () => {
19+
mockedVscodeClassesAndTypes();
20+
mockNamespaces();
21+
22+
return mockedVSCode;
23+
}
24+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
import { mock, when, anyString, anyOfClass, anything, instance } from "ts-mockito";
3+
4+
type VSCode = typeof vscode;
5+
6+
let mockedWindow: typeof vscode.window;
7+
export const mockWindowNamespace = (mockedVSCode: Partial<VSCode>) => {
8+
mockedWindow = mock<typeof vscode.window>();
9+
mockedVSCode.window = instance(mockedWindow);
10+
mockCreateWebViewPanel();
11+
mockCreateOutputChannel();
12+
mockMessageView();
13+
}
14+
15+
const mockCreateWebViewPanel = () => {
16+
const mockedWebviewPanel = mock<vscode.WebviewPanel>();
17+
when(mockedWindow.createWebviewPanel(
18+
anyString(),
19+
anyString(),
20+
anyOfClass(Number),
21+
anything()
22+
)).thenReturn(instance(mockedWebviewPanel));
23+
}
24+
25+
const mockCreateOutputChannel = () => {
26+
const mockedOutputChannel = mock<vscode.OutputChannel>();
27+
when(mockedWindow.createOutputChannel(
28+
anyString()
29+
)).thenReturn(instance(mockedOutputChannel));
30+
}
31+
32+
const mockMessageView = () => {
33+
when(mockedWindow.showErrorMessage(anyString())).thenReturn(Promise.resolve(anyString()));
34+
when(mockedWindow.showInformationMessage(anyString())).thenReturn(Promise.resolve(anyString()));
35+
when(mockedWindow.showWarningMessage(anyString())).thenReturn(Promise.resolve(anyString()));
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
enum ViewColumn {
2+
Active = -1,
3+
Beside = -2,
4+
One = 1,
5+
Two = 2,
6+
Three = 3,
7+
Four = 4,
8+
Five = 5,
9+
Six = 6,
10+
Seven = 7,
11+
Eight = 8,
12+
Nine = 9,
13+
}
14+
15+
export const mockedEnums = {
16+
viewColumn: ViewColumn
17+
}

vscode/src/test/unit/mocks/vscode/vscodeMock.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

vscode/src/test/unit/runTest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as Mocha from 'mocha';
22
import { glob } from 'glob';
33
import * as path from 'path';
4-
import { vscodeMockInit } from './mocks/vscode/vscodeMock';
5-
4+
import { initMocks } from './mocks/init';
65

76
const mocha = new Mocha({
87
ui: 'tdd',
9-
color: true
8+
color: true,
9+
timeout: 600 * 1000
1010
});
1111

1212

@@ -46,7 +46,7 @@ try {
4646
if (args.length) {
4747
console.log(`Running unit tests for following speicified modules: ${args.map(el => el)}`);
4848
}
49-
vscodeMockInit();
49+
initMocks();
5050
testRunner(args);
5151
} catch (err: any) {
5252
console.error("Exception occurred while running tests");

vscode/src/test/unit/testUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { LOGGER } from "../../logger";
2+
3+
export const getOsType = (): string => {
4+
switch (process.platform) {
5+
case "linux":
6+
return "linux";
7+
case "darwin":
8+
return "macOS";
9+
default:
10+
return "windows";
11+
}
12+
}
13+
14+
export const getMachineArch = (): string => {
15+
switch (process.arch) {
16+
case "arm64":
17+
return "aarch64";
18+
default:
19+
return "x64";
20+
}
21+
}
22+
23+
export const getHtmlTagContent = (html: string, tagName: string): string => {
24+
const regex = new RegExp(`<${tagName}[^>]*>(.*?)<\/${tagName}>`, 'is');
25+
const match = html.match(regex);
26+
return match?.[1]?.trim() || '';
27+
}
28+
29+
export const checkTagContentNotEmpty = (html: string, tagName: string): boolean => {
30+
const content = getHtmlTagContent(html, tagName);
31+
return content.length !== 0;
32+
}
33+
34+
export const enableMockedLoggers = (sandbox: sinon.SinonSandbox) => {
35+
sandbox.stub(LOGGER, 'log').callsFake((message) => {
36+
console.log(message);
37+
});
38+
sandbox.stub(LOGGER, 'error').callsFake((message) => {
39+
console.error(message);
40+
});
41+
sandbox.stub(LOGGER, 'warn').callsFake((message) => {
42+
console.warn(message);
43+
});
44+
}

vscode/src/test/unit/webviews/jdkDownloader.unit.test.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)