diff --git a/README.md b/README.md index 333832a..6947a77 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ In a file used in the `setupFilesAfterEnv` option of Jest, add this code: import failOnConsole from 'jest-fail-on-console' failOnConsole() + +// or with options: +failOnConsole({ + shouldFailOnWarn: false, +}) ``` ## But I have some expected console errors/warning @@ -47,17 +52,32 @@ test('should log an error', () => { You can pass an object with options to the function: -### ignoreError +### shouldFailOnWarn + +Use this to make a test fail when a warning is logged. + +- Type: `boolean` +- Default: `true` + +### shouldFailOnError + +Use this to make a test fail when an error is logged. + +- Type: `boolean` +- Default: `true` + +### silenceMessage -Signature: `(errorMessage: string) => boolean` +- Signature: `(message: string, methodName: 'warn' | 'error') => boolean` -This will be call for every error. If you return true, the test will not fail. +This function is called for every console warn/error. +If true is returned, the message will not show in the console and the test won't fail. Example: ```ts failOnConsole({ - ignoreError: (errorMessage) => { + silenceMessage: (errorMessage) => { if (/Not implemented: navigation/.test(errorMessage)) { return true } diff --git a/index.d.ts b/index.d.ts index 012dcc5..6492b89 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,14 @@ export type InitOptions = { - ignoreError?: (errorMessage: string) => boolean + /** + * This function is called for every console warn/error. + * If true is returned, the message will not show in the console + * and the test won't fail. + */ + silenceMessage?: (message: string, methodName: 'warn' | 'error') => boolean + /** defaults to true */ + shouldFailOnWarn?: boolean + /** defaults to true */ + shouldFailOnError?: boolean } declare function init(options?: InitOptions): void diff --git a/index.js b/index.js index 85e25b5..e031716 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ const util = require('util') const chalk = require('chalk') -const init = ({ ignoreError }) => { +const init = ({ silenceMessage, shouldFailOnWarn = true, shouldFailOnError = true }) => { const patchConsoleMethod = (methodName, unexpectedConsoleCallStacks) => { const newMethod = (format, ...args) => { - if (ignoreError && ignoreError(format)) { + if (silenceMessage && silenceMessage(format, methodName)) { return } @@ -66,12 +66,21 @@ const init = ({ ignoreError }) => { const unexpectedErrorCallStacks = [] const unexpectedWarnCallStacks = [] - const errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks) - const warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks) + let errorMethod, warnMethod + if (shouldFailOnError) { + errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks) + } + if (shouldFailOnWarn) { + warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks) + } const flushAllUnexpectedConsoleCalls = () => { - flushUnexpectedConsoleCalls(errorMethod, 'error', unexpectedErrorCallStacks) - flushUnexpectedConsoleCalls(warnMethod, 'warn', unexpectedWarnCallStacks) + if (shouldFailOnError) { + flushUnexpectedConsoleCalls(errorMethod, 'error', unexpectedErrorCallStacks) + } + if (shouldFailOnWarn) { + flushUnexpectedConsoleCalls(warnMethod, 'warn', unexpectedWarnCallStacks) + } unexpectedErrorCallStacks.length = 0 unexpectedWarnCallStacks.length = 0 } diff --git a/package.json b/package.json index b3a7b4f..9d36e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jest-fail-on-console", - "version": "1.0.1", + "version": "2.0.0", "description": "Utility to make jest tests fail when console.error() or console.warn() are used", "main": "index.js", "repository": "git@github.com:ricardo-ch/jest-fail-on-console.git",