Skip to content

Commit

Permalink
Implement "keep" option
Browse files Browse the repository at this point in the history
  • Loading branch information
jevakallio committed Jan 18, 2021
1 parent b72b53a commit c7a6aaa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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

0 comments on commit c7a6aaa

Please sign in to comment.