Skip to content

Commit

Permalink
feat: add slow tests reporter (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow authored Jan 4, 2024
1 parent 8c09454 commit 591db4a
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Available reporters:
- [silent](https://www.npmjs.com/package/@reporters/silent) - a silent reporter
- [bail](https://www.npmjs.com/package/@reporters/bail) - bail on first failure
- [testwatch](https://www.npmjs.com/package/@reporters/testwatch) - An interactive REPL for `node:test` watch mode.
- [slow](https://www.npmjs.com/package/@reporters/slow) - report slow tests
21 changes: 21 additions & 0 deletions packages/slow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[![npm version](https://img.shields.io/npm/v/@reporters/slow)](https://www.npmjs.com/package/@reporters/slow) ![tests](https://github.com/MoLow/reporters/actions/workflows/test.yaml/badge.svg?branch=main) [![codecov](https://codecov.io/gh/MoLow/reporters/branch/main/graph/badge.svg?token=0LFVC8SCQV)](https://codecov.io/gh/MoLow/reporters)

# Slow tests Reporter
A Slow tests reporter for `node:test`.

## Installation

```bash
npm install --save-dev @reporters/slow
```
or
```bash
yarn add --dev @reporters/slow
```

## Usage

```bash
node --test --test-reporter=@reporters/slow
```

47 changes: 47 additions & 0 deletions packages/slow/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

/* eslint-disable camelcase */
/* eslint-disable no-continue */
const ms = require('ms');

const GRAY = '\x1b[38;5;8m';
const CLEAR = '\x1b[0m';
const THRESHOLDS = [
[250, CLEAR],
[600, '\x1b[33m'],
[900, '\x1b[38;5;215m'],
[Infinity, '\x1b[31m'],
];

const COLORED_CWD = `${GRAY}${process.cwd()}${CLEAR}`;

module.exports = async function* slowTestsReporter(source) {
const files = new Map();
for await (const event of source) {
if (event.type !== 'test:pass' && event.type !== 'test:fail') continue;
const {
data: {
details: { type, duration_ms }, name, file, column, line,
},
} = event;
if (duration_ms < THRESHOLDS[0][0] || type === 'suite') continue;
const slowTests = files.get(file) || [];
slowTests.push({
duration_ms, name, file, column, line,
});
files.set(file, slowTests);
}

for (const [file, slowTests] of files) {
const colord_file = file.replace(process.cwd(), COLORED_CWD);
yield `file: ${colord_file} has slow tests:\n`;
slowTests.sort((a, b) => b.duration_ms - a.duration_ms);
for (const {
duration_ms, name, column, line,
} of slowTests) {
const [, color] = THRESHOLDS
.find(([threshold]) => duration_ms < threshold) || THRESHOLDS[THRESHOLDS.length - 1];
yield ` ${color}-${CLEAR} ${name} [${color}${ms(duration_ms)}${CLEAR}] (${CLEAR}${colord_file}:${line}:${column})\n`;
}
}
};
29 changes: 29 additions & 0 deletions packages/slow/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@reporters/slow",
"version": "1.2.4",
"description": "A slow tests reporter for `node:test`",
"type": "commonjs",
"keywords": [
"node:test",
"test",
"reporter",
"reporters"
],
"files": [
"./index.js"
],
"scripts": {
"test": "node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=../github/index.js --test-reporter-destination=stdout --test"
},
"bugs": {
"url": "https://github.com/MoLow/reporters/issues"
},
"main": "index.js",
"homepage": "https://github.com/MoLow/reporters/tree/main/packages/slow",
"repository": "https://github.com/MoLow/reporters.git",
"author": "Moshe Atlow",
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
}
}
17 changes: 17 additions & 0 deletions packages/slow/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');
const { test } = require('node:test');
const { spawnSync } = require('child_process');
const { compareLines } = require('../../../tests/utils');

test('spwan with reporter', () => {
const child = spawnSync(process.execPath, ['--test-reporter', './index.js', '../../tests/slow_tests.js'], { env: { FORCE_COLOR: 1 } });
assert.strictEqual(child.stderr?.toString(), '');
compareLines(child.stdout?.toString(), `\
file: .*tests/slow_tests\\.js has slow tests:
\\\x1B\\[31m-\\\x1B\\[0m is too slow \\[\x1B\\[31m1s\\\x1B\\[0m\\] \\(\\\x1B\\[0m.*tests/slow_tests\\.js:9:3\\)
\\\x1B\\[38;5;215m-\\\x1B\\[0m is pretty slow \\[\\\x1B\\[38;5;215m.*ms\\\x1B\\[0m\\] \\(\\\x1B\\[0m.*tests/slow_tests\\.js:8:3\\)
\\\x1B\\[33m-\\\x1B\\[0m is a little slow \\[\x1B\\[33m.*ms\\\x1B\\[0m\\] \\(\\\x1B\\[0m.*tests/slow_tests\\.js:7:3\\)
`);
});
3 changes: 2 additions & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"packages/junit": {},
"packages/silent": {},
"packages/bail": {},
"packages/testwatch": {}
"packages/testwatch": {},
"packages/slow": {}
}
}
13 changes: 13 additions & 0 deletions tests/slow_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const { describe, it } = require('node:test');

describe({ concurrency: 5 }, () => {
it('is ok', () => {});
it('is a little slow', (t, done) => setTimeout(done, 300));
it('is pretty slow', (t, done) => setTimeout(done, 700));
it('is too slow', (t, done) => setTimeout(done, 1000));
it('fails', () => {
throw new Error('this is an error');
});
});
4 changes: 1 addition & 3 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ function compareLines(output = '', expected = '') {
const outputLines = output.split(/\r?\n/);
const expectedLines = expected.split(/\r?\n/);
if (outputLines.length !== expectedLines.length) {
console.log(`Output: ${outputLines.length} lines`);
console.log(output);
console.log(`Expected: ${expectedLines.length} lines`);
console.log(expected);
assert.fail('Output and expected have different number of lines');
assert.strictEqual(outputLines.length, expectedLines.length, 'Output and expected have different number of lines');
}
for (let i = 0; i < expectedLines.length; i += 1) {
const expectedRegex = new RegExp(expectedLines[i]);
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ ms@2.1.2:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@^2.1.1:
ms@^2.1.1, ms@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
Expand Down

0 comments on commit 591db4a

Please sign in to comment.