Skip to content

Commit 0169408

Browse files
committed
Simplify tests and fix teardown
- Use the vscode-test command line instead of a separate script in order to run the tests. - Add proper teardown to some test suites that didn't have it Switching over to the vscode-test cli changed the order in which the tests are executed. Since some tests didn't have proper teardown, the following tests in the suite started failing. By adding the proper teardown (and in one case changing the expected result) I got them passing again. Signed-off-by: David Thompson <davthomp@redhat.com> Reduce expected number of calls to get tests passing It used to be 2 because a document from a previous test was left open, which triggered the update twice (once during the direct invocation, and once during the faked document changed notification). Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 30fed2e commit 0169408

File tree

12 files changed

+65
-145
lines changed

12 files changed

+65
-145
lines changed

.vscode-test.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { defineConfig } from '@vscode/test-cli';
22

33
export default defineConfig({
4-
files: 'out/smoke-test/**/*.test.js',
5-
workspaceFolder: './smoke-test/',
4+
files: 'out/test/**/*.test.js',
5+
workspaceFolder: '.',
6+
mocha: {
7+
ui: 'bdd',
8+
timeout: 10_000,
9+
},
610
});

.vscode-test.smoketest.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from '@vscode/test-cli';
2+
3+
export default defineConfig({
4+
files: 'out/smoke-test/**/*.test.js',
5+
workspaceFolder: './smoke-test/',
6+
mocha: {
7+
ui: 'bdd'
8+
}
9+
});

.vscode/launch.json

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616
"VSCODE_REDHAT_TELEMETRY_DEBUG":"true"
1717
}
1818
},
19-
{
20-
"name": "Extension Tests",
21-
"type": "extensionHost",
22-
"request": "launch",
23-
"runtimeExecutable": "${execPath}",
24-
"preLaunchTask": "compile test",
25-
"args": [
26-
"--disable-extension=ms-kubernetes-tools.vscode-kubernetes-tools",
27-
"--extensionDevelopmentPath=${workspaceFolder}",
28-
"--extensionTestsPath=${workspaceFolder}/out/test",
29-
"${workspaceRoot}/test/testFixture"
30-
],
31-
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
32-
"env": {
33-
"DEBUG_VSCODE_YAML":"true"
34-
}
35-
},
3619
{
3720
"name": "Launch Web Extension",
3821
"type": "extensionHost",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@
293293
"compile": "webpack --mode none",
294294
"format": "prettier --write .",
295295
"lint": "eslint -c .eslintrc.js --ext .ts src test",
296-
"test": "npm run test-compile && sh scripts/e2e.sh",
296+
"test": "npm run test-compile && npx vscode-test",
297297
"ui-test": "npm run test-compile && extest setup-and-run out/test/ui-test/allTestsSuite.js -c 1.76.2",
298298
"vscode:prepublish": "webpack --mode production && npm run copy-l10n",
299299
"copy-l10n": "cp -r node_modules/yaml-language-server/l10n dist/l10n",
300300
"watch": "webpack --mode development --watch --info-verbosity verbose",
301301
"test-compile": "npm run clean && tsc -p ./ && webpack --mode development && npm run copy-l10n",
302-
"smoke-test": "npm run test-compile && vscode-test smoke-test/*",
302+
"smoke-test": "npm run test-compile && vscode-test --config .vscode-test.smoketest.mjs",
303303
"compile-smoke-test-web": "webpack --config smoke-test/webpack.config",
304304
"smoke-test-web": "npm run compile-smoke-test-web && vscode-test-web --extensionDevelopmentPath=. --extensionTestsPath=./out/smoke-test/smoke-test-runner.js ./smoke-test",
305305
"run-in-chromium": "npm run compile && vscode-test-web --browserType=chromium --extensionDevelopmentPath=. smoke-test"

scripts/e2e.sh

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

smoke-test/smoke-test-runner.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
require('mocha/mocha');
22

33
export function run(): Promise<void> {
4+
return new Promise((c, e) => {
5+
mocha.setup({
6+
ui: 'bdd',
7+
reporter: undefined,
8+
});
49

5-
return new Promise((c, e) => {
6-
mocha.setup({
7-
ui: 'tdd',
8-
reporter: undefined
9-
});
10+
// bundles all files in the current directory matching `*.test`
11+
const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r);
12+
importAll(require.context('.', true, /\.test$/));
1013

11-
// bundles all files in the current directory matching `*.test`
12-
const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r);
13-
importAll(require.context('.', true, /\.test$/));
14-
15-
try {
16-
// Run the mocha test
17-
mocha.run(failures => {
18-
if (failures > 0) {
19-
e(new Error(`${failures} tests failed.`));
20-
} else {
21-
c();
22-
}
23-
});
24-
} catch (err) {
25-
console.error(err);
26-
e(err);
27-
}
28-
});
29-
}
14+
try {
15+
// Run the mocha test
16+
mocha.run((failures) => {
17+
if (failures > 0) {
18+
e(new Error(`${failures} tests failed.`));
19+
} else {
20+
c();
21+
}
22+
});
23+
} catch (err) {
24+
console.error(err);
25+
e(err);
26+
}
27+
});
28+
}

smoke-test/smoke.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert } from 'chai';
22
import * as vscode from 'vscode';
33
import { URI } from 'vscode-uri';
44

5-
suite('Smoke test suite', function () {
5+
describe('Smoke test suite', function () {
66
this.timeout(10_000);
77

88
// diagnostics take some time to appear; the language server must be started and respond to file open event
@@ -16,7 +16,7 @@ suite('Smoke test suite', function () {
1616
let throughSettingsUri: URI;
1717
let unformattedUri: URI;
1818

19-
this.beforeAll(async function () {
19+
before(async function () {
2020
if (vscode.workspace.workspaceFolders === undefined) {
2121
assert.fail('No workspace folder');
2222
}
@@ -32,7 +32,7 @@ suite('Smoke test suite', function () {
3232
});
3333
});
3434

35-
test('instance has right diagnostics', async function () {
35+
it('has right diagnostics when schema is referenced through a comment', async function () {
3636
const textDocument = await vscode.workspace.openTextDocument(schemaInstanceUri);
3737
await vscode.window.showTextDocument(textDocument);
3838
await new Promise((resolve) => setTimeout(resolve, DIAGNOSTICS_DELAY));
@@ -42,7 +42,7 @@ suite('Smoke test suite', function () {
4242
assert.strictEqual(diagnostics[0].message, 'Value is below the minimum of 0.');
4343
});
4444

45-
test('has right diagnostics when schema is referenced through settings', async function () {
45+
it('has right diagnostics when schema is referenced through settings', async function () {
4646
const textDocument = await vscode.workspace.openTextDocument(throughSettingsUri);
4747
await vscode.window.showTextDocument(textDocument);
4848
await new Promise((resolve) => setTimeout(resolve, DIAGNOSTICS_DELAY));
@@ -52,7 +52,7 @@ suite('Smoke test suite', function () {
5252
assert.strictEqual(diagnostics[0].message, 'Value is below the minimum of 0.');
5353
});
5454

55-
test('has right formatting', async function () {
55+
it('has right formatting', async function () {
5656
const textDocument = await vscode.workspace.openTextDocument(unformattedUri);
5757
await vscode.window.showTextDocument(textDocument);
5858

test/completion.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ describe('Completion should work in multiple different scenarios', () => {
1616
await resetSettings('schemaStore.enable', true);
1717
});
1818

19+
after(async () => {
20+
vscode.window.tabGroups.close(vscode.window.tabGroups.activeTabGroup);
21+
});
22+
1923
it('completion works with local schema', async () => {
2024
await activate(docUri);
2125
const schemaPath = path.join(__dirname, '..', '..', 'test', 'testFixture', 'schemas', 'basic_completion_schema.json');

test/index.ts

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

test/json-schema-selection.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Status bar should work in multiple different scenarios', () => {
3939
const context: vscode.ExtensionContext = {
4040
subscriptions: [],
4141
} as vscode.ExtensionContext;
42-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
42+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
4343
createStatusBarItemStub.returns(statusBar);
4444
clcStub.sendRequest.resolves([]);
4545

@@ -54,7 +54,7 @@ describe('Status bar should work in multiple different scenarios', () => {
5454
const context: vscode.ExtensionContext = {
5555
subscriptions: [],
5656
} as vscode.ExtensionContext;
57-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
57+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
5858
createStatusBarItemStub.returns(statusBar);
5959
onDidChangeActiveTextEditorStub.returns({});
6060
clcStub.sendRequest.resolves([{ uri: 'https://foo.com/bar.json', name: 'bar schema' }]);
@@ -66,14 +66,14 @@ describe('Status bar should work in multiple different scenarios', () => {
6666
expect(statusBar.text).to.equal('bar schema');
6767
expect(statusBar.tooltip).to.equal('Select JSON Schema');
6868
expect(statusBar.backgroundColor).to.be.undefined;
69-
expect(statusBar.show).calledTwice;
69+
expect(statusBar.show).calledOnce;
7070
});
7171

7272
it('Should inform if there are no schema', async () => {
7373
const context: vscode.ExtensionContext = {
7474
subscriptions: [],
7575
} as vscode.ExtensionContext;
76-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
76+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
7777
createStatusBarItemStub.returns(statusBar);
7878
onDidChangeActiveTextEditorStub.returns({});
7979
clcStub.sendRequest.resolves([]);
@@ -85,14 +85,14 @@ describe('Status bar should work in multiple different scenarios', () => {
8585
expect(statusBar.text).to.equal('No JSON Schema');
8686
expect(statusBar.tooltip).to.equal('Select JSON Schema');
8787
expect(statusBar.backgroundColor).to.be.undefined;
88-
expect(statusBar.show).calledTwice;
88+
expect(statusBar.show).calledOnce;
8989
});
9090

9191
it('Should inform if there are more than one schema', async () => {
9292
const context: vscode.ExtensionContext = {
9393
subscriptions: [],
9494
} as vscode.ExtensionContext;
95-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
95+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
9696
createStatusBarItemStub.returns(statusBar);
9797
onDidChangeActiveTextEditorStub.returns({});
9898
clcStub.sendRequest.resolves([{}, {}]);
@@ -104,14 +104,14 @@ describe('Status bar should work in multiple different scenarios', () => {
104104
expect(statusBar.text).to.equal('Multiple JSON Schemas...');
105105
expect(statusBar.tooltip).to.equal('Multiple JSON Schema used to validate this file, click to select one');
106106
expect(statusBar.backgroundColor).to.eql({ id: 'statusBarItem.warningBackground' });
107-
expect(statusBar.show).calledTwice;
107+
expect(statusBar.show).calledOnce;
108108
});
109109

110110
it('Should show JSON Schema Store schema version', async () => {
111111
const context: vscode.ExtensionContext = {
112112
subscriptions: [],
113113
} as vscode.ExtensionContext;
114-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
114+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
115115
createStatusBarItemStub.returns(statusBar);
116116
onDidChangeActiveTextEditorStub.returns({ document: { uri: vscode.Uri.parse('/foo.yaml') } });
117117
clcStub.sendRequest
@@ -128,14 +128,14 @@ describe('Status bar should work in multiple different scenarios', () => {
128128
expect(statusBar.text).to.equal('bar schema(1.0.0)');
129129
expect(statusBar.tooltip).to.equal('Select JSON Schema');
130130
expect(statusBar.backgroundColor).to.be.undefined;
131-
expect(statusBar.show).calledTwice;
131+
expect(statusBar.show).calledOnce;
132132
});
133133

134134
it('Should show JSON Schema Store schema version, dont include version', async () => {
135135
const context: vscode.ExtensionContext = {
136136
subscriptions: [],
137137
} as vscode.ExtensionContext;
138-
const statusBar = ({ show: sandbox.stub() } as unknown) as vscode.StatusBarItem;
138+
const statusBar = ({ show: sandbox.stub(), hide: sandbox.stub() } as unknown) as vscode.StatusBarItem;
139139
createStatusBarItemStub.returns(statusBar);
140140
onDidChangeActiveTextEditorStub.returns({ document: { uri: vscode.Uri.parse('/foo.yaml') } });
141141
clcStub.sendRequest
@@ -152,6 +152,6 @@ describe('Status bar should work in multiple different scenarios', () => {
152152
expect(statusBar.text).to.equal('bar schema(1.0.0)');
153153
expect(statusBar.tooltip).to.equal('Select JSON Schema');
154154
expect(statusBar.backgroundColor).to.be.undefined;
155-
expect(statusBar.show).calledTwice;
155+
expect(statusBar.show).calledOnce;
156156
});
157157
});

0 commit comments

Comments
 (0)