From f8865b72c596d48db1430f3ab352bcccabf6eea0 Mon Sep 17 00:00:00 2001 From: Trey Cucco Date: Thu, 11 Jul 2024 14:06:22 -0700 Subject: [PATCH] Add fix message (#3) * Add optional fix message to output * 0.2.0 --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 10 +++++++--- src/types.ts | 1 + src/utils.ts | 20 ++++++++++++++++---- tests/index.test.ts | 12 ++++++++---- 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4827ba2..7e5e843 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@closeio/test-console", - "version": "0.1.2", + "version": "0.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@closeio/test-console", - "version": "0.1.2", + "version": "0.2.0", "license": "MIT", "devDependencies": { "@babel/core": "^7.19.3", diff --git a/package.json b/package.json index 8c7d414..4128d8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@closeio/test-console", - "version": "0.1.2", + "version": "0.2.0", "description": "Library for handling console logs in tests in a flexible way", "keywords": [], "author": "Trey Cucco ", diff --git a/src/index.ts b/src/index.ts index cf22099..299b2b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { LogLevel, LogTest, ConsoleMethods } from './types'; -import { getLogLevel, getTestLocation } from './utils'; +import { getMatchingTest, getTestLocation } from './utils'; import { levels } from './consts'; export interface PatchConsoleMethodsOptions { @@ -33,15 +33,19 @@ const patchConsoleMethods = ( global.console[consoleMethod] = (...args) => { try { - const logLevel = getLogLevel(tests, args); + const match = getMatchingTest(tests, args); - if (logLevel === null || levels[logLevel] >= thresholdValue) { + if (match === null || levels[match.level] >= thresholdValue) { const location = getTestLocation({ filenameRegex, getTestName }); originalMethod( ...args, /* c8 ignore next */ location ? `\n\n${location}` : '', ); + const fixMessage = match?.fix; + if (fixMessage) { + originalMethod(`Proposed fix: ${match.fix}`); + } } } catch (ex) { const location = getTestLocation({ filenameRegex, getTestName }); diff --git a/src/types.ts b/src/types.ts index 07e2740..5fc6291 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,7 @@ export type ConsoleArgs = any[]; export interface LogTest { matcher: Matcher; level: LogLevel; + fix?: string; } export type ConsoleMethod = (args: ConsoleArgs) => void; diff --git a/src/utils.ts b/src/utils.ts index fe251eb..684ba3b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -33,23 +33,35 @@ export const getTestLocation = ({ }; // Given a list of test and the args passed to a console method, returns the -// LogLevel of the first test that matches the args. Or returns null if no +// LogTest of the first test that matches the args. Or returns null if no // tests match the args. -export const getLogLevel = ( +export const getMatchingTest = ( tests: LogTest[], consoleArgs: ConsoleArgs, -): LogLevel | null => { +): LogTest | null => { const args = prepareArgs(consoleArgs); const matchingTest = tests.find(({ matcher }) => argsMatch(matcher, args)); if (matchingTest) { - return matchingTest.level; + return matchingTest; } return null; }; +// Given a list of test and the args passed to a console method, returns the +// LogLevel of the first test that matches the args. Or returns null if no +// tests match the args. +export const getLogLevel = ( + tests: LogTest[], + consoleArgs: ConsoleArgs, +): LogLevel | null => { + const matchingTest = getMatchingTest(tests, consoleArgs); + + return matchingTest?.level ?? null; +}; + // Prepares console.log (and friends) args to be tested against string // and RegExp matchers. // diff --git a/tests/index.test.ts b/tests/index.test.ts index e1494ac..fa9b052 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -16,7 +16,7 @@ describe('public api', () => { [ { matcher: 'is info', level: 'INFO' }, { matcher: 'is warning', level: 'WARNING' }, - { matcher: 'is error', level: 'ERROR' }, + { matcher: 'is error', level: 'ERROR', fix: 'this is a fix' }, ], { filenameRegex: /\.test\.[jt]sx?/, @@ -36,21 +36,25 @@ describe('public api', () => { describe('at threshold', () => { it('logs the message', () => { console.log('is warning'); - expect(logSpy).toHaveBeenCalled(); + expect(logSpy).toHaveBeenCalledTimes(1); }); }); describe('above threshold', () => { it('logs the message', () => { console.error('is error'); - expect(errorSpy).toHaveBeenCalled(); + expect(errorSpy).toHaveBeenCalledTimes(2); + expect(errorSpy).toHaveBeenNthCalledWith( + 2, + 'Proposed fix: this is a fix', + ); }); }); describe('no log matcher', () => { it('logs the message', () => { console.error('nothing matches this'); - expect(errorSpy).toHaveBeenCalled(); + expect(errorSpy).toHaveBeenCalledTimes(1); }); });