Skip to content

Commit 9feaa19

Browse files
committed
Refactor tests to improve performance and readability
1 parent de87e1d commit 9feaa19

File tree

1 file changed

+147
-96
lines changed

1 file changed

+147
-96
lines changed

src/test/extension.test.ts

Lines changed: 147 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,105 @@
11
import * as assert from 'assert';
22
import * as vscode from 'vscode';
3-
import configUrlsToWatchRequired from './examples/config-urls-to-watch-required.json';
4-
import configPluginConfigRequired from './examples/config-plugin-config-required.json';
5-
import configPluginConfigRequiredDisabled from './examples/config-plugin-config-required-disabled.json';
6-
import configPluginConfigMissing from './examples/config-plugin-config-missing.json';
7-
import configPluginConfigMissingDisabled from './examples/config-plugin-config-missing.json';
8-
import {isConfigFile} from '../helpers';
3+
import * as fs from 'fs';
4+
import {
5+
getASTNode,
6+
getStartPositionFromASTNode,
7+
isConfigFile,
8+
sleep,
9+
} from '../helpers';
910
import * as path from 'path';
11+
import parse from 'json-to-ast';
1012
import {createCodeLensForPluginNodes} from '../codelens';
1113

1214
suite('Extension Test Suite', () => {
13-
test('should activate when JSON file is opened', async () => {
14-
assert.strictEqual(
15-
vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')
16-
?.isActive,
17-
false
18-
);
15+
test('should activate when untitled JSON file is opened', async () => {
16+
const extensionId = 'garrytrinder.dev-proxy-toolkit';
1917
await vscode.workspace.openTextDocument({
2018
language: 'json',
2119
content: '',
2220
});
2321
await sleep(1000);
24-
assert.strictEqual(
25-
vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')
26-
?.isActive,
27-
true
28-
);
29-
});
3022

31-
test('should show error when no urlsToWatch found', async () => {
32-
const document = await vscode.workspace.openTextDocument({
33-
language: 'json',
34-
content: JSON.stringify(configUrlsToWatchRequired, null, 2),
35-
});
36-
await sleep(1000);
37-
const diagnostics = vscode.languages.getDiagnostics(document.uri);
38-
assert.strictEqual(diagnostics?.length, 1);
39-
assert.strictEqual(
40-
diagnostics?.[0].message,
41-
'Add at least one url to watch.'
42-
);
43-
assert.strictEqual(
44-
diagnostics?.[0].severity,
45-
vscode.DiagnosticSeverity.Error
46-
);
23+
const expected = true;
24+
const actual = vscode.extensions.getExtension(extensionId)?.isActive;
25+
assert.strictEqual(actual, expected);
4726
});
4827

49-
test('should show error when plugin requires config section', async () => {
50-
const document = await vscode.workspace.openTextDocument({
51-
language: 'json',
52-
content: JSON.stringify(configPluginConfigRequired, null, 2),
53-
});
28+
test('should activate when JSON file is opened from disk', async () => {
29+
const extensionId = 'garrytrinder.dev-proxy-toolkit';
30+
const fileName = 'foo.json';
31+
const filePath = path.resolve(__dirname, 'examples', fileName);
32+
await vscode.workspace.openTextDocument(filePath);
5433
await sleep(1000);
55-
const diagnostics = vscode.languages.getDiagnostics(document.uri);
56-
assert.strictEqual(diagnostics?.length, 1);
57-
assert.strictEqual(
58-
diagnostics?.[0].message,
59-
'GenericRandomErrorPlugin requires a config section.'
60-
);
61-
assert.strictEqual(
62-
diagnostics?.[0].severity,
63-
vscode.DiagnosticSeverity.Error
64-
);
34+
35+
const expected = true;
36+
const actual = vscode.extensions.getExtension(extensionId)?.isActive;
37+
assert.strictEqual(actual, expected);
6538
});
39+
});
6640

67-
test('should show warning when disabled plugin requires config section', async () => {
68-
const document = await vscode.workspace.openTextDocument({
69-
language: 'json',
70-
content: JSON.stringify(configPluginConfigRequiredDisabled, null, 2),
71-
});
72-
await sleep(1000);
41+
suite('urlsToWatch', () => {
42+
test('should show error when opening document with no urlsToWatch found', async () => {
43+
const fileName = 'config-urls-to-watch-required.json';
44+
const filePath = path.resolve(__dirname, 'examples', fileName);
45+
const document = await vscode.workspace.openTextDocument(filePath);
7346
const diagnostics = vscode.languages.getDiagnostics(document.uri);
74-
assert.strictEqual(diagnostics?.length, 1);
75-
assert.strictEqual(
76-
diagnostics?.[0].message,
77-
'GenericRandomErrorPlugin requires a config section.'
78-
);
79-
assert.strictEqual(
80-
diagnostics?.[0].severity,
81-
vscode.DiagnosticSeverity.Warning
82-
);
47+
48+
const expected = {
49+
message: 'Add at least one url to watch.',
50+
severity: vscode.DiagnosticSeverity.Error,
51+
};
52+
const actual = {
53+
message: diagnostics[0]?.message,
54+
severity: diagnostics[0]?.severity,
55+
};
56+
assert.deepStrictEqual(actual, expected);
8357
});
8458

85-
test('should show error when plugin config section is not defined', async () => {
59+
test('should show error when document changes and has no urlsToWatch found', async () => {
60+
const fileName = 'config-urls-to-watch-required.json';
61+
const filePath = path.resolve(__dirname, 'examples', fileName);
62+
const fileContents = fs.readFileSync(filePath, 'utf8');
8663
const document = await vscode.workspace.openTextDocument({
8764
language: 'json',
88-
content: JSON.stringify(configPluginConfigMissing, null, 2),
65+
content: fileContents,
8966
});
9067
await sleep(1000);
9168
const diagnostics = vscode.languages.getDiagnostics(document.uri);
92-
assert.strictEqual(diagnostics?.length, 1);
93-
assert.strictEqual(
94-
diagnostics?.[0].message,
95-
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one."
96-
);
97-
assert.strictEqual(
98-
diagnostics?.[0].severity,
99-
vscode.DiagnosticSeverity.Error
100-
);
69+
70+
const expected = {
71+
message: 'Add at least one url to watch.',
72+
severity: vscode.DiagnosticSeverity.Error,
73+
};
74+
const actual = {
75+
message: diagnostics[0]?.message,
76+
severity: diagnostics[0]?.severity,
77+
};
78+
assert.deepStrictEqual(actual, expected);
10179
});
10280

103-
test('should show warning when disabled plugin config section is not defined', async () => {
104-
const document = await vscode.workspace.openTextDocument({
105-
language: 'json',
106-
content: JSON.stringify(configPluginConfigMissingDisabled, null, 2),
107-
});
81+
test('should have no error after adding a urlToWatch', async () => {
82+
const fileName = 'config-urls-to-watch-required.json';
83+
const filePath = path.resolve(__dirname, 'examples', fileName);
84+
const document = await vscode.workspace.openTextDocument(filePath);
10885
await sleep(1000);
109-
const diagnostics = vscode.languages.getDiagnostics(document.uri);
110-
assert.strictEqual(diagnostics?.length, 1);
111-
assert.strictEqual(
112-
diagnostics?.[0].message,
113-
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one."
86+
const documentNode = parse(document.getText()) as parse.ObjectNode;
87+
const urlsToWatchNode = getASTNode(
88+
documentNode.children,
89+
'Identifier',
90+
'urlsToWatch'
11491
);
115-
assert.strictEqual(
116-
diagnostics?.[0].severity,
117-
vscode.DiagnosticSeverity.Error
92+
const position = getStartPositionFromASTNode(
93+
urlsToWatchNode?.value as parse.ArrayNode
11894
);
95+
const edit = new vscode.WorkspaceEdit();
96+
edit.insert(document.uri, position, '"https://example.com"');
97+
await vscode.workspace.applyEdit(edit);
98+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
99+
100+
const expected = 0;
101+
const actual = diagnostics.length;
102+
assert.strictEqual(actual, expected);
119103
});
120104
});
121105

@@ -187,23 +171,90 @@ suite('isConfigFile', () => {
187171
});
188172
});
189173

190-
suite('Plugins Code Lens', () => {
174+
suite('plugins', () => {
175+
test('should show error when plugin requires config section', async () => {
176+
const fileName = 'config-plugin-config-required.json';
177+
const filePath = path.resolve(__dirname, 'examples', fileName);
178+
const document = await vscode.workspace.openTextDocument(filePath);
179+
await sleep(1000);
180+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
181+
182+
const expected = {
183+
message: 'GenericRandomErrorPlugin requires a config section.',
184+
severity: vscode.DiagnosticSeverity.Error,
185+
};
186+
const actual = {
187+
message: diagnostics[0]?.message,
188+
severity: diagnostics[0]?.severity,
189+
};
190+
assert.deepStrictEqual(actual, expected);
191+
});
192+
193+
test('should show warning when disabled plugin requires config section', async () => {
194+
const fileName = 'config-plugin-config-required-disabled.json';
195+
const filePath = path.resolve(__dirname, 'examples', fileName);
196+
const document = await vscode.workspace.openTextDocument(filePath);
197+
await sleep(1000);
198+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
199+
200+
const expected = {
201+
message: 'GenericRandomErrorPlugin requires a config section.',
202+
severity: vscode.DiagnosticSeverity.Warning,
203+
};
204+
const actual = {
205+
message: diagnostics[0]?.message,
206+
severity: diagnostics[0]?.severity,
207+
};
208+
assert.deepStrictEqual(actual, expected);
209+
});
210+
211+
test('should show error when plugin config section is not defined', async () => {
212+
const fileName = 'config-plugin-config-missing.json';
213+
const filePath = path.resolve(__dirname, 'examples', fileName);
214+
const document = await vscode.workspace.openTextDocument(filePath);
215+
await sleep(1000);
216+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
217+
218+
const expected = {
219+
message:
220+
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one.",
221+
severity: vscode.DiagnosticSeverity.Error,
222+
};
223+
const actual = {
224+
message: diagnostics[0]?.message,
225+
severity: diagnostics[0]?.severity,
226+
};
227+
assert.deepStrictEqual(actual, expected);
228+
});
229+
230+
test('should show warning when disabled plugin config section is not defined', async () => {
231+
const fileName = 'config-plugin-config-missing-disabled.json';
232+
const filePath = path.resolve(__dirname, 'examples', fileName);
233+
const document = await vscode.workspace.openTextDocument(filePath);
234+
await sleep(1000);
235+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
236+
237+
const expected = {
238+
message:
239+
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one.",
240+
severity: vscode.DiagnosticSeverity.Warning,
241+
};
242+
const actual = {
243+
message: diagnostics[0]?.message,
244+
severity: diagnostics[0]?.severity,
245+
};
246+
assert.deepStrictEqual(actual, expected);
247+
});
248+
191249
test('should show code lens for each plugin', async () => {
192250
const fileName = 'config-plugins-codelens.json';
193251
const filePath = path.resolve(__dirname, 'examples', fileName);
194252
const document = await vscode.workspace.openTextDocument(filePath);
195253
await sleep(1000);
196-
197254
const codeLens = createCodeLensForPluginNodes(document);
198255

199256
const expected = 2;
200257
const actual = codeLens.length;
201258
assert.strictEqual(actual, expected);
202259
});
203260
});
204-
205-
function sleep(ms: number): Promise<void> {
206-
return new Promise(resolve => {
207-
setTimeout(resolve, ms);
208-
});
209-
}

0 commit comments

Comments
 (0)