Skip to content

Commit 1298032

Browse files
committed
Allow filtering messages by stack trace/origin
1 parent ed59356 commit 1298032

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ they should be grouped in the summary.
101101

102102
Each rule has three options, `match`, `group` and (optionally) `keep`.
103103

104-
#### `rule.match : RegExp | string | (message, level) => boolean`
104+
#### `rule.match : RegExp | string | (message, level, origin) => boolean`
105105

106106
`match` is either a regular expression, a string, or a predicate function:
107107

@@ -110,6 +110,7 @@ Each rule has three options, `match`, `group` and (optionally) `keep`.
110110
- A predicate function that's called with `match(message, level)` where
111111
- `message` is the full console message
112112
- `level` is the log level (error, warning, log etc..).
113+
- `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.
113114
- To match this message, the predicate may return any truthy value.
114115

115116
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
215216
}
216217
```
217218

219+
### Can I ignore random `console.error`s from a specific library?
220+
221+
Yes, `console.error` comes with an `origin` property that contains the full stack trace
222+
at the time of logging, which you should be able to use to filter per library, or even per file and line!
223+
224+
The origin may not be available for other log types, so check it before you use it.
225+
226+
```js
227+
{
228+
match: (_message, _type, origin) =>
229+
origin && /node_modules\/rc-form\/lib\/createBaseForm/g.test(origin),
230+
group: 'rc-form validation warnings'
231+
},
232+
```
233+
218234
### Can I help make this library better?
219235

220236
Yes, see [Contibuting](#contributing).

lib/CleanConsoleReporter.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ class CleanConsoleReporter extends DefaultReporter {
3838
const retain = [];
3939

4040
for (const frame of consoleBuffer) {
41-
const { type, message } = frame;
42-
4341
// Check if this a known type message
44-
const [key, keep] = getLogGroupKey(rules, message, type);
42+
const [key, keep] = getLogGroupKey(rules, frame);
4543
if (key) {
46-
this.groupMessageByKey(type, key);
44+
this.groupMessageByKey(frame.type, key);
4745
if (keep) {
4846
retain.push(frame);
4947
}

lib/getLogGroupKey.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global module */
2-
const matchWith = (matcher, message, type) => {
2+
const matchWith = (matcher, message, type, origin) => {
33
if (matcher instanceof RegExp) {
44
return matcher.test(message);
55
}
@@ -11,7 +11,7 @@ const matchWith = (matcher, message, type) => {
1111
}
1212
}
1313
if (typeof matcher === "function") {
14-
return matcher(message, type);
14+
return matcher(message, type, origin);
1515
}
1616

1717
throw new Error("Filter must be a string, function or a regular expression");
@@ -37,9 +37,9 @@ const formatMessage = (formatter, message, type, matcher) => {
3737
return message;
3838
};
3939

40-
const getLogGroupKey = (rules, message, type) => {
40+
const getLogGroupKey = (rules, { message, type, origin }) => {
4141
for (let { match: matcher, group: formatter, keep = false } of rules) {
42-
if (matchWith(matcher, message, type)) {
42+
if (matchWith(matcher, message, type, origin)) {
4343
return [formatMessage(formatter, message, type, matcher), keep];
4444
}
4545
}

0 commit comments

Comments
 (0)