# @90poe/jest-performant-warnings-stats-reporter

This is a custom jest reporter that recursively collects all warnings while tests run and notifying user _afterwards_ improving performance.

## Motivation

We had problems with:

1. Slow tests run on CI even with silent mode
1. When running tests locally noise in the terminal slows tests enormously

## List of superpowers

- more performant than default one (check [benchmark](#benchmarks))
- can provide statistics on warnings for every test suite (see [example](#showwarningsummary-true))
- can show slowest tests (see [example](#showslowest-true))

## Installation

```shell
yarn add @90poe/jest-performant-warnings-stats-reporter --dev
```

or

```shell
npm install @90poe/jest-performant-warnings-stats-reporter --save-dev
```

## Usage

In your jest config add the following entry:

```javascript
{
  reporters: [
        [
            '@90poe/jest-performant-warnings-stats-reporter',
            {
                showSlowest: false,
                maxSlowTests: 5,
                warnOnSlowerThan: 1000,
                showWarningSummary: false,
                warningsConfig: [
                    {
                        name: 'act(...) warnings',
                        regex: /act(...)/im,
                    },
                    {
                        name: 'React Key Issues',
                        regex: /Encountered two children with the same key/im,
                    },
                    // ...
                ],
            },
        ],
    ],
}
```

## Configuration

`@90poe/jest-performant-warnings-stats-reporter` configuration is based on [Jest](https://facebook.github.io/jest/docs/en/configuration.html), available options are shown below.

|       Option       | Description                                                                                                                                                                                            |                    type                     | Default value |
| :----------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-----------------------------------------: | :-----------: |
|    showSlowest     | `true / false` flag whether to show slowest tests or not                                                                                                                                               |                  `boolean`                  |    `false`    |
|    maxSlowTests    | quantity of slowest tests to show, dependant on `showSlowest` flag                                                                                                                                     |                  `number`                   |      `5`      |
|  warnOnSlowerThan  | a limit in milliseconds, used to identify slow tests                                                                                                                                                   |                  `number`                   |     `400`     |
| showWarningSummary | `true / false` flag whether to show detailed information on warnings                                                                                                                                   |                  `boolean`                  |    `false`    |
|   warningsConfig   | an `array` of objects with `name` and `regexp` props, where `name` is title for a warning and `regexp` is regular expression used to match the console warning. Dependant on `showWarningSummary` flag | Array<{ name: `string`, regexp: `RegExp` }> |      []       |

## Sample of output for different configurations

### showSlowest: true

<figure>
    <img src="./examples/showSlowest_true.png" width="650" alt="Sample of output if showSlowest=true">
</figure>

### showWarningSummary: false

<figure>
    <img src="./examples/showWarningSummary_false.png" width="450" alt="Sample of output if showWarningSummary=false">
</figure>

### showWarningSummary: true

<figure>
    <img src="./examples/showWarningSummary_true.png" width="450" alt="Sample of output if showWarningSummary=true">
</figure>

## Benchmarks

Using [hyperfine](https://github.com/sharkdp/hyperfine) for running 130 test suites in real world project, we can see advantage in performance comparing with:

- default reporter (up to 20% improvement possible, varies by the number of warnings that exist)
- default reporter with silent mode on (up to 5% improvement)

### default jest reporter

<figure>
    <img src="./examples/defaultReporter.png" alt="Sample of benchmark for default jest reporter">
</figure>

### default jest reporter with silent mode on

<figure>
    <img src="./examples/defaultReporter_silent.png" alt="Sample of benchmark for default jest reporter with silent mode on">
</figure>

### @90poe/jest-performant-warnings-stats-reporter

<figure>
    <img src="./examples/jest-performant-warnings-stats-reporter.png" alt="Sample of benchmark for @90poe/jest-performant-warnings-stats-reporter">
</figure>

## Contribution

[Document](ISSUE_TEMPLATE.md)

## Code of conduct

[Document](CODE_OF_CONDUCT.md)

## Licence

[MIT](LICENSE.md)
