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 tests for the shards
  • Loading branch information
rluvaton committed Jul 2, 2023
commit bf9d2e564ea3621403b77de07cda90a7f2992d3b
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/shards/a.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const test = require('node:test');

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

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

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

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

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

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

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

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

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

test('this should pass');
99 changes: 92 additions & 7 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,23 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

describe('sharding', () => {
const shardsTestsFixtures = fixtures.path('test-runner/shards');
const shardsTestsFiles = [
'a.cjs',
'b.cjs',
'c.cjs',
'd.cjs',
'e.cjs',
'f.cjs',
'g.cjs',
'h.cjs',
'i.cjs',
'j.cjs',
].map((file) => join(shardsTestsFixtures, file));

describe('validation', () => {
it('should require shards.total when having shards option', () => {
assert.throws(() => run({ files: [join(testFixtures, 'test/random.cjs')], shards: {} }), {
assert.throws(() => run({ files: shardsTestsFiles, shards: {} }), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shards.total" property must be of type number. Received undefined'
Expand All @@ -158,7 +172,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

it('should require shards.index when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
files: shardsTestsFiles,
shards: {
total: 5
}
Expand All @@ -171,7 +185,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

it('should require shards.total to be greater than 0 when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
files: shardsTestsFiles,
shards: {
total: 0,
index: 1
Expand All @@ -185,7 +199,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

it('should require shards.index to be greater than 0 when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
files: shardsTestsFiles,
shards: {
total: 6,
index: 0
Expand All @@ -199,7 +213,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

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')],
files: shardsTestsFiles,
shards: {
total: 6,
index: 7
Expand All @@ -213,7 +227,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

it('should require watch mode to e disabled when having shards option', () => {
assert.throws(() => run({
files: [join(testFixtures, 'test/random.cjs')],
files: shardsTestsFiles,
watch: true,
shards: {
total: 6,
Expand All @@ -228,11 +242,82 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should run only the tests files matching the shard index', async () => {
const stream = run({
files: shardsTestsFiles,
shards: {
total: 5,
index: 1
}
});

const executedTestFiles = [];
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;

assert.deepStrictEqual(executedTestFiles, [
join(shardsTestsFixtures, 'a.cjs'),
join(shardsTestsFixtures, 'f.cjs'),
]);
});

it('different shards should not run the same file', async () => {
const executedTestFiles = [];

const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shards: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}

await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
}));

assert.deepStrictEqual(executedTestFiles, [...new Set(executedTestFiles)]);
});

it('should run only the tests file', async () => {
it('combination of all shards should be all the tests', async () => {
const executedTestFiles = [];

const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shards: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}

await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
}));

assert.deepStrictEqual(executedTestFiles.sort(), shardsTestsFiles.sort());
});
});
});