Skip to content

Commit

Permalink
Allow filtering messages by stack trace/origin
Browse files Browse the repository at this point in the history
  • Loading branch information
jevakallio committed Jan 19, 2021
1 parent ed59356 commit 1298032
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
6 changes: 2 additions & 4 deletions lib/CleanConsoleReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/getLogGroupKey.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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");
Expand All @@ -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];
}
}
Expand Down

0 comments on commit 1298032

Please sign in to comment.