test_runner: add reporters

PR-URL: https://github.com/nodejs/node/pull/45712
Backport-PR-URL: https://github.com/nodejs/node/pull/46839
Fixes: https://github.com/nodejs/node/issues/45648
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Moshe Atlow authored and Juan José Arboleda committed 2023-03-04 23:03:32 -05:00
1 parent 5fdf374c74
commit 4d50db14b3
36 files changed
+1138 -245

No files matched your search

+1
View File
@@ -75,6 +75,7 @@ const expectedModules = new Set([
'NativeModule internal/mime',
'NativeModule internal/modules/cjs/helpers',
'NativeModule internal/modules/cjs/loader',
'NativeModule internal/modules/utils',
'NativeModule internal/modules/esm/assert',
'NativeModule internal/modules/esm/create_dynamic_module',
'NativeModule internal/modules/esm/fetch_module',
+1 -2
View File
@@ -20,8 +20,7 @@ async function runAndKill(file) {
});
const [code, signal] = await once(child, 'exit');
await finished(child.stdout);
assert.match(stdout, /not ok 1/);
assert.match(stdout, /# cancelled 1\n/);
assert.strictEqual(stdout, 'TAP version 13\n');
assert.strictEqual(signal, null);
assert.strictEqual(code, 1);
}
+95
View File
@@ -0,0 +1,95 @@
'use strict';
require('../common');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const { describe, it } = require('node:test');
const { spawnSync } = require('node:child_process');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const testFile = fixtures.path('test-runner/reporters.js');
tmpdir.refresh();
let tmpFiles = 0;
describe('node:test reporters', { concurrency: true }, () => {
it('should default to outputing TAP to stdout', async () => {
const child = spawnSync(process.execPath, ['--test', testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.match(child.stdout.toString(), /TAP version 13/);
assert.match(child.stdout.toString(), /ok 1 - ok/);
assert.match(child.stdout.toString(), /not ok 2 - failing/);
assert.match(child.stdout.toString(), /ok 2 - top level/);
});
it('should default destination to stdout when passing a single reporter', async () => {
const child = spawnSync(process.execPath, ['--test', '--test-reporter', 'dot', testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(), '.XX.X\n');
});
it('should throw when passing reporters without a destination', async () => {
const child = spawnSync(process.execPath, ['--test', '--test-reporter', 'dot', '--test-reporter', 'tap', testFile]);
assert.match(child.stderr.toString(), /The argument '--test-reporter' must match the number of specified '--test-reporter-destination'\. Received \[ 'dot', 'tap' \]/);
assert.strictEqual(child.stdout.toString(), '');
});
it('should throw when passing a destination without a reporter', async () => {
const child = spawnSync(process.execPath, ['--test', '--test-reporter-destination', 'tap', testFile]);
assert.match(child.stderr.toString(), /The argument '--test-reporter' must match the number of specified '--test-reporter-destination'\. Received \[\]/);
assert.strictEqual(child.stdout.toString(), '');
});
it('should support stdout as a destination', async () => {
const child = spawnSync(process.execPath,
['--test', '--test-reporter', 'dot', '--test-reporter-destination', 'stdout', testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(), '.XX.X\n');
});
it('should support stderr as a destination', async () => {
const child = spawnSync(process.execPath,
['--test', '--test-reporter', 'dot', '--test-reporter-destination', 'stderr', testFile]);
assert.strictEqual(child.stderr.toString(), '.XX.X\n');
assert.strictEqual(child.stdout.toString(), '');
});
it('should support a file as a destination', async () => {
const file = path.join(tmpdir.path, `${tmpFiles++}.out`);
const child = spawnSync(process.execPath,
['--test', '--test-reporter', 'dot', '--test-reporter-destination', file, testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(), '');
assert.strictEqual(fs.readFileSync(file, 'utf8'), '.XX.X\n');
});
it('should support multiple reporters', async () => {
const file = path.join(tmpdir.path, `${tmpFiles++}.out`);
const file2 = path.join(tmpdir.path, `${tmpFiles++}.out`);
const child = spawnSync(process.execPath,
['--test',
'--test-reporter', 'dot', '--test-reporter-destination', file,
'--test-reporter', 'spec', '--test-reporter-destination', file2,
'--test-reporter', 'tap', '--test-reporter-destination', 'stdout',
testFile]);
assert.match(child.stdout.toString(), /TAP version 13/);
assert.match(child.stdout.toString(), /# duration_ms/);
assert.strictEqual(fs.readFileSync(file, 'utf8'), '.XX.X\n');
const file2Contents = fs.readFileSync(file2, 'utf8');
assert.match(file2Contents, /▶ nested/);
assert.match(file2Contents, /✔ ok/);
assert.match(file2Contents, /✖ failing/);
});
['js', 'cjs', 'mjs'].forEach((ext) => {
it(`should support a '${ext}' file as a custom reporter`, async () => {
const filename = `custom.${ext}`;
const child = spawnSync(process.execPath,
['--test', '--test-reporter', fixtures.path('test-runner/custom_reporters/', filename),
testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(), `${filename} {"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}`);
});
});
});
-1
View File
@@ -10,7 +10,6 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should run with no tests', async () => {
const stream = run({ files: [] });
stream.setEncoding('utf8');
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars