From 407e7b0c620d1bb20ce52ccaa8e8ae555ab3edc4 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Wed, 3 May 2023 14:50:27 -0400 Subject: [PATCH] [snap runner] debug mode --- compiler/forget/packages/snap/src/compiler-worker.ts | 10 +++++++++- compiler/forget/packages/snap/src/runner.ts | 10 ++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/forget/packages/snap/src/compiler-worker.ts b/compiler/forget/packages/snap/src/compiler-worker.ts index df657766e3..cf35707069 100644 --- a/compiler/forget/packages/snap/src/compiler-worker.ts +++ b/compiler/forget/packages/snap/src/compiler-worker.ts @@ -28,9 +28,11 @@ export type TestResult = { export async function compile( compilerPath: string, + loggerPath: string, fixturesDir: string, fixture: string, - compilerVersion: number + compilerVersion: number, + isOnlyFixture: boolean ): Promise { const seenConsoleErrors: Array = []; console.error = (...messages: Array) => { @@ -67,6 +69,12 @@ export async function compile( // NOTE: we intentionally require lazily here so that we can clear the require cache // and load fresh versions of the compiler when `compilerVersion` changes. const { runReactForgetBabelPlugin } = require(compilerPath); + const { toggleLogging } = require(loggerPath); + + // only try logging if we filtered out all but one fixture, + // since console log order is non-deterministic + const shouldLogPragma = input.split("\n")[0].includes("@debug"); + toggleLogging(isOnlyFixture && shouldLogPragma); // Extract the first line to quickly check for custom test directives const firstLine = input.substring(0, input.indexOf("\n")); diff --git a/compiler/forget/packages/snap/src/runner.ts b/compiler/forget/packages/snap/src/runner.ts index 1dc4d438d9..83981f9701 100644 --- a/compiler/forget/packages/snap/src/runner.ts +++ b/compiler/forget/packages/snap/src/runner.ts @@ -28,6 +28,7 @@ const COMPILER_PATH = path.join( "Babel", "RunReactForgetBabelPlugin.js" ); +const LOGGER_PATH = path.join(process.cwd(), "dist", "Utils", "logger.js"); const FIXTURES_PATH = path.join( process.cwd(), "src", @@ -144,6 +145,7 @@ async function run( } else { fixtures = allFixtures; } + const isOnlyFixture = filter !== null && fixtures.length === 1; let entries: Array<[string, TestResult]>; if (!opts.sync) { @@ -152,9 +154,11 @@ async function run( fixtures.map(async (fixture) => { let output = await worker.compile( COMPILER_PATH, + LOGGER_PATH, FIXTURES_PATH, fixture, - compilerVersion + compilerVersion, + isOnlyFixture ); return [fixture, output]; }) @@ -164,9 +168,11 @@ async function run( for (const fixture of fixtures) { let output = await compiler.compile( COMPILER_PATH, + LOGGER_PATH, FIXTURES_PATH, fixture, - compilerVersion + compilerVersion, + isOnlyFixture ); entries.push([fixture, output]); }