Skip to content

Commit 2091b47

Browse files
atlowChemidanielleadams
authored andcommitted
test_runner: expose reporter for use in run api
PR-URL: #47238Fixes: #47231 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent fc6ab26 commit 2091b47

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎doc/api/test.md‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ test('spies on an object method', (t) => {
510510

511511
<!-- YAML
512512
added: v18.15.0
513+
changes:
514+
- version: REPLACEME
515+
pr-url: https://github.com/nodejs/node/pull/47238
516+
description: Reporters are now exposed at `node:test/reporters`.
513517
-->
514518

515519
The `node:test` module supports passing [`--test-reporter`][]
@@ -531,6 +535,16 @@ The following built-reporters are supported:
531535
When `stdout` is a [TTY][], the `spec` reporter is used by default.
532536
Otherwise, the `tap` reporter is used by default.
533537

538+
The reporters are available via the `node:test/reporters` module:
539+
540+
```mjs
541+
import { tap, spec, dot } from'node:test/reporters';
542+
```
543+
544+
```cjs
545+
const { tap, spec, dot } =require('node:test/reporters');
546+
```
547+
534548
### Custom reporters
535549

536550
[`--test-reporter`][] can be used to specify a path to custom reporter.
@@ -722,8 +736,20 @@ added: v18.9.0
722736
**Default:**`undefined`.
723737
* Returns: {TestsStream}
724738

725-
```js
739+
```mjs
740+
import { tap } from'node:test/reporters';
741+
importprocessfrom'node:process';
742+
743+
run({ files: [path.resolve('./tests/test.js')] })
744+
.compose(tap)
745+
.pipe(process.stdout);
746+
```
747+
748+
```cjs
749+
const { tap } =require('node:test/reporters');
750+
726751
run({ files: [path.resolve('./tests/test.js')] })
752+
.compose(tap)
727753
.pipe(process.stdout);
728754
```
729755

‎lib/test/reporters.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const{ ObjectDefineProperties }=primordials;
4+
5+
letdot;
6+
letspec;
7+
lettap;
8+
9+
ObjectDefineProperties(module.exports,{
10+
__proto__: null,
11+
dot: {
12+
__proto__: null,
13+
configurable: true,
14+
enumerable: true,
15+
get(){
16+
dot??=require('internal/test_runner/reporter/dot');
17+
returndot;
18+
},
19+
},
20+
spec: {
21+
__proto__: null,
22+
configurable: true,
23+
enumerable: true,
24+
get(){
25+
spec??=require('internal/test_runner/reporter/spec');
26+
returnspec;
27+
},
28+
},
29+
tap: {
30+
__proto__: null,
31+
configurable: true,
32+
enumerable: true,
33+
get(){
34+
tap??=require('internal/test_runner/reporter/tap');
35+
returntap;
36+
},
37+
},
38+
});

‎test/parallel/test-runner-run.mjs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as common from '../common/index.mjs';
22
import*asfixturesfrom'../common/fixtures.mjs';
33
import{join}from'node:path';
44
import{describe,it,run}from'node:test';
5+
import{dot,spec,tap}from'node:test/reporters';
56
importassertfrom'node:assert';
67

78
consttestFixtures=fixtures.path('test-runner');
@@ -65,4 +66,39 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
6566
code: 'ERR_INVALID_ARG_TYPE'
6667
}));
6768
});
69+
70+
it('should be piped with dot',async()=>{
71+
constresult=awaitrun({files: [join(testFixtures,'test/random.cjs')]}).compose(dot).toArray();
72+
assert.deepStrictEqual(result,[
73+
'.',
74+
'\n',
75+
]);
76+
});
77+
78+
it('should be piped with spec',async()=>{
79+
constspecReporter=newspec();
80+
constresult=awaitrun({files: [join(testFixtures,'test/random.cjs')]}).compose(specReporter).toArray();
81+
conststringResults=result.map((bfr)=>bfr.toString());
82+
assert.match(stringResults[0],/thisshouldpass/);
83+
assert.match(stringResults[1],/tests1/);
84+
assert.match(stringResults[1],/pass1/);
85+
});
86+
87+
it('should be piped with tap',async()=>{
88+
constresult=awaitrun({files: [join(testFixtures,'test/random.cjs')]}).compose(tap).toArray();
89+
assert.strictEqual(result.length,13);
90+
assert.strictEqual(result[0],'TAP version 13\n');
91+
assert.strictEqual(result[1],'# Subtest: this should pass\n');
92+
assert.strictEqual(result[2],'ok 1 - this should pass\n');
93+
assert.match(result[3],/duration_ms:\d+\.?\d*/);
94+
assert.strictEqual(result[4],'1..1\n');
95+
assert.strictEqual(result[5],'# tests 1\n');
96+
assert.strictEqual(result[6],'# suites 0\n');
97+
assert.strictEqual(result[7],'# pass 1\n');
98+
assert.strictEqual(result[8],'# fail 0\n');
99+
assert.strictEqual(result[9],'# cancelled 0\n');
100+
assert.strictEqual(result[10],'# skipped 0\n');
101+
assert.strictEqual(result[11],'# todo 0\n');
102+
assert.match(result[12],/#duration_ms\d+\.?\d*/);
103+
});
68104
});

0 commit comments

Comments
 (0)