-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add slow tests reporter (#105)
- Loading branch information
Showing
9 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\) | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters