Repository: rickhanlonii/jest-silent-reporter
Branch: main
Commit: a1ce53d8cc80
Files: 9
Total size: 8.9 KB
Directory structure:
gitextract_v38xw2bw/
├── .gitignore
├── .prettierrc
├── LICENSE.md
├── README.md
├── SilentReporter.js
├── StdIo.js
├── helpers.js
├── index.js
└── package.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
node_modules/
.DS_store
================================================
FILE: .prettierrc
================================================
{
"proseWrap": "always",
"singleQuote": true,
"trailingComma": "es5"
}
================================================
FILE: LICENSE.md
================================================
MIT License
Copyright (c) 2018 Rick Hanlon II
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
================================================
FILE: README.md
================================================
Jest Silent Reporter
Custom reporter
for Jest that only prints failed tests.
## Installation
Using [npm](https://www.npmjs.com/):
```sh
$ npm i --save-dev jest-silent-reporter
```
Using [yarn](https://yarnpkg.com/):
```sh
$ yarn add --dev jest-silent-reporter
```
## Usage
Jest CLI:
```bash
jest --reporters=jest-silent-reporter
```
Jest config:
```json
{
"reporters": ["jest-silent-reporter"]
}
```
## Options
### useDots: boolean
For large test suites, `jest-silent-reporter` can cause CI to fail due to having
no output for some configured amount of time. Using the `useDots` option will
output dots for each test file, similar to a dot reporter.
```json
{
"reporters": [["jest-silent-reporter", { "useDots": true }]]
}
```
Note: this config is also available as an environment variable `JEST_SILENT_REPORTER_DOTS=true`.
### showWarnings: boolean
Warnings are supressed by default, use `showWarnings` to log them.
```json
{
"reporters": [["jest-silent-reporter", { "showWarnings": true }]]
}
```
Note: this config is also available as an environment variable `JEST_SILENT_REPORTER_SHOW_WARNINGS=true`.
### showPaths: boolean
Sometimes it might come in handy to display the test suites' paths (i.e. when
running tests in a terminal inside IDE for quicker file navigation).
```json
{
"reporters": [["jest-silent-reporter", { "showPaths": true }]]
}
```
Note: this config is also available as an environment variable `JEST_SILENT_REPORTER_SHOW_PATHS=true`.
## Screenshots
#### All tests passed

#### Tests failed

## Licence
MIT
================================================
FILE: SilentReporter.js
================================================
const jestUtils = require('jest-util');
const helpers = require('./helpers');
const StdIo = require('./StdIo');
class SilentReporter {
constructor(globalConfig, options = {}) {
this._globalConfig = globalConfig;
this.stdio = new StdIo();
this.useDots = !!process.env.JEST_SILENT_REPORTER_DOTS || !!options.useDots;
this.showPaths =
!!process.env.JEST_SILENT_REPORTER_SHOW_PATHS || !!options.showPaths;
this.showWarnings =
!!process.env.JEST_SILENT_REPORTER_SHOW_WARNINGS ||
!!options.showWarnings;
this.showSeed = !!globalConfig.showSeed
}
onRunStart() {
if (jestUtils.isInteractive) {
jestUtils.clearLine(process.stderr);
}
}
onRunComplete() {
if (this.useDots) {
this.stdio.log('\n');
}
if (this.showSeed) {
this.stdio.log(`Seed: ${this._globalConfig.seed}`)
}
this.stdio.close();
}
onTestResult(test, testResult) {
if (this.useDots) {
this.stdio.logInline('.');
}
if (!testResult.skipped) {
const didUpdate = this._globalConfig.updateSnapshot === 'all';
let hasSnapshotFailures = false;
if (testResult.snapshot) {
if (!didUpdate && testResult.snapshot.unchecked) {
hasSnapshotFailures = true;
}
if (testResult.snapshot.unmatched) {
hasSnapshotFailures = true;
}
}
const hasFailures = testResult.failureMessage || hasSnapshotFailures;
if (this.showPaths && hasFailures) {
this.stdio.log('\n' + test.path);
}
if (testResult.failureMessage)
this.stdio.log('\n' + testResult.failureMessage);
if (testResult.console && this.showWarnings) {
testResult.console
.filter(entry => ['error', 'warn'].includes(entry.type) && entry.message)
.map(entry => entry.message)
.forEach(this.stdio.log);
}
const snapshotStatuses = helpers.getSnapshotStatus(
testResult.snapshot,
didUpdate
);
snapshotStatuses.forEach(this.stdio.log);
}
}
}
module.exports = SilentReporter;
================================================
FILE: StdIo.js
================================================
class StdIo {
constructor() {
this._out = process.stdout.write.bind(process.stdout);
this._err = process.stderr.write.bind(process.stderr);
this._bufferedOutput = new Set();
this._wrapStdio(process.stdout);
this._wrapStdio(process.stderr);
}
log(message) {
process.stderr.write(message + '\n');
}
logInline(message) {
process.stderr.write(message);
}
close() {
this._forceFlushBufferedOutput();
process.stdout.write = this._out;
process.stderr.write = this._err;
}
// Don't wait for the debounced call and flush all output immediately.
_forceFlushBufferedOutput() {
for (const flushBufferedOutput of this._bufferedOutput) {
flushBufferedOutput();
}
}
_wrapStdio(stream) {
const originalWrite = stream.write;
let buffer = [];
let timeout = null;
const flushBufferedOutput = () => {
const string = buffer.join('');
buffer = [];
if (string) {
originalWrite.call(stream, string);
}
this._bufferedOutput.delete(flushBufferedOutput);
};
this._bufferedOutput.add(flushBufferedOutput);
const debouncedFlush = () => {
// If the process blows up no errors would be printed.
// There should be a smart way to buffer stderr, but for now
// we just won't buffer it.
if (stream === process.stderr) {
flushBufferedOutput();
} else {
if (!timeout) {
timeout = setTimeout(() => {
flushBufferedOutput();
timeout = null;
}, 100);
}
}
};
stream.write = chunk => {
buffer.push(chunk);
debouncedFlush();
return true;
};
}
}
module.exports = StdIo;
================================================
FILE: helpers.js
================================================
const chalk = require('chalk');
const ARROW = ' \u203A ';
const FAIL_COLOR = chalk.bold.red;
const SNAPSHOT_ADDED = chalk.bold.green;
const SNAPSHOT_REMOVED = chalk.bold.red;
const SNAPSHOT_UPDATED = chalk.bold.green;
const pluralize = (word, count) => `${count} ${word}${count === 1 ? '' : 's'}`;
const getSnapshotStatus = (snapshot, afterUpdate) => {
const statuses = [];
if (snapshot.added) {
statuses.push(
SNAPSHOT_ADDED(ARROW + pluralize('snapshot', snapshot.added)) +
' written.'
);
}
if (snapshot.updated) {
statuses.push(
SNAPSHOT_UPDATED(ARROW + pluralize('snapshot', snapshot.updated)) +
` updated.`
);
}
if (snapshot.unchecked) {
statuses.push(
FAIL_COLOR(ARROW + pluralize('obsolete snapshot', snapshot.unchecked)) +
(afterUpdate ? ' removed' : ' found') +
'.'
);
}
if (snapshot.fileDeleted) {
statuses.push(
SNAPSHOT_REMOVED(ARROW + 'Obsolete snapshot file') + ` removed.`
);
}
if (snapshot.unmatched) {
statuses.push(
FAIL_COLOR(ARROW + pluralize('snapshot test', snapshot.unmatched)) +
' failed.'
);
}
return statuses;
};
module.exports = {
getSnapshotStatus,
};
================================================
FILE: index.js
================================================
const SilentReporter = require('./SilentReporter');
module.exports = SilentReporter;
================================================
FILE: package.json
================================================
{
"name": "jest-silent-reporter",
"version": "0.6.0",
"description": "A silent reporter for Jest",
"main": "index.js",
"repository": "https://github.com/rickhanlonii/jest-silent-reporter",
"author": "rickhanlonii ",
"license": "MIT",
"scripts": {
"prettier": "prettier --write **/**.js **/**.md"
},
"dependencies": {
"chalk": "^4.0.0",
"jest-util": "^26.0.0"
},
"devDependencies": {
"prettier": "^1.10.2"
}
}