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: add cli support
  • Loading branch information
rluvaton committed Jul 3, 2023
commit 5c6ab88b27e1b6a6acfefa45dc8464ad40af5c29
23 changes: 21 additions & 2 deletions lib/internal/main/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const { getOptionValue } = require('internal/options');
const { isUsingInspector } = require('internal/util/inspector');
const { run } = require('internal/test_runner/runner');
const { setupTestReporters } = require('internal/test_runner/utils');
const { exitCodes: { kGenericUserError }, codes: { ERR_INVALID_ARG_VALUE } } = internalBinding('errors');
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const {
codes: {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { NumberParseInt, NumberIsNaN } = primordials;

prepareMainThreadExecution(false);
Expand All @@ -26,12 +31,26 @@ if (isUsingInspector()) {
let shards;
const shardsOption = getOptionValue('--shards');
if (shardsOption) {
const { 0: indexStr, 1: totalStr } = shardsOption[0].split('/');
const shardsParts = shardsOption.split('/');

if (shardsParts.length !== 2) {
process.exitCode = kGenericUserError;

throw new ERR_INVALID_ARG_VALUE(
'--shards',
shardsOption,
'must be in the form of <index>/<total>',
);
}

const { 0: indexStr, 1: totalStr } = shardsParts;

const index = NumberParseInt(indexStr);
const total = NumberParseInt(totalStr);

if (NumberIsNaN(index) || NumberIsNaN(total)) {
process.exitCode = kGenericUserError;

throw new ERR_INVALID_ARG_VALUE(
'--shards',
shardsOption,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/a.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('a.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/b.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('b.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/c.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('c.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/d.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('d.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/e.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('e.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/f.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('f.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/g.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('g.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/h.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('h.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/i.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('i.cjs this should pass');
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/shards/j.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const test = require('node:test');

test('this should pass');
test('j.cjs this should pass');
104 changes: 104 additions & 0 deletions test/parallel/test-runner-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,107 @@ const testFixtures = fixtures.path('test-runner');
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
}

{
// --shards option validation
const args = ['--test', '--shards=1', join(testFixtures, 'index.js')];
const child = spawnSync(process.execPath, args, { cwd: testFixtures });

assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.match(child.stderr.toString(), /The argument '--shards' must be in the form of <index>\/<total>\. Received '1'/);
const stdout = child.stdout.toString();
assert.strictEqual(stdout, '');
}

{
// --shards option validation
const args = ['--test', '--shards=1/2/3', join(testFixtures, 'index.js')];
const child = spawnSync(process.execPath, args, { cwd: testFixtures });

assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.match(child.stderr.toString(), /The argument '--shards' must be in the form of <index>\/<total>\. Received '1\/2\/3'/);
const stdout = child.stdout.toString();
assert.strictEqual(stdout, '');
}

{
// --shards option validation
const args = ['--test', '--shards=hello', join(testFixtures, 'index.js')];
const child = spawnSync(process.execPath, args, { cwd: testFixtures });

assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.match(child.stderr.toString(), /The argument '--shards' must be in the form of <index>\/<total>\. Received 'hello'/);
const stdout = child.stdout.toString();
assert.strictEqual(stdout, '');
}

{
// --shards option, first shard
const args = [
'--test',
'--shards=1/2',
join(testFixtures, 'shards/*.cjs'),
];
const child = spawnSync(process.execPath, args);

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /# Subtest: a\.cjs this should pass/);
assert.match(stdout, /ok 1 - a\.cjs this should pass/);

assert.match(stdout, /# Subtest: c\.cjs this should pass/);
assert.match(stdout, /ok 2 - c\.cjs this should pass/);

assert.match(stdout, /# Subtest: e\.cjs this should pass/);
assert.match(stdout, /ok 3 - e\.cjs this should pass/);

assert.match(stdout, /# Subtest: g\.cjs this should pass/);
assert.match(stdout, /ok 4 - g\.cjs this should pass/);

assert.match(stdout, /# Subtest: i\.cjs this should pass/);
assert.match(stdout, /ok 5 - i\.cjs this should pass/);

assert.match(stdout, /# tests 5/);
assert.match(stdout, /# pass 5/);
assert.match(stdout, /# fail 0/);
assert.match(stdout, /# skipped 0/);
}

{
// --shards option, last shard
const args = [
'--test',
'--shards=2/2',
join(testFixtures, 'shards/*.cjs'),
];
const child = spawnSync(process.execPath, args);

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /# Subtest: b\.cjs this should pass/);
assert.match(stdout, /ok 1 - b\.cjs this should pass/);

assert.match(stdout, /# Subtest: d\.cjs this should pass/);
assert.match(stdout, /ok 2 - d\.cjs this should pass/);

assert.match(stdout, /# Subtest: f\.cjs this should pass/);
assert.match(stdout, /ok 3 - f\.cjs this should pass/);

assert.match(stdout, /# Subtest: h\.cjs this should pass/);
assert.match(stdout, /ok 4 - h\.cjs this should pass/);

assert.match(stdout, /# Subtest: j\.cjs this should pass/);
assert.match(stdout, /ok 5 - j\.cjs this should pass/);

assert.match(stdout, /# tests 5/);
assert.match(stdout, /# pass 5/);
assert.match(stdout, /# fail 0/);
assert.match(stdout, /# skipped 0/);
}