From 7d6d0d5524fb4efcef265d8cf197eb22d58785f6 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 8 May 2024 21:49:17 -0700 Subject: [PATCH 1/3] Load unit tests more consistently (pulled from ESM branch) --- src/testRunner/_namespaces/Harness.ts | 6 ------ src/testRunner/parallel/host.ts | 5 +++-- src/testRunner/parallel/worker.ts | 7 +++++-- src/testRunner/runner.ts | 17 +++++++---------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/testRunner/_namespaces/Harness.ts b/src/testRunner/_namespaces/Harness.ts index 8648f7ddf675c..fd50d80a5621f 100644 --- a/src/testRunner/_namespaces/Harness.ts +++ b/src/testRunner/_namespaces/Harness.ts @@ -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"); -} diff --git a/src/testRunner/parallel/host.ts b/src/testRunner/parallel/host.ts index d1ff118ba0482..d41473efe414a 100644 --- a/src/testRunner/parallel/host.ts +++ b/src/testRunner/parallel/host.ts @@ -25,7 +25,7 @@ import { import * as ts from "../_namespaces/ts.js"; import * as Utils from "../_namespaces/Utils.js"; -export function start() { +export async function start(importTests: () => Promise) { const Mocha = require("mocha") as typeof import("mocha"); const Base = Mocha.reporters.Base; const color = Base.color; @@ -656,5 +656,6 @@ export function start() { shimNoopTestInterface(global); } - setTimeout(() => startDelayed(perfData, totalCost), 0); // Do real startup on next tick, so all unit tests have been collected + await importTests(); + setTimeout(() => startDelayed(perfData, totalCost), 0); } diff --git a/src/testRunner/parallel/worker.ts b/src/testRunner/parallel/worker.ts index ee0f699eea865..4f0a517bbd153 100644 --- a/src/testRunner/parallel/worker.ts +++ b/src/testRunner/parallel/worker.ts @@ -16,7 +16,7 @@ import { UnitTestTask, } from "../_namespaces/Harness.Parallel.js"; -export function start() { +export function start(importTests: () => Promise) { function hookUncaughtExceptions() { if (!exceptionsHooked) { process.on("uncaughtException", handleUncaughtException); @@ -277,7 +277,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; @@ -338,4 +340,5 @@ export function start() { } process.on("message", processHostMessage); + const importTestsPromise = importTests(); } diff --git a/src/testRunner/runner.ts b/src/testRunner/runner.ts index 819a9026ba03a..12fc1308ecba7 100644 --- a/src/testRunner/runner.ts +++ b/src/testRunner/runner.ts @@ -249,6 +249,10 @@ function beginTests() { } } +function importTests() { + return import("./tests.js"); +} + export let isWorker: boolean; function startTestEnvironment() { // For debugging convenience. @@ -256,20 +260,13 @@ function startTestEnvironment() { 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"); -} From 820d2858ed21c247a7f872655d92a8e52bab7b96 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 9 May 2024 09:51:07 -0700 Subject: [PATCH 2/3] Lift the import, because it won't acutally happen until yield --- src/testRunner/parallel/worker.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/testRunner/parallel/worker.ts b/src/testRunner/parallel/worker.ts index 4f0a517bbd153..4b61633e45a53 100644 --- a/src/testRunner/parallel/worker.ts +++ b/src/testRunner/parallel/worker.ts @@ -17,6 +17,9 @@ import { } from "../_namespaces/Harness.Parallel.js"; export function start(importTests: () => Promise) { + // 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); @@ -340,5 +343,4 @@ export function start(importTests: () => Promise) { } process.on("message", processHostMessage); - const importTestsPromise = importTests(); } From 204ba7ab5fdd6a4b9279dc2891eb90a998ddbc4b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 9 May 2024 09:53:33 -0700 Subject: [PATCH 3/3] De-async the host start func too --- src/testRunner/parallel/host.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/testRunner/parallel/host.ts b/src/testRunner/parallel/host.ts index d41473efe414a..cbfc9b391c491 100644 --- a/src/testRunner/parallel/host.ts +++ b/src/testRunner/parallel/host.ts @@ -25,7 +25,7 @@ import { import * as ts from "../_namespaces/ts.js"; import * as Utils from "../_namespaces/Utils.js"; -export async function start(importTests: () => Promise) { +export function start(importTests: () => Promise) { const Mocha = require("mocha") as typeof import("mocha"); const Base = Mocha.reporters.Base; const color = Base.color; @@ -656,6 +656,5 @@ export async function start(importTests: () => Promise) { shimNoopTestInterface(global); } - await importTests(); - setTimeout(() => startDelayed(perfData, totalCost), 0); + importTests().then(() => startDelayed(perfData, totalCost)); }