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 validations
  • Loading branch information
rluvaton committed Jul 2, 2023
commit 3bc36b8f044b12bc7c8b04b7f9da7f4e12453c64
22 changes: 15 additions & 7 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ const {
ERR_TEST_FAILURE,
},
} = require('internal/errors');
const { validateArray, validateBoolean, validateFunction, validateObject, validateNumber} = require('internal/validators');
const {
validateArray,
validateBoolean,
validateFunction,
validateObject,
validateNumber,
} = require('internal/validators');
const { getInspectPort, isUsingInspector, isInspectorMessage } = require('internal/util/inspector');
const { isRegExp } = require('internal/util/types');
const { kEmptyObject } = require('internal/util');
Expand Down Expand Up @@ -431,17 +437,19 @@ function run(options) {
validateNumber(shards.total, 'options.shards.total');
validateNumber(shards.index, 'options.shards.index');

if(shards.total <= 0) {
if (shards.total <= 0) {
throw new ERR_INVALID_ARG_VALUE('options.shards.total', shards.total, 'total shards must be greater than 0');
}
if(shards.index <= 0) {
if (shards.index <= 0) {
throw new ERR_INVALID_ARG_VALUE('options.shards.index', shards.index, 'shard index must be greater than 0');
}
if(shards.total < shards.index) {
throw new ERR_INVALID_ARG_VALUE('options.shards.index', shards.index, 'shard index must be less than total shards');
if (shards.total < shards.index) {
throw new ERR_INVALID_ARG_VALUE(
'options.shards.index', shards.index, 'shard index must be less than total shards',
);
}

if(watch) {
if (watch) {
Copy link
Member

Choose a reason for hiding this comment

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

q: why?

Copy link
Member Author

Choose a reason for hiding this comment

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

why would you use sharding if you need to watch all the files? watch is for active development

Copy link
Member

Choose a reason for hiding this comment

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

You had a bug in a certain shard in CI and you want to iteratively fix it? Is there a reason not to allow it?

Copy link
Member Author

Choose a reason for hiding this comment

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

if you add a file it will change the sharding, we can add it I just think it won't be really useful

throw new ERR_INVALID_ARG_VALUE('options.shards', watch, 'shards not supported with watch mode');
}
}
Expand All @@ -468,7 +476,7 @@ function run(options) {
const root = createTestTree({ concurrency, timeout, signal });
let testFiles = files ?? createTestFileList();

if(shards) {
if (shards) {
testFiles = testFiles.filter((file, index) => (index % shards.total) === (shards.index - 1));
}

Expand Down
115 changes: 104 additions & 11 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,57 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should fail with non existing file', async () => {
const stream = run({ files: ['a-random-file-that-does-not-exist.js'] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should succeed with a file', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should run same file twice', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs'), join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should run a failed test', async () => {
const stream = run({ files: [testFixtures] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should support timeout', async () => {
const stream = run({ timeout: 50, files: [
fixtures.path('test-runner', 'never_ending_sync.js'),
fixtures.path('test-runner', 'never_ending_async.js'),
] });
const stream = run({
timeout: 50, files: [
fixtures.path('test-runner', 'never_ending_sync.js'),
fixtures.path('test-runner', 'never_ending_async.js'),
]
});
stream.on('test:fail', common.mustCall(2));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for await (const _ of stream) ;
});

it('should validate files', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
[Symbol(), {}, () => {
}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((files) => assert.throws(() => run({ files }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
Expand Down Expand Up @@ -142,4 +145,94 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
}
});
});

describe('sharding', () => {
describe('validation', () => {
it('should require shards.total when having shards option', () => {
assert.throws(() => run({ files: [join(testFixtures, 'test/random.cjs')], shards: {} }), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shards.total" property must be of type number. Received undefined'
});
});

it('should require shards.index when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
shards: {
total: 5
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shards.index" property must be of type number. Received undefined'
});
});

it('should require shards.total to be greater than 0 when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
shards: {
total: 0,
index: 1
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shards.total\' total shards must be greater than 0. Received 0'
});
});

it('should require shards.index to be greater than 0 when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
shards: {
total: 6,
index: 0
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shards.index\' shard index must be greater than 0. Received 0'
});
});

it('should require shards.index to not be greater than the shards total when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
shards: {
total: 6,
index: 7
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shards.index\' shard index must be less than total shards. Received 7'
});
});

it('should require watch mode to e disabled when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
watch: true,
shards: {
total: 6,
index: 1
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shards\' shards not supported with watch mode. Received true'
});
});
});

it('should run only the tests files matching the shard index', async () => {

});

it('should run only the tests file', async () => {

});
});
});