From f716c4874dc45200cfa229490e1c389dc0880750 Mon Sep 17 00:00:00 2001 From: Wilson Ler Date: Wed, 24 Aug 2022 17:42:11 +0800 Subject: [PATCH] Param to skip test names when specified (#25) --- README.md | 10 ++++++++++ index.d.ts | 5 +++++ index.js | 3 +++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 943c3ad..ea922e9 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,16 @@ failOnConsole({ }) ``` +### skipTestNames + +Use this if you want to ignore checks introduced by this library for specific test names. + +```ts +failOnConsole({ + skipTestNames: ['TEST_NAME'], +}) +``` + ## License [MIT](https://github.com/ValentinH/jest-fail-on-console/blob/master/LICENSE) diff --git a/index.d.ts b/index.d.ts index 5fd8c04..5498a57 100644 --- a/index.d.ts +++ b/index.d.ts @@ -37,6 +37,11 @@ declare namespace init { message: string, methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn' ) => boolean + + /** + * This parameter lets you define a list of test names to skip console checks for. + */ + skipTestNames?: string[] } } diff --git a/index.js b/index.js index 853ed2a..155f8ed 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ const init = ({ shouldFailOnInfo = false, shouldFailOnLog = false, shouldFailOnWarn = true, + skipTestNames = [], silenceMessage, } = {}) => { const flushUnexpectedConsoleCalls = (methodName, unexpectedConsoleCallStacks) => { @@ -71,11 +72,13 @@ const init = ({ let originalMethod = console[methodName] beforeEach(() => { + if (skipTestNames.includes(expect.getState().currentTestName)) return console[methodName] = newMethod // eslint-disable-line no-console unexpectedConsoleCallStacks.length = 0 }) afterEach(() => { + if (skipTestNames.includes(expect.getState().currentTestName)) return flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks) console[methodName] = originalMethod })