From 728e02a1c222877f3671c9d3dd2cba7ef53fd4ee Mon Sep 17 00:00:00 2001 From: Max Patiiuk Date: Mon, 2 Sep 2024 07:59:40 -0700 Subject: [PATCH] feat: do not capture log messages --- src/index.ts | 78 +++++++++++---------------------------------- src/types.ts | 2 -- tests/index.spec.ts | 2 +- 3 files changed, 20 insertions(+), 62 deletions(-) diff --git a/src/index.ts b/src/index.ts index eec2483..d0f95ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,8 @@ -import { beforeEach, afterEach, expect } from "vitest"; +import { beforeEach, afterEach, expect } from 'vitest'; import * as util from 'util'; import chalk from 'chalk'; -import { - ConsoleCallStacks, - ConsoleMethod, - VitestFailOnConsoleFunction, -} from './types'; +import { ConsoleMethod, VitestFailOnConsoleFunction } from './types'; -const LINE_RETURN = '\n'; const defaultErrorMessage = (methodName: ConsoleMethod) => `vitest-fail-on-console > Expected test not to call ${chalk.bold( `console.${methodName}()` @@ -29,7 +24,7 @@ const init = ( shouldFailOnWarn = true, skipTest = undefined, silenceMessage = undefined, - afterEachDelay = undefined + afterEachDelay = undefined, }: VitestFailOnConsoleFunction = { errorMessage: defaultErrorMessage, shouldFailOnAssert: false, @@ -40,60 +35,24 @@ const init = ( shouldFailOnWarn: true, silenceMessage: undefined, skipTest: undefined, - afterEachDelay: undefined + afterEachDelay: undefined, } ) => { - const flushUnexpectedConsoleCalls = ( - methodName: ConsoleMethod, - unexpectedConsoleCallStacks: ConsoleCallStacks - ) => { - if (unexpectedConsoleCallStacks.length) { - const messages = unexpectedConsoleCallStacks.map( - ([stack, message]) => { - const stackLines = stack.split(LINE_RETURN); - return ( - `${chalk.red(message)}${LINE_RETURN}` + - `${stackLines - .map((line, index) => { - if (index === stackLines.length - 1) { - return chalk.white(line); - } - return chalk.gray(line); - }) - .join(LINE_RETURN)}` - ); - } - ); - - const message = errorMessage(methodName); - const doubleLineReturn = `${LINE_RETURN}${LINE_RETURN}`; - throw new Error( - `${message}${doubleLineReturn}${messages.join( - doubleLineReturn - )}` - ); - } + const flushUnexpectedConsoleCalls = (methodName: ConsoleMethod) => { + throw new Error(errorMessage(methodName)); }; const patchConsoleMethod = (methodName: ConsoleMethod) => { - const unexpectedConsoleCallStacks: ConsoleCallStacks = []; + let hadConsoleCall = false; - const captureMessage = (format: unknown, ...args) => { + const handleConsoleCall = (format: unknown, ...args) => { const message = util.format(format, ...args); if (silenceMessage && silenceMessage(message, methodName)) { return; } - // Capture the call stack now, so we can warn about it later. - // The call stack has helpful information for the test author. - // Don't throw yet though b'c it might be accidentally caught and suppressed. - const { stack } = new Error(); - if (stack) { - unexpectedConsoleCallStacks.push([ - stack.slice(stack.indexOf(LINE_RETURN) + 1), - message, - ]); - } + hadConsoleCall = true; + originalMethod(message); }; const newAssertMethod = ( @@ -104,13 +63,13 @@ const init = ( if (assertion) { return; } - captureMessage(format, ...args); + handleConsoleCall(format, ...args); }; const newMethod = methodName === ConsoleMethod.Assert ? newAssertMethod - : captureMessage; + : handleConsoleCall; const originalMethod = console[methodName]; @@ -126,7 +85,7 @@ const init = ( return; } console[methodName] = newMethod; // eslint-disable-line no-console - unexpectedConsoleCallStacks.length = 0; + hadConsoleCall = false; }); afterEach(async () => { @@ -134,12 +93,13 @@ const init = ( return; } if (afterEachDelay) { - await new Promise(resolve => setTimeout(resolve, afterEachDelay)); + await new Promise((resolve) => + setTimeout(resolve, afterEachDelay) + ); + } + if (hadConsoleCall) { + flushUnexpectedConsoleCalls(methodName); } - flushUnexpectedConsoleCalls( - methodName, - unexpectedConsoleCallStacks - ); console[methodName] = originalMethod; }); }; diff --git a/src/types.ts b/src/types.ts index 02522ee..d9e7650 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,8 +22,6 @@ export enum ConsoleMethod { Warn = 'warn', } -export type ConsoleCallStacks = [string, string][]; - export type VitestFailOnConsoleFunction = { shouldFailOnAssert?: boolean; shouldFailOnDebug?: boolean; diff --git a/tests/index.spec.ts b/tests/index.spec.ts index e8f5429..3418a6e 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -10,7 +10,7 @@ describe('vitest-fail-on-console', () => { const configFilePath = `${fixtureDirectory}/vitest.config.ts`; const cmd = `./node_modules/.bin/vitest related ${testFilePath} -c ${configFilePath} --run`; return new Promise((resolve) => { - exec(cmd, (error, stdout, stderr) => { + exec(cmd, (_error, _stdout, stderr) => { resolve({ stderr }); }); });