Skip to content

Commit

Permalink
Make it easier to have unit tests in VSCode packages
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Jun 19, 2023
1 parent a8ea10d commit 40876ec
Show file tree
Hide file tree
Showing 25 changed files with 26 additions and 31 deletions.
2 changes: 2 additions & 0 deletions docs/contributing/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Run the `workbench.action.debug.selectandstart` command and then select

See [test-case-recorder.md](./test-case-recorder.md).

It is also possible to write manual tests. When doing so, we have a convention that any test that must be run within a VSCode context (eg because it imports `"vscode"`), should be placed in a file with the suffix `.vscode.test.ts`. All other tests should end with just `.test.ts`. This allows us to run non-VSCode tests locally outside of VSCode using the `Run unit tests` launch config. These tests run much faster than the full VSCode test suite.

## Parse tree support

### Adding a new programming language
Expand Down
2 changes: 1 addition & 1 deletion packages/test-harness/src/runners/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import { runAllTestsInDir } from "../util/runAllTestsInDir";
* @returns A promise that resolves when tests have finished running
*/
export function run(): Promise<void> {
return runAllTestsInDir(path.join(getCursorlessRepoRoot(), "packages"));
return runAllTestsInDir(path.join(getCursorlessRepoRoot(), "packages"), true);
}
15 changes: 0 additions & 15 deletions packages/test-harness/src/runners/endToEndOnly.ts

This file was deleted.

15 changes: 8 additions & 7 deletions packages/test-harness/src/runners/unitTestsOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

import { getCursorlessRepoRoot } from "@cursorless/common";
import * as path from "path";
import { runAllTestsInDirs } from "../util/runAllTestsInDir";

const testDirectories = ["cursorless-engine", "common"];
import { runAllTestsInDir } from "../util/runAllTestsInDir";

/**
* Runs all tests that don't have to be run within VSCode.
* @returns A promise that resolves when tests have finished running
*/
export function run(): Promise<void> {
return runAllTestsInDirs(
testDirectories.map((testDirectory) =>
path.resolve(getCursorlessRepoRoot(), `packages/${testDirectory}`),
),
return runAllTestsInDir(
path.join(getCursorlessRepoRoot(), "packages"),
false,
);
}
5 changes: 1 addition & 4 deletions packages/test-harness/src/scripts/runTestsCI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { launchVscodeAndRunTests } from "../util/launchVscodeAndRunTests";

(async function main() {
// Note that we run all tests, including unit tests, in VSCode, even though
// unit tests could be run separately. If we wanted to run unit tests
// separately, we could instead use `../runners/endToEndOnly` instead of
// `../runners/all` and then just call `await runUnitTests()` beforehand to
// run the unit tests directly, instead of as part of VSCode runner.
// unit tests could be run separately.
const extensionTestsPath = path.resolve(
getCursorlessRepoRoot(),
"packages/test-harness/out/runners/all",
Expand Down
18 changes: 14 additions & 4 deletions packages/test-harness/src/util/runAllTestsInDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { promisify } from "util";

const glob = promisify(globRaw);

export function runAllTestsInDir(testRoot: string) {
return runAllTestsInDirs([testRoot]);
export function runAllTestsInDir(
testRoot: string,
includeVscodeTests: boolean,
) {
return runAllTestsInDirs([testRoot], includeVscodeTests);
}

export async function runAllTestsInDirs(testRoots: string[]): Promise<void> {
export async function runAllTestsInDirs(
testRoots: string[],
includeVscodeTests: boolean,
): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: "tdd",
Expand All @@ -19,7 +25,11 @@ export async function runAllTestsInDirs(testRoots: string[]): Promise<void> {
});

for (const testRoot of testRoots) {
const files = await glob("**/**.test.js", { cwd: testRoot });
let files = await glob("**/**.test.js", { cwd: testRoot });

if (!includeVscodeTests) {
files = files.filter((f) => !f.endsWith("vscode.test.js"));
}

// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testRoot, f)));
Expand Down

0 comments on commit 40876ec

Please sign in to comment.