Skip to content
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
Prev Previous commit
Next Next commit
test_runner: CR add UT & lazy require
  • Loading branch information
atlowChemi committed Mar 25, 2023
commit 16edd363b5ca2cdf0370e1687a9dc02e515f7322
4 changes: 4 additions & 0 deletions 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 Down
51 changes: 42 additions & 9 deletions lib/test/reporters.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
'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,
};
const { ObjectDefineProperty } = primordials;

let dot;
let spec;
let tap;

ObjectDefineProperty(module.exports, 'dot', {
Comment thread
atlowChemi marked this conversation as resolved.
Outdated
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (dot === undefined) {
dot = require('internal/test_runner/reporter/dot');
}
Comment thread
atlowChemi marked this conversation as resolved.
Outdated

return dot;
},
});
ObjectDefineProperty(module.exports, 'spec', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (spec === undefined) {
spec = require('internal/test_runner/reporter/spec');
}

return spec;
},
});
ObjectDefineProperty(module.exports, 'tap', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (tap === undefined) {
tap = require('internal/test_runner/reporter/tap');
}

return tap;
},
});
30 changes: 29 additions & 1 deletion test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 } from 'node:test/reporters';
import { dot, spec, tap } from 'node:test/reporters';
import assert from 'node:assert';

const testFixtures = fixtures.path('test-runner');
Expand Down Expand Up @@ -74,4 +74,32 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
'\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.strictEqual(stringResults.length, 2);
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*/);
});
});