Skip to content

Commit e8294d5

Browse files
Clean up unit tests' descriptions and logical separation (#3641)
I read through all the tests in order to firstly understand fully what we're covering and secondly be able to write correct BDD-style descriptions. I split tests up that could be for more logical separation, and formatted the files. I also enabled tests that had been previously disabled/skipped for reasons that are no longer applicable. We now have 55 separate unit tests, up from 35.
1 parent e1b55a5 commit e8294d5

8 files changed

+166
-162
lines changed

test/core/paths.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import * as path from "path";
77
import * as vscode from "vscode";
88
import utils = require("../utils");
99

10-
describe("Path assumptions", function() {
10+
describe("Path assumptions", function () {
1111
before(utils.ensureExtensionIsActivated);
1212

1313
// TODO: This is skipped because it intereferes with other tests. Either
1414
// need to find a way to close the opened folder via a Code API, or find
1515
// another way to test this.
16-
it.skip("The examples folder can be opened (and exists)", async function() {
16+
it.skip("Opens the examples folder at the expected path", async function () {
1717
assert(await vscode.commands.executeCommand("PowerShell.OpenExamplesFolder"));
1818
});
1919

20-
it("The session folder is created in the right place", async function() {
20+
it("Creates the session folder at the correct path", function () {
2121
assert(fs.existsSync(path.resolve(utils.rootPath, "sessions")));
2222
});
2323

24-
it("The logs folder is created in the right place", async function() {
24+
it("Creates the log folder at the correct path", function () {
2525
assert(fs.existsSync(path.resolve(utils.rootPath, "logs")));
2626
});
2727
});

test/core/platform.test.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ function setupTestEnvironment(testPlatform: ITestPlatform) {
467467
}
468468
}
469469

470-
describe("Platform module", function() {
471-
describe("PlatformDetails", function() {
470+
describe("Platform module", function () {
471+
it("Gets the correct platform details", function () {
472472
const platformDetails: platform.IPlatformDetails = platform.getPlatformDetails();
473473
switch (process.platform) {
474474
case "darwin":
@@ -517,18 +517,18 @@ describe("Platform module", function() {
517517
return;
518518

519519
default:
520-
assert.fail("Tests run on unsupported platform");
520+
assert.fail("This platform is unsupported");
521521
}
522522
});
523523

524-
describe("Default PowerShell installation", function() {
525-
afterEach(function() {
524+
describe("Default PowerShell installation", function () {
525+
afterEach(function () {
526526
sinon.restore();
527527
mockFS.restore();
528528
});
529529

530530
for (const testPlatform of successTestCases) {
531-
it(`Default PowerShell path on ${testPlatform.name}`, function() {
531+
it(`Finds it on ${testPlatform.name}`, function () {
532532
setupTestEnvironment(testPlatform);
533533

534534
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
@@ -542,7 +542,7 @@ describe("Platform module", function() {
542542
}
543543

544544
for (const testPlatform of errorTestCases) {
545-
it(`Extension startup fails gracefully on ${testPlatform.name}`, function() {
545+
it(`Fails gracefully on ${testPlatform.name}`, function () {
546546
setupTestEnvironment(testPlatform);
547547

548548
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
@@ -553,14 +553,14 @@ describe("Platform module", function() {
553553
}
554554
});
555555

556-
describe("Expected PowerShell installation list", function() {
557-
afterEach(function() {
556+
describe("Expected PowerShell installation list", function () {
557+
afterEach(function () {
558558
sinon.restore();
559559
mockFS.restore();
560560
});
561561

562562
for (const testPlatform of successTestCases) {
563-
it(`PowerShell installation list on ${testPlatform.name}`, function() {
563+
it(`Finds them on ${testPlatform.name}`, function () {
564564
setupTestEnvironment(testPlatform);
565565

566566
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
@@ -583,7 +583,7 @@ describe("Platform module", function() {
583583
}
584584

585585
for (const testPlatform of errorTestCases) {
586-
it(`Extension startup fails gracefully on ${testPlatform.name}`, function() {
586+
it(`Fails gracefully on ${testPlatform.name}`, function () {
587587
setupTestEnvironment(testPlatform);
588588

589589
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
@@ -594,16 +594,16 @@ describe("Platform module", function() {
594594
}
595595
});
596596

597-
describe("Windows PowerShell path fix", function() {
598-
afterEach(function() {
597+
describe("Windows PowerShell path fix", function () {
598+
afterEach(function () {
599599
sinon.restore();
600600
mockFS.restore();
601601
});
602602

603603
for (const testPlatform of successTestCases
604-
.filter((tp) => tp.platformDetails.operatingSystem === platform.OperatingSystem.Windows)) {
604+
.filter((tp) => tp.platformDetails.operatingSystem === platform.OperatingSystem.Windows)) {
605605

606-
it(`Corrects the Windows PowerShell path on ${testPlatform.name}`, function() {
606+
it(`Corrects the Windows PowerShell path on ${testPlatform.name}`, function () {
607607
setupTestEnvironment(testPlatform);
608608

609609
function getWinPSPath(systemDir: string) {

test/core/settings.test.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,38 @@ import * as assert from "assert";
55
import * as vscode from "vscode";
66
import Settings = require("../../src/settings");
77

8-
describe("Settings module", function() {
9-
it("Settings load without error", function() {
8+
describe("Settings module", function () {
9+
it("Loads without error", function () {
1010
assert.doesNotThrow(Settings.load);
1111
});
1212

13-
it("Settings update correctly", async function() {
14-
// then syntax
13+
it("Updates correctly with 'then' syntax", async function () {
1514
Settings.change("helpCompletion", "BlockComment", false).then(() =>
1615
assert.strictEqual(Settings.load().helpCompletion, "BlockComment"));
16+
});
1717

18-
// async/await syntax
18+
it("Updates correctly with 'async/await' syntax", async function () {
1919
await Settings.change("helpCompletion", "LineComment", false);
2020
assert.strictEqual(Settings.load().helpCompletion, "LineComment");
2121
});
2222

23-
it("Settings that can only be user settings update correctly", async function() {
24-
// set to false means it's set as a workspace-level setting so this should throw.
23+
describe("User-only settings", async function () {
2524
const psExeDetails = [{
2625
versionName: "My PowerShell",
2726
exePath: "dummyPath",
2827
}];
2928

30-
assert.rejects(async () => await Settings.change("powerShellAdditionalExePaths", psExeDetails, false));
29+
it("Throws when updating at workspace-level", async function () {
30+
assert.rejects(async () => await Settings.change("powerShellAdditionalExePaths", psExeDetails, false /* workspace-level */));
31+
});
3132

32-
// set to true means it's a user-level setting so this should not throw.
33-
await Settings.change("powerShellAdditionalExePaths", psExeDetails, true);
34-
assert.strictEqual(Settings.load().powerShellAdditionalExePaths[0].versionName, psExeDetails[0].versionName);
33+
it("Doesn't throw when updating at user-level", async function () {
34+
await Settings.change("powerShellAdditionalExePaths", psExeDetails, true /* user-level */);
35+
assert.strictEqual(Settings.load().powerShellAdditionalExePaths[0].versionName, psExeDetails[0].versionName);
36+
});
3537
});
3638

37-
it("Can get effective configuration target", async function() {
39+
it("Gets the effective configuration target", async function () {
3840
await Settings.change("helpCompletion", "LineComment", false);
3941
let target = await Settings.getEffectiveConfigurationTarget("helpCompletion");
4042
assert.strictEqual(target, vscode.ConfigurationTarget.Workspace);

test/features/CustomViews.test.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ function convertToVSCodeResourceScheme(filePath: string): string {
3030
return vscode.Uri.file(filePath).toString().replace("file://", "vscode-resource://");
3131
}
3232

33-
describe("CustomViews tests", function() {
33+
describe("CustomViews feature", function () {
3434
const testCases: IHtmlContentViewTestCase[] = [
35-
// Basic test that has no js or css.
3635
{
37-
name: "Basic",
36+
name: "with no JavaScript or CSS",
3837
htmlContent: "hello",
3938
javaScriptFiles: [],
4039
cssFiles: [],
@@ -45,7 +44,7 @@ hello
4544

4645
// A test that adds a js file.
4746
{
48-
name: "With JavaScript file",
47+
name: "with a JavaScript file but no CSS",
4948
htmlContent: "hello",
5049
javaScriptFiles: [
5150
{
@@ -62,7 +61,7 @@ hello
6261

6362
// A test that adds a js file in the current directory, and the parent directory.
6463
{
65-
name: "With 2 JavaScript files in two different locations",
64+
name: "with two JavaScript files in different locations, but no CSS",
6665
htmlContent: "hello",
6766
javaScriptFiles: [
6867
{
@@ -84,7 +83,7 @@ hello
8483

8584
// A test that adds a js file and a css file.
8685
{
87-
name: "With JavaScript and CSS file",
86+
name: "with a JavaScript and a CSS file",
8887
htmlContent: "hello",
8988
javaScriptFiles: [
9089
{
@@ -98,8 +97,7 @@ hello
9897
content: "body: { background-color: green; }",
9998
},
10099
],
101-
expectedHtmlString: `<html><head><link rel="stylesheet" href="${
102-
convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.css"))}">
100+
expectedHtmlString: `<html><head><link rel="stylesheet" href="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.css"))}">
103101
</head><body>
104102
hello
105103
<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script>
@@ -108,7 +106,7 @@ hello
108106
];
109107

110108
for (const testCase of testCases) {
111-
it(`Can create an HtmlContentView and get its content - ${testCase.name}`, function() {
109+
it(`Correctly creates an HtmlContentView ${testCase.name}`, function () {
112110
const htmlContentView = new HtmlContentView();
113111

114112
const jsPaths = testCase.javaScriptFiles.map((jsFile) => {

test/features/ExternalApi.test.ts

+63-71
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,85 @@ import * as assert from "assert";
55
import utils = require("../utils");
66
import { IExternalPowerShellDetails, IPowerShellExtensionClient } from "../../src/features/ExternalApi";
77

8-
describe("ExternalApi feature - Registration API", function() {
9-
let powerShellExtensionClient: IPowerShellExtensionClient;
10-
before(async function() {
11-
const powershellExtension = await utils.ensureExtensionIsActivated();
12-
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
13-
});
8+
describe("ExternalApi feature", function () {
9+
describe("External extension registration", function () {
10+
let powerShellExtensionClient: IPowerShellExtensionClient;
11+
before(async function () {
12+
const powershellExtension = await utils.ensureExtensionIsActivated();
13+
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
14+
});
1415

15-
it("It can register and unregister an extension", function() {
16-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
17-
assert.notStrictEqual(sessionId , "");
18-
assert.notStrictEqual(sessionId , null);
19-
assert.strictEqual(
20-
powerShellExtensionClient.unregisterExternalExtension(sessionId),
21-
true);
22-
});
16+
it("Registers and unregisters an extension", function () {
17+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
18+
assert.notStrictEqual(sessionId, "");
19+
assert.notStrictEqual(sessionId, null);
20+
assert.strictEqual(
21+
powerShellExtensionClient.unregisterExternalExtension(sessionId),
22+
true);
23+
});
2324

24-
it("It can register and unregister an extension with a version", function() {
25-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
26-
assert.notStrictEqual(sessionId , "");
27-
assert.notStrictEqual(sessionId , null);
28-
assert.strictEqual(
29-
powerShellExtensionClient.unregisterExternalExtension(sessionId),
30-
true);
31-
});
25+
it("Registers and unregisters an extension with a version", function () {
26+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
27+
assert.notStrictEqual(sessionId, "");
28+
assert.notStrictEqual(sessionId, null);
29+
assert.strictEqual(
30+
powerShellExtensionClient.unregisterExternalExtension(sessionId),
31+
true);
32+
});
3233

33-
/*
34-
NEGATIVE TESTS
35-
*/
36-
it("API fails if not registered", async function() {
37-
assert.rejects(
38-
async () => await powerShellExtensionClient.getPowerShellVersionDetails(""))
39-
});
34+
it("Rejects if not registered", async function () {
35+
assert.rejects(
36+
async () => await powerShellExtensionClient.getPowerShellVersionDetails(""))
37+
});
38+
39+
it("Throws if attempting to register an extension more than once", async function () {
40+
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
41+
try {
42+
assert.throws(
43+
() => powerShellExtensionClient.registerExternalExtension(utils.extensionId),
44+
{
45+
message: `The extension '${utils.extensionId}' is already registered.`
46+
});
47+
} finally {
48+
powerShellExtensionClient.unregisterExternalExtension(sessionId);
49+
}
50+
});
4051

41-
it("It can't register the same extension twice", async function() {
42-
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
43-
try {
52+
it("Throws when unregistering an extension that isn't registered", async function () {
4453
assert.throws(
45-
() => powerShellExtensionClient.registerExternalExtension(utils.extensionId),
54+
() => powerShellExtensionClient.unregisterExternalExtension("not-real"),
4655
{
47-
message: `The extension '${utils.extensionId}' is already registered.`
56+
message: `No extension registered with session UUID: not-real`
4857
});
49-
} finally {
50-
powerShellExtensionClient.unregisterExternalExtension(sessionId);
51-
}
52-
});
53-
54-
it("It can't unregister an extension that isn't registered", async function() {
55-
assert.throws(
56-
() => powerShellExtensionClient.unregisterExternalExtension("not-real"),
57-
{
58-
message: `No extension registered with session UUID: not-real`
59-
});
6058
});
61-
});
62-
63-
describe("ExternalApi feature - Other APIs", () => {
64-
let sessionId: string;
65-
let powerShellExtensionClient: IPowerShellExtensionClient;
66-
67-
before(async function() {
68-
const powershellExtension = await utils.ensureExtensionIsActivated();
69-
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
7059
});
7160

72-
beforeEach(function() {
73-
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
74-
});
61+
describe("PowerShell version details", () => {
62+
let sessionId: string;
63+
let powerShellExtensionClient: IPowerShellExtensionClient;
7564

76-
afterEach(function() {
77-
powerShellExtensionClient.unregisterExternalExtension(sessionId);
78-
});
65+
before(async function () {
66+
const powershellExtension = await utils.ensureExtensionIsActivated();
67+
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
68+
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
69+
});
7970

80-
it("It can get PowerShell version details", async function() {
81-
const versionDetails: IExternalPowerShellDetails = await powerShellExtensionClient.getPowerShellVersionDetails(sessionId);
71+
after(function () { powerShellExtensionClient.unregisterExternalExtension(sessionId); });
8272

83-
assert.notStrictEqual(versionDetails.architecture, "");
84-
assert.notStrictEqual(versionDetails.architecture, null);
73+
it("Gets non-empty version details from the PowerShell Editor Services", async function () {
74+
const versionDetails: IExternalPowerShellDetails = await powerShellExtensionClient.getPowerShellVersionDetails(sessionId);
8575

86-
assert.notStrictEqual(versionDetails.displayName, "");
87-
assert.notStrictEqual(versionDetails.displayName, null);
76+
assert.notStrictEqual(versionDetails.architecture, "");
77+
assert.notStrictEqual(versionDetails.architecture, null);
8878

89-
assert.notStrictEqual(versionDetails.exePath, "");
90-
assert.notStrictEqual(versionDetails.exePath, null);
79+
assert.notStrictEqual(versionDetails.displayName, "");
80+
assert.notStrictEqual(versionDetails.displayName, null);
9181

92-
assert.notStrictEqual(versionDetails.version, "");
93-
assert.notStrictEqual(versionDetails.version, null);
82+
assert.notStrictEqual(versionDetails.exePath, "");
83+
assert.notStrictEqual(versionDetails.exePath, null);
9484

95-
// Start up can take some time...so set the timeout to 30 seconds.
85+
assert.notStrictEqual(versionDetails.version, "");
86+
assert.notStrictEqual(versionDetails.version, null);
87+
});
9688
});
9789
});

0 commit comments

Comments
 (0)