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 1 commit
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
22 changes: 21 additions & 1 deletion doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,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 +749,18 @@ added:
**Default:** `undefined`.
* Returns: {TestsStream}

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

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
11 changes: 11 additions & 0 deletions lib/test/reporters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const dot = require('internal/test_runner/reporter/dot');
const spec = require('internal/test_runner/reporter/spec');
const tap = require('internal/test_runner/reporter/tap');

module.exports = {
dot,
spec,
tap,
};
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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 { Writable } from 'node:stream';
import { dot } from 'node:test/reporters';
MoLow marked this conversation as resolved.
Show resolved Hide resolved
import assert from 'node:assert';

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

it('should be piped with dot', (ctx, done) => {
const written = [];
const writer = new Writable();
writer._write = function(chunk, encoding, cb) {
written.push(chunk.toString());
process.nextTick(cb);
};
function finish() {
assert.deepStrictEqual(written, [
'.',
'\n',
]);
done();
}
writer.on('finish', finish);
run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(dot).pipe(writer);
MoLow marked this conversation as resolved.
Show resolved Hide resolved
});
});