From c7a6aaa50de6d31257f1ed6416aede6443813360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jani=20Ev=C3=A4kallio?= Date: Mon, 18 Jan 2021 20:15:36 +0000 Subject: [PATCH] Implement "keep" option --- README.md | 26 +++++++++++++++++--------- lib/CleanConsoleReporter.js | 11 +++++++---- lib/getLogGroupKey.js | 6 ++++-- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3f99460..deda5c2 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ const jestConfig = { Rules tell the reporter which console messages should be filtered, and how they should be grouped in the summary. -Each rule has two parts, `match` and `group`. +Each rule has three options, `match`, `group` and (optionally) `keep`. #### `rule.match : RegExp | string | (message, level) => boolean` @@ -123,6 +123,10 @@ Matched messages are grouped according to the `group` property: - `matcher` is the original matcher used to match this message. This can be useful if you want to e.g. execute the regular expression for capture groups. - The value returned by this function is used as capture key. If the function returns `null`, the message is ignored. +### `rule.keep: boolean` + +Setting `keep: true` option allows you to keep the original console output for this group intact, while also displaying it in the test run summary. + ### options.levels Define which log levels to display in the summary at the end of the test run: @@ -131,14 +135,6 @@ Default: `["error", "warn", "info", "debug", "log"]` These levels only affect the summary display, and have no effect on whether messages are matched. For that, see [Can I ignore all messages of certain log level?](#can-i-ignore-all-messages-of-certain-log-level). -```js -const knownWarnings = [ - // ...other rules... - // this rule should be defined last - { match: (message, level) => level === "log", group: null }, -]; -``` - ## Never Asked Questions Here are some questions nobody has ever asked, but might be helpful anyway. @@ -176,6 +172,18 @@ Yes, just give them the same group key: ]; ``` +### Can I temporarily let a certain message through? + +Yes, just set the rule's `keep` property to `true`: + +```js +{ + match: /^Warning: An update to (\w*) inside a test was not wrapped in act/, + group: "An update to (Component) inside a test was not wrapped in act", + keep: true +} +``` + ### Can I use this with .json config? You can use string matchers, which are first compared to the message as literal strings, and failing that, attempted to test against the message as regular expressions: diff --git a/lib/CleanConsoleReporter.js b/lib/CleanConsoleReporter.js index bf8bf37..9b35c65 100644 --- a/lib/CleanConsoleReporter.js +++ b/lib/CleanConsoleReporter.js @@ -35,24 +35,27 @@ class CleanConsoleReporter extends DefaultReporter { filterOutKnownMessages(consoleBuffer = []) { const rules = this.rules; - const unmatched = []; + const retain = []; for (const frame of consoleBuffer) { const { type, message } = frame; // Check if this a known type message - const key = getLogGroupKey(rules, message, type); + const [key, keep] = getLogGroupKey(rules, message, type); if (key) { this.groupMessageByKey(type, key); + if (keep) { + retain.push(frame); + } } else if (key === null) { this.ignored++; } else { - unmatched.push(frame); + retain.push(frame); } } // Based implementation expects undefined instead of empty array - return unmatched.length ? unmatched : undefined; + return retain.length ? retain : undefined; } groupMessageByKey(type, key) { diff --git a/lib/getLogGroupKey.js b/lib/getLogGroupKey.js index 0b55e29..0fb17ef 100644 --- a/lib/getLogGroupKey.js +++ b/lib/getLogGroupKey.js @@ -38,11 +38,13 @@ const formatMessage = (formatter, message, type, matcher) => { }; const getLogGroupKey = (rules, message, type) => { - for (let { match: matcher, group: formatter } of rules) { + for (let { match: matcher, group: formatter, keep = false } of rules) { if (matchWith(matcher, message, type)) { - return formatMessage(formatter, message, type, matcher); + return [formatMessage(formatter, message, type, matcher), keep]; } } + + return []; }; module.exports = getLogGroupKey;