Skip to content

Commit bc6c56b

Browse files
committed
Some example tests added and basic unit tests architecture setup
1 parent a2354a0 commit bc6c56b

File tree

11 files changed

+261
-77
lines changed

11 files changed

+261
-77
lines changed
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}
13+
14+
const mockCreateWebViewPanel = () => {
15+
const mockedWebviewPanel = mock<vscode.WebviewPanel>();
16+
when(mockedWindow.createWebviewPanel(
17+
anyString(),
18+
anyString(),
19+
anyOfClass(Number),
20+
anything()
21+
)).thenReturn(instance(mockedWebviewPanel));
22+
}
23+
24+
const mockCreateOutputChannel = () => {
25+
const mockedOutputChannel = mock<vscode.OutputChannel>();
26+
when(mockedWindow.createOutputChannel(
27+
anyString()
28+
)).thenReturn(instance(mockedOutputChannel));
29+
}
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/webviews/jdkDownloader.unit.test.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, it, beforeEach, afterEach } from 'mocha';
2+
import { expect } from 'chai';
3+
import * as sinon from 'sinon';
4+
import { WebviewPanel, window } from 'vscode';
5+
import { LOGGER } from '../../../logger';
6+
import { JdkDownloaderAction } from '../../../webviews/jdkDownloader/action';
7+
import { JdkDownloaderView } from '../../../webviews/jdkDownloader/view';
8+
9+
describe('JDK Downloader action tests', () => {
10+
let jdkDownloaderAction: JdkDownloaderAction;
11+
const sandbox = sinon.createSandbox();
12+
13+
beforeEach(() => {
14+
jdkDownloaderAction = new JdkDownloaderAction(new JdkDownloaderView());
15+
sandbox.stub(LOGGER, 'log').callsFake((message) => {
16+
console.log(message);
17+
});
18+
sandbox.stub(LOGGER, 'error').callsFake((message) => {
19+
console.error(message);
20+
});
21+
sandbox.stub(LOGGER, 'warn').callsFake((message) => {
22+
console.warn(message);
23+
});
24+
});
25+
26+
afterEach(() => {
27+
sandbox.restore();
28+
});
29+
30+
describe('JDK Downloader action attachListener tests', () => {
31+
32+
});
33+
});

0 commit comments

Comments
 (0)