Skip to content
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

Add fix message #3

Merged
merged 2 commits into from
Jul 11, 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 });
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ConsoleArgs = any[];
export interface LogTest {
matcher: Matcher;
level: LogLevel;
fix?: string;
}

export type ConsoleMethod = (args: ConsoleArgs) => void;
Expand Down
20 changes: 16 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
12 changes: 8 additions & 4 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?/,
Expand All @@ -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);
});
});

Expand Down
Loading