Skip to content

Load unit tests more consistently (pulled from ESM branch) #58481

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 3 commits into from
May 9, 2024
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
6 changes: 0 additions & 6 deletions src/testRunner/_namespaces/Harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ export * from "../fourslashRunner.js";
export * from "../compilerRunner.js";
export * from "../transpileRunner.js";
export * from "../runner.js";

// If running as emitted CJS, don't start executing the tests here; instead start in runner.ts.
// If running bundled, we want this to be here so that esbuild places the tests after runner.ts.
if (!__filename.endsWith("Harness.js")) {
require("../tests.js");
}
4 changes: 2 additions & 2 deletions src/testRunner/parallel/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import * as ts from "../_namespaces/ts.js";
import * as Utils from "../_namespaces/Utils.js";

export function start() {
export function start(importTests: () => Promise<unknown>) {
const Mocha = require("mocha") as typeof import("mocha");
const Base = Mocha.reporters.Base;
const color = Base.color;
Expand Down Expand Up @@ -656,5 +656,5 @@ export function start() {
shimNoopTestInterface(global);
}

setTimeout(() => startDelayed(perfData, totalCost), 0); // Do real startup on next tick, so all unit tests have been collected
importTests().then(() => startDelayed(perfData, totalCost));
}
9 changes: 7 additions & 2 deletions src/testRunner/parallel/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
UnitTestTask,
} from "../_namespaces/Harness.Parallel.js";

export function start() {
export function start(importTests: () => Promise<unknown>) {
// This brings in the tests after we finish setting things up and yield to the event loop.
const importTestsPromise = importTests();

function hookUncaughtExceptions() {
if (!exceptionsHooked) {
process.on("uncaughtException", handleUncaughtException);
Expand Down Expand Up @@ -277,7 +280,9 @@ export function start() {
return !!tasks && Array.isArray(tasks) && tasks.length > 0 && tasks.every(validateTest);
}

function processHostMessage(message: ParallelHostMessage) {
async function processHostMessage(message: ParallelHostMessage) {
await importTestsPromise;

if (!validateHostMessage(message)) {
console.log("Invalid message:", message);
return;
Expand Down
17 changes: 7 additions & 10 deletions src/testRunner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,27 +249,24 @@ function beginTests() {
}
}

function importTests() {
return import("./tests.js");
}

export let isWorker: boolean;
function startTestEnvironment() {
// For debugging convenience.
(globalThis as any).ts = ts;

isWorker = handleTestConfig();
if (isWorker) {
return Parallel.Worker.start();
return Parallel.Worker.start(importTests);
}
else if (taskConfigsFolder && workerCount && workerCount > 1) {
return Parallel.Host.start();
return Parallel.Host.start(importTests);
}
beginTests();
importTests();
}

startTestEnvironment();

// This brings in all of the unittests.

// If running as emitted CJS, we want to start the tests here after startTestEnvironment.
// If running bundled, we will do this in Harness.ts.
if (__filename.endsWith("runner.js")) {
require("./tests.js");
}