diff --git a/README.md b/README.md index b672e9d..63a8265 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ they should be grouped in the summary. Each rule has three options, `match`, `group` and (optionally) `keep`. -#### `rule.match : RegExp | string | (message, level) => boolean` +#### `rule.match : RegExp | string | (message, level, origin) => boolean` `match` is either a regular expression, a string, or a predicate function: @@ -110,6 +110,7 @@ Each rule has three options, `match`, `group` and (optionally) `keep`. - A predicate function that's called with `match(message, level)` where - `message` is the full console message - `level` is the log level (error, warning, log etc..). + - `origin` is the stack trace string for this error. Useful if you want to ignore all errors from a certain library, for example. Note that this string can contain newlines, so any regexes used to match it should use the `/g` flag. - To match this message, the predicate may return any truthy value. Rules are matched in order, from top down. A message that is not matched by any rule will be displayed in the Jest test output as normal. @@ -215,6 +216,21 @@ You can use the grouping function, where the original matcher is provided as a t } ``` +### Can I ignore random `console.error`s from a specific library? + +Yes, `console.error` comes with an `origin` property that contains the full stack trace +at the time of logging, which you should be able to use to filter per library, or even per file and line! + +The origin may not be available for other log types, so check it before you use it. + +```js + { + match: (_message, _type, origin) => + origin && /node_modules\/rc-form\/lib\/createBaseForm/g.test(origin), + group: 'rc-form validation warnings' + }, +``` + ### Can I help make this library better? Yes, see [Contibuting](#contributing). diff --git a/lib/CleanConsoleReporter.js b/lib/CleanConsoleReporter.js index 9b35c65..2143319 100644 --- a/lib/CleanConsoleReporter.js +++ b/lib/CleanConsoleReporter.js @@ -38,12 +38,10 @@ class CleanConsoleReporter extends DefaultReporter { const retain = []; for (const frame of consoleBuffer) { - const { type, message } = frame; - // Check if this a known type message - const [key, keep] = getLogGroupKey(rules, message, type); + const [key, keep] = getLogGroupKey(rules, frame); if (key) { - this.groupMessageByKey(type, key); + this.groupMessageByKey(frame.type, key); if (keep) { retain.push(frame); } diff --git a/lib/getLogGroupKey.js b/lib/getLogGroupKey.js index 0fb17ef..2fe6439 100644 --- a/lib/getLogGroupKey.js +++ b/lib/getLogGroupKey.js @@ -1,5 +1,5 @@ /* global module */ -const matchWith = (matcher, message, type) => { +const matchWith = (matcher, message, type, origin) => { if (matcher instanceof RegExp) { return matcher.test(message); } @@ -11,7 +11,7 @@ const matchWith = (matcher, message, type) => { } } if (typeof matcher === "function") { - return matcher(message, type); + return matcher(message, type, origin); } throw new Error("Filter must be a string, function or a regular expression"); @@ -37,9 +37,9 @@ const formatMessage = (formatter, message, type, matcher) => { return message; }; -const getLogGroupKey = (rules, message, type) => { +const getLogGroupKey = (rules, { message, type, origin }) => { for (let { match: matcher, group: formatter, keep = false } of rules) { - if (matchWith(matcher, message, type)) { + if (matchWith(matcher, message, type, origin)) { return [formatMessage(formatter, message, type, matcher), keep]; } }