Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: expose reporter for use in run api #47238

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ test('spies on an object method', (t) => {
added:
- v19.6.0
- v18.15.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47238
description: Reporters are now exposed at `node:test/reporters`.
-->

The `node:test` module supports passing [`--test-reporter`][]
Expand All @@ -546,6 +550,16 @@ Node.js, and should not be relied on programmatically. If programmatic access
to the test runner's output is required, use the events emitted by the
{TestsStream}.

The reporters are available via the `node:test/reporters` module:

```mjs
import { tap, spec, dot } from 'node:test/reporters';
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
```

```cjs
const { tap, spec, dot } = require('node:test/reporters');
```

### Custom reporters

[`--test-reporter`][] can be used to specify a path to custom reporter.
Expand Down Expand Up @@ -739,8 +753,20 @@ added:
**Default:** `undefined`.
* Returns: {TestsStream}

```js
```mjs
import { tap } from 'node:test/reporters';
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
import process from 'node:process';

run({ files: [path.resolve('./tests/test.js')] })
.compose(tap)
.pipe(process.stdout);
```

```cjs
const { tap } = require('node:test/reporters');

run({ files: [path.resolve('./tests/test.js')] })
.compose(tap)
.pipe(process.stdout);
```

Expand Down
38 changes: 38 additions & 0 deletions lib/test/reporters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const { ObjectDefineProperties } = primordials;

let dot;
let spec;
let tap;

ObjectDefineProperties(module.exports, {
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure that cjs-module-lexer will be able to pick up this pattern as named exports?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a pretty common pattern in node's built-in modules, so it probably does:

ObjectDefineProperties(module.exports, {

ObjectDefineProperties(module.exports, {

node/lib/dns.js

Line 345 in 9dbb162

ObjectDefineProperties(module.exports, {

node/lib/os.js

Line 399 in 9dbb162

ObjectDefineProperties(module.exports, {

ObjectDefineProperties(module.exports, {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably doesn’t but we don’t use cjs-module-lexer for core modules, because core modules are not CJS (or at least, not regular CJS) and the use of named imports for core modules predates the introduction of cjs-module-lexer.

__proto__: null,
dot: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
dot ??= require('internal/test_runner/reporter/dot');
return dot;
},
},
spec: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
spec ??= require('internal/test_runner/reporter/spec');
return spec;
},
},
tap: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
tap ??= require('internal/test_runner/reporter/tap');
return tap;
},
},
});
36 changes: 36 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { join } from 'node:path';
import { describe, it, run } from 'node:test';
import { dot, spec, tap } from 'node:test/reporters';
import assert from 'node:assert';

const testFixtures = fixtures.path('test-runner');
Expand Down Expand Up @@ -65,4 +66,39 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
code: 'ERR_INVALID_ARG_TYPE'
}));
});

it('should be piped with dot', async () => {
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(dot).toArray();
assert.deepStrictEqual(result, [
'.',
'\n',
]);
});

it('should be piped with spec', async () => {
const specReporter = new spec();
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(specReporter).toArray();
const stringResults = result.map((bfr) => bfr.toString());
assert.match(stringResults[0], /this should pass/);
assert.match(stringResults[1], /tests 1/);
assert.match(stringResults[1], /pass 1/);
});

it('should be piped with tap', async () => {
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(tap).toArray();
assert.strictEqual(result.length, 13);
assert.strictEqual(result[0], 'TAP version 13\n');
assert.strictEqual(result[1], '# Subtest: this should pass\n');
assert.strictEqual(result[2], 'ok 1 - this should pass\n');
assert.match(result[3], /duration_ms: \d+\.?\d*/);
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
assert.strictEqual(result[6], '# suites 0\n');
assert.strictEqual(result[7], '# pass 1\n');
assert.strictEqual(result[8], '# fail 0\n');
assert.strictEqual(result[9], '# cancelled 0\n');
assert.strictEqual(result[10], '# skipped 0\n');
assert.strictEqual(result[11], '# todo 0\n');
assert.match(result[12], /# duration_ms \d+\.?\d*/);
});
});