Skip to content

Commit 515fb66

Browse files
Convert Mocha unit tests to BDD style (PowerShell#3607)
1 parent 9013d58 commit 515fb66

9 files changed

+52
-57
lines changed

test/core/paths.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ import * as assert from "assert";
55
import * as fs from "fs";
66
import * as path from "path";
77
import * as vscode from "vscode";
8-
import { suiteSetup } from "mocha";
98
import utils = require("../utils");
109

11-
suite("Path assumptions", () => {
12-
suiteSetup(utils.ensureExtensionIsActivated);
10+
describe("Path assumptions", () => {
11+
before(utils.ensureExtensionIsActivated);
1312

1413
// TODO: This is skipped because it intereferes with other tests. Either
1514
// need to find a way to close the opened folder via a Code API, or find
1615
// another way to test this.
17-
test.skip("The examples folder can be opened (and exists)", async () => {
16+
it.skip("The examples folder can be opened (and exists)", async () => {
1817
assert(await vscode.commands.executeCommand("PowerShell.OpenExamplesFolder"));
1918
});
2019

21-
test("The session folder is created in the right place", async () => {
20+
it("The session folder is created in the right place", async () => {
2221
assert(fs.existsSync(path.resolve(utils.rootPath, "sessions")));
2322
});
2423

25-
test("The logs folder is created in the right place", async () => {
24+
it("The logs folder is created in the right place", async () => {
2625
assert(fs.existsSync(path.resolve(utils.rootPath, "logs")));
2726
});
2827
});

test/core/platform.test.ts

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

470-
suite("Platform module", () => {
471-
suite("PlatformDetails", () => {
470+
describe("Platform module", () => {
471+
describe("PlatformDetails", () => {
472472
const platformDetails: platform.IPlatformDetails = platform.getPlatformDetails();
473473
switch (process.platform) {
474474
case "darwin":
@@ -521,14 +521,14 @@ suite("Platform module", () => {
521521
}
522522
});
523523

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

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

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

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

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

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

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

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

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

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

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

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

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

609609
function getWinPSPath(systemDir: string) {

test/core/settings.test.ts

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

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

13-
test("Settings update correctly", async () => {
13+
it("Settings update correctly", async () => {
1414
// then syntax
1515
Settings.change("helpCompletion", "BlockComment", false).then(() =>
1616
assert.strictEqual(Settings.load().helpCompletion, "BlockComment"));
@@ -20,7 +20,7 @@ suite("Settings module", () => {
2020
assert.strictEqual(Settings.load().helpCompletion, "LineComment");
2121
});
2222

23-
test("Settings that can only be user settings update correctly", async () => {
23+
it("Settings that can only be user settings update correctly", async () => {
2424
// set to false means it's set as a workspace-level setting so this should throw.
2525
const psExeDetails = [{
2626
versionName: "My PowerShell",
@@ -34,7 +34,7 @@ suite("Settings module", () => {
3434
assert.strictEqual(Settings.load().powerShellAdditionalExePaths[0].versionName, psExeDetails[0].versionName);
3535
});
3636

37-
test("Can get effective configuration target", async () => {
37+
it("Can get effective configuration target", async () => {
3838
await Settings.change("helpCompletion", "LineComment", false);
3939
let target = await Settings.getEffectiveConfigurationTarget("helpCompletion");
4040
assert.strictEqual(target, vscode.ConfigurationTarget.Workspace);

test/features/CustomViews.test.ts

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

33-
suite("CustomViews tests", () => {
33+
describe("CustomViews tests", () => {
3434
const testCases: IHtmlContentViewTestCase[] = [
3535
// Basic test that has no js or css.
3636
{
@@ -108,7 +108,7 @@ hello
108108
];
109109

110110
for (const testCase of testCases) {
111-
test(`Can create an HtmlContentView and get its content - ${testCase.name}`, () => {
111+
it(`Can create an HtmlContentView and get its content - ${testCase.name}`, () => {
112112
const htmlContentView = new HtmlContentView();
113113

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

test/features/ExternalApi.test.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
// Licensed under the MIT License.
33

44
import * as assert from "assert";
5-
import { suiteSetup, setup, teardown } from "mocha";
65
import utils = require("../utils");
76
import { IExternalPowerShellDetails, IPowerShellExtensionClient } from "../../src/features/ExternalApi";
87

9-
suite("ExternalApi feature - Registration API", () => {
8+
describe("ExternalApi feature - Registration API", () => {
109
let powerShellExtensionClient: IPowerShellExtensionClient;
11-
suiteSetup(async () => {
10+
before(async () => {
1211
const powershellExtension = await utils.ensureExtensionIsActivated();
1312
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
1413
});
1514

16-
test("It can register and unregister an extension", () => {
15+
it("It can register and unregister an extension", () => {
1716
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
1817
assert.notStrictEqual(sessionId , "");
1918
assert.notStrictEqual(sessionId , null);
@@ -22,7 +21,7 @@ suite("ExternalApi feature - Registration API", () => {
2221
true);
2322
});
2423

25-
test("It can register and unregister an extension with a version", () => {
24+
it("It can register and unregister an extension with a version", () => {
2625
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
2726
assert.notStrictEqual(sessionId , "");
2827
assert.notStrictEqual(sessionId , null);
@@ -34,12 +33,12 @@ suite("ExternalApi feature - Registration API", () => {
3433
/*
3534
NEGATIVE TESTS
3635
*/
37-
test("API fails if not registered", async () => {
36+
it("API fails if not registered", async () => {
3837
assert.rejects(
3938
async () => await powerShellExtensionClient.getPowerShellVersionDetails(""))
4039
});
4140

42-
test("It can't register the same extension twice", async () => {
41+
it("It can't register the same extension twice", async () => {
4342
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
4443
try {
4544
assert.throws(
@@ -52,7 +51,7 @@ suite("ExternalApi feature - Registration API", () => {
5251
}
5352
});
5453

55-
test("It can't unregister an extension that isn't registered", async () => {
54+
it("It can't unregister an extension that isn't registered", async () => {
5655
assert.throws(
5756
() => powerShellExtensionClient.unregisterExternalExtension("not-real"),
5857
{
@@ -61,24 +60,24 @@ suite("ExternalApi feature - Registration API", () => {
6160
});
6261
});
6362

64-
suite("ExternalApi feature - Other APIs", () => {
63+
describe("ExternalApi feature - Other APIs", () => {
6564
let sessionId: string;
6665
let powerShellExtensionClient: IPowerShellExtensionClient;
6766

68-
suiteSetup(async () => {
67+
before(async () => {
6968
const powershellExtension = await utils.ensureExtensionIsActivated();
7069
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
7170
});
7271

73-
setup(() => {
72+
beforeEach(() => {
7473
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
7574
});
7675

77-
teardown(() => {
76+
afterEach(() => {
7877
powerShellExtensionClient.unregisterExternalExtension(sessionId);
7978
});
8079

81-
test("It can get PowerShell version details", async () => {
80+
it("It can get PowerShell version details", async () => {
8281
const versionDetails: IExternalPowerShellDetails = await powerShellExtensionClient.getPowerShellVersionDetails(sessionId);
8382

8483
assert.notStrictEqual(versionDetails.architecture, "");

test/features/ISECompatibility.test.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,36 @@
33

44
import * as assert from "assert";
55
import * as vscode from "vscode";
6-
import { suiteSetup, setup, suiteTeardown, teardown } from "mocha";
76
import { ISECompatibilityFeature } from "../../src/features/ISECompatibility";
87
import utils = require("../utils");
98

10-
suite("ISECompatibility feature", () => {
9+
describe("ISECompatibility feature", () => {
1110
let currentTheme: string;
1211

13-
suiteSetup(async () => {
12+
before(async () => {
1413
// Save user's current theme.
1514
currentTheme = await vscode.workspace.getConfiguration("workbench").get("colorTheme");
1615
await utils.ensureExtensionIsActivated();
1716
});
1817

19-
setup(async () => { await vscode.commands.executeCommand("PowerShell.EnableISEMode"); });
18+
beforeEach(async () => { await vscode.commands.executeCommand("PowerShell.EnableISEMode"); });
2019

21-
teardown(async () => { await vscode.commands.executeCommand("PowerShell.DisableISEMode"); });
20+
afterEach(async () => { await vscode.commands.executeCommand("PowerShell.DisableISEMode"); });
2221

23-
suiteTeardown(async () => {
22+
after(async () => {
2423
// Reset user's current theme.
2524
await vscode.workspace.getConfiguration("workbench").update("colorTheme", currentTheme, true);
2625
assert.strictEqual(vscode.workspace.getConfiguration("workbench").get("colorTheme"), currentTheme);
2726
});
2827

29-
test("It sets ISE Settings", async () => {
28+
it("It sets ISE Settings", async () => {
3029
for (const iseSetting of ISECompatibilityFeature.settings) {
3130
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
3231
assert.strictEqual(currently, iseSetting.value);
3332
}
3433
});
3534

36-
test("It unsets ISE Settings", async () => {
35+
it("It unsets ISE Settings", async () => {
3736
// Change state to something that DisableISEMode will change
3837
await vscode.workspace.getConfiguration("workbench").update("colorTheme", "PowerShell ISE", true);
3938
assert.strictEqual(vscode.workspace.getConfiguration("workbench").get("colorTheme"), "PowerShell ISE");
@@ -45,7 +44,7 @@ suite("ISECompatibility feature", () => {
4544
}
4645
});
4746

48-
test("It doesn't change theme when disabled if theme was manually changed after being enabled", async () => {
47+
it("It doesn't change theme when disabled if theme was manually changed after being enabled", async () => {
4948
assert.strictEqual(vscode.workspace.getConfiguration("workbench").get("colorTheme"), "PowerShell ISE");
5049

5150
// "Manually" change theme after enabling ISE mode. Use a built-in theme but not the default.

test/features/RunCode.test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as fs from "fs";
66
import * as path from "path";
77
import rewire = require("rewire");
88
import vscode = require("vscode");
9-
import { suiteSetup } from "mocha";
109
import utils = require("../utils");
1110
import { sleep } from "../../src/utils";
1211

@@ -19,10 +18,10 @@ enum LaunchType {
1918
Run,
2019
}
2120

22-
suite("RunCode tests", () => {
23-
suiteSetup(utils.ensureExtensionIsActivated);
21+
describe("RunCode tests", () => {
22+
before(utils.ensureExtensionIsActivated);
2423

25-
test("Can create the launch config", () => {
24+
it("Can create the launch config", () => {
2625
const commandToRun: string = "Invoke-Build";
2726
const args: string[] = ["Clean"];
2827

@@ -43,7 +42,7 @@ suite("RunCode tests", () => {
4342
assert.deepStrictEqual(actual, expected);
4443
});
4544

46-
test("Can run Pester tests from file", async () => {
45+
it("Can run Pester tests from file", async () => {
4746
const pesterTests = path.resolve(__dirname, "../../../examples/Tests/SampleModule.Tests.ps1");
4847
assert(fs.existsSync(pesterTests));
4948

test/features/UpdatePowerShell.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import { GitHubReleaseInformation } from "../../src/features/UpdatePowerShell";
88
// the GitHub API rate limit often. Let's skip these tests on macOS until
99
// they are hooked up to only run on release.
1010
if (process.env.TF_BUILD && process.platform === "win32") {
11-
suite("UpdatePowerShell tests", () => {
12-
test("Can get the latest version", async () => {
11+
describe("UpdatePowerShell tests", () => {
12+
it("Can get the latest version", async () => {
1313
const release: GitHubReleaseInformation = await GitHubReleaseInformation.FetchLatestRelease(false);
1414
assert.strictEqual(release.isPreview, false, "expected to not be preview.");
1515
assert.strictEqual(
1616
release.version.prerelease.length === 0, true, "expected to not have preview in version.");
1717
assert.strictEqual(release.assets.length > 0, true, "expected to have assets.");
1818
});
1919

20-
test("Can get the latest preview version", async () => {
20+
it("Can get the latest preview version", async () => {
2121
const release: GitHubReleaseInformation = await GitHubReleaseInformation.FetchLatestRelease(true);
2222
assert.strictEqual(release.isPreview, true, "expected to be preview.");
2323
assert.strictEqual(release.version.prerelease.length > 0, true, "expected to have preview in version.");

test/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import * as glob from "glob";
1111
export function run(): Promise<void> {
1212
// Create the mocha test
1313
const mocha = new Mocha({
14-
ui: "tdd",
1514
color: !process.env.TF_BUILD, // colored output from test results
1615
reporter: "mocha-multi-reporters",
1716
timeout: 30000, // 30s because PowerShell startup is slow!

0 commit comments

Comments
 (0)