Skip to content

Convert Mocha unit tests to BDD style #3607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions test/core/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { suiteSetup } from "mocha";
import utils = require("../utils");

suite("Path assumptions", () => {
suiteSetup(utils.ensureExtensionIsActivated);
describe("Path assumptions", () => {
before(utils.ensureExtensionIsActivated);

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

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

test("The logs folder is created in the right place", async () => {
it("The logs folder is created in the right place", async () => {
assert(fs.existsSync(path.resolve(utils.rootPath, "logs")));
});
});
26 changes: 13 additions & 13 deletions test/core/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ function setupTestEnvironment(testPlatform: ITestPlatform) {
}
}

suite("Platform module", () => {
suite("PlatformDetails", () => {
describe("Platform module", () => {
describe("PlatformDetails", () => {
const platformDetails: platform.IPlatformDetails = platform.getPlatformDetails();
switch (process.platform) {
case "darwin":
Expand Down Expand Up @@ -521,14 +521,14 @@ suite("Platform module", () => {
}
});

suite("Default PowerShell installation", () => {
teardown(() => {
describe("Default PowerShell installation", () => {
afterEach(() => {
sinon.restore();
mockFS.restore();
});

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

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

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

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

suite("Expected PowerShell installation list", () => {
teardown(() => {
describe("Expected PowerShell installation list", () => {
afterEach(() => {
sinon.restore();
mockFS.restore();
});

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

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

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

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

suite("Windows PowerShell path fix", () => {
teardown(() => {
describe("Windows PowerShell path fix", () => {
afterEach(() => {
sinon.restore();
mockFS.restore();
});

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

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

function getWinPSPath(systemDir: string) {
Expand Down
10 changes: 5 additions & 5 deletions test/core/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as assert from "assert";
import * as vscode from "vscode";
import Settings = require("../../src/settings");

suite("Settings module", () => {
test("Settings load without error", () => {
describe("Settings module", () => {
it("Settings load without error", () => {
assert.doesNotThrow(Settings.load);
});

test("Settings update correctly", async () => {
it("Settings update correctly", async () => {
// then syntax
Settings.change("helpCompletion", "BlockComment", false).then(() =>
assert.strictEqual(Settings.load().helpCompletion, "BlockComment"));
Expand All @@ -20,7 +20,7 @@ suite("Settings module", () => {
assert.strictEqual(Settings.load().helpCompletion, "LineComment");
});

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

test("Can get effective configuration target", async () => {
it("Can get effective configuration target", async () => {
await Settings.change("helpCompletion", "LineComment", false);
let target = await Settings.getEffectiveConfigurationTarget("helpCompletion");
assert.strictEqual(target, vscode.ConfigurationTarget.Workspace);
Expand Down
4 changes: 2 additions & 2 deletions test/features/CustomViews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function convertToVSCodeResourceScheme(filePath: string): string {
return vscode.Uri.file(filePath).toString().replace("file://", "vscode-resource://");
}

suite("CustomViews tests", () => {
describe("CustomViews tests", () => {
const testCases: IHtmlContentViewTestCase[] = [
// Basic test that has no js or css.
{
Expand Down Expand Up @@ -108,7 +108,7 @@ hello
];

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

const jsPaths = testCase.javaScriptFiles.map((jsFile) => {
Expand Down
25 changes: 12 additions & 13 deletions test/features/ExternalApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
// Licensed under the MIT License.

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

suite("ExternalApi feature - Registration API", () => {
describe("ExternalApi feature - Registration API", () => {
let powerShellExtensionClient: IPowerShellExtensionClient;
suiteSetup(async () => {
before(async () => {
const powershellExtension = await utils.ensureExtensionIsActivated();
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
});

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

test("It can register and unregister an extension with a version", () => {
it("It can register and unregister an extension with a version", () => {
const sessionId: string = powerShellExtensionClient.registerExternalExtension(utils.extensionId, "v2");
assert.notStrictEqual(sessionId , "");
assert.notStrictEqual(sessionId , null);
Expand All @@ -34,12 +33,12 @@ suite("ExternalApi feature - Registration API", () => {
/*
NEGATIVE TESTS
*/
test("API fails if not registered", async () => {
it("API fails if not registered", async () => {
assert.rejects(
async () => await powerShellExtensionClient.getPowerShellVersionDetails(""))
});

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

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

suite("ExternalApi feature - Other APIs", () => {
describe("ExternalApi feature - Other APIs", () => {
let sessionId: string;
let powerShellExtensionClient: IPowerShellExtensionClient;

suiteSetup(async () => {
before(async () => {
const powershellExtension = await utils.ensureExtensionIsActivated();
powerShellExtensionClient = powershellExtension!.exports as IPowerShellExtensionClient;
});

setup(() => {
beforeEach(() => {
sessionId = powerShellExtensionClient.registerExternalExtension(utils.extensionId);
});

teardown(() => {
afterEach(() => {
powerShellExtensionClient.unregisterExternalExtension(sessionId);
});

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

assert.notStrictEqual(versionDetails.architecture, "");
Expand Down
17 changes: 8 additions & 9 deletions test/features/ISECompatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@

import * as assert from "assert";
import * as vscode from "vscode";
import { suiteSetup, setup, suiteTeardown, teardown } from "mocha";
import { ISECompatibilityFeature } from "../../src/features/ISECompatibility";
import utils = require("../utils");

suite("ISECompatibility feature", () => {
describe("ISECompatibility feature", () => {
let currentTheme: string;

suiteSetup(async () => {
before(async () => {
// Save user's current theme.
currentTheme = await vscode.workspace.getConfiguration("workbench").get("colorTheme");
await utils.ensureExtensionIsActivated();
});

setup(async () => { await vscode.commands.executeCommand("PowerShell.EnableISEMode"); });
beforeEach(async () => { await vscode.commands.executeCommand("PowerShell.EnableISEMode"); });

teardown(async () => { await vscode.commands.executeCommand("PowerShell.DisableISEMode"); });
afterEach(async () => { await vscode.commands.executeCommand("PowerShell.DisableISEMode"); });

suiteTeardown(async () => {
after(async () => {
// Reset user's current theme.
await vscode.workspace.getConfiguration("workbench").update("colorTheme", currentTheme, true);
assert.strictEqual(vscode.workspace.getConfiguration("workbench").get("colorTheme"), currentTheme);
});

test("It sets ISE Settings", async () => {
it("It sets ISE Settings", async () => {
for (const iseSetting of ISECompatibilityFeature.settings) {
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
assert.strictEqual(currently, iseSetting.value);
}
});

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

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

// "Manually" change theme after enabling ISE mode. Use a built-in theme but not the default.
Expand Down
9 changes: 4 additions & 5 deletions test/features/RunCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as fs from "fs";
import * as path from "path";
import rewire = require("rewire");
import vscode = require("vscode");
import { suiteSetup } from "mocha";
import utils = require("../utils");
import { sleep } from "../../src/utils";

Expand All @@ -19,10 +18,10 @@ enum LaunchType {
Run,
}

suite("RunCode tests", () => {
suiteSetup(utils.ensureExtensionIsActivated);
describe("RunCode tests", () => {
before(utils.ensureExtensionIsActivated);

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions test/features/UpdatePowerShell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import { GitHubReleaseInformation } from "../../src/features/UpdatePowerShell";
// the GitHub API rate limit often. Let's skip these tests on macOS until
// they are hooked up to only run on release.
if (process.env.TF_BUILD && process.platform === "win32") {
suite("UpdatePowerShell tests", () => {
test("Can get the latest version", async () => {
describe("UpdatePowerShell tests", () => {
it("Can get the latest version", async () => {
const release: GitHubReleaseInformation = await GitHubReleaseInformation.FetchLatestRelease(false);
assert.strictEqual(release.isPreview, false, "expected to not be preview.");
assert.strictEqual(
release.version.prerelease.length === 0, true, "expected to not have preview in version.");
assert.strictEqual(release.assets.length > 0, true, "expected to have assets.");
});

test("Can get the latest preview version", async () => {
it("Can get the latest preview version", async () => {
const release: GitHubReleaseInformation = await GitHubReleaseInformation.FetchLatestRelease(true);
assert.strictEqual(release.isPreview, true, "expected to be preview.");
assert.strictEqual(release.version.prerelease.length > 0, true, "expected to have preview in version.");
Expand Down
1 change: 0 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as glob from "glob";
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: "tdd",
color: !process.env.TF_BUILD, // colored output from test results
reporter: "mocha-multi-reporters",
timeout: 30000, // 30s because PowerShell startup is slow!
Expand Down