Skip to content

Commit 40876ec

Browse files
committed
Make it easier to have unit tests in VSCode packages
1 parent a8ea10d commit 40876ec

25 files changed

+26
-31
lines changed

docs/contributing/CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Run the `workbench.action.debug.selectandstart` command and then select
7676

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

79+
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.
80+
7981
## Parse tree support
8082

8183
### Adding a new programming language

packages/test-harness/src/runners/all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { runAllTestsInDir } from "../util/runAllTestsInDir";
99
* @returns A promise that resolves when tests have finished running
1010
*/
1111
export function run(): Promise<void> {
12-
return runAllTestsInDir(path.join(getCursorlessRepoRoot(), "packages"));
12+
return runAllTestsInDir(path.join(getCursorlessRepoRoot(), "packages"), true);
1313
}

packages/test-harness/src/runners/endToEndOnly.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/test-harness/src/runners/unitTestsOnly.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
import { getCursorlessRepoRoot } from "@cursorless/common";
55
import * as path from "path";
6-
import { runAllTestsInDirs } from "../util/runAllTestsInDir";
7-
8-
const testDirectories = ["cursorless-engine", "common"];
6+
import { runAllTestsInDir } from "../util/runAllTestsInDir";
97

8+
/**
9+
* Runs all tests that don't have to be run within VSCode.
10+
* @returns A promise that resolves when tests have finished running
11+
*/
1012
export function run(): Promise<void> {
11-
return runAllTestsInDirs(
12-
testDirectories.map((testDirectory) =>
13-
path.resolve(getCursorlessRepoRoot(), `packages/${testDirectory}`),
14-
),
13+
return runAllTestsInDir(
14+
path.join(getCursorlessRepoRoot(), "packages"),
15+
false,
1516
);
1617
}

packages/test-harness/src/scripts/runTestsCI.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import { launchVscodeAndRunTests } from "../util/launchVscodeAndRunTests";
88

99
(async function main() {
1010
// Note that we run all tests, including unit tests, in VSCode, even though
11-
// unit tests could be run separately. If we wanted to run unit tests
12-
// separately, we could instead use `../runners/endToEndOnly` instead of
13-
// `../runners/all` and then just call `await runUnitTests()` beforehand to
14-
// run the unit tests directly, instead of as part of VSCode runner.
11+
// unit tests could be run separately.
1512
const extensionTestsPath = path.resolve(
1613
getCursorlessRepoRoot(),
1714
"packages/test-harness/out/runners/all",

packages/test-harness/src/util/runAllTestsInDir.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import { promisify } from "util";
66

77
const glob = promisify(globRaw);
88

9-
export function runAllTestsInDir(testRoot: string) {
10-
return runAllTestsInDirs([testRoot]);
9+
export function runAllTestsInDir(
10+
testRoot: string,
11+
includeVscodeTests: boolean,
12+
) {
13+
return runAllTestsInDirs([testRoot], includeVscodeTests);
1114
}
1215

13-
export async function runAllTestsInDirs(testRoots: string[]): Promise<void> {
16+
export async function runAllTestsInDirs(
17+
testRoots: string[],
18+
includeVscodeTests: boolean,
19+
): Promise<void> {
1420
// Create the mocha test
1521
const mocha = new Mocha({
1622
ui: "tdd",
@@ -19,7 +25,11 @@ export async function runAllTestsInDirs(testRoots: string[]): Promise<void> {
1925
});
2026

2127
for (const testRoot of testRoots) {
22-
const files = await glob("**/**.test.js", { cwd: testRoot });
28+
let files = await glob("**/**.test.js", { cwd: testRoot });
29+
30+
if (!includeVscodeTests) {
31+
files = files.filter((f) => !f.endsWith("vscode.test.js"));
32+
}
2333

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

0 commit comments

Comments
 (0)