Skip to content

Commit

Permalink
test: reorder test files fixtures for better understanding
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48787
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
  • Loading branch information
rluvaton committed Aug 14, 2023
1 parent fab6f58 commit cb816ec
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 24 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions test/parallel/test-runner-cli.js
Expand Up @@ -22,8 +22,8 @@ const testFixtures = fixtures.path('test-runner');
{
// Default behavior. node_modules is ignored. Files that don't match the
// pattern are ignored except in test/ directories.
const args = ['--test', testFixtures];
const child = spawnSync(process.execPath, args);
const args = ['--test'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });

assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
Expand All @@ -39,8 +39,8 @@ const testFixtures = fixtures.path('test-runner');

{
// Same but with a prototype mutation in require scripts.
const args = ['--require', join(testFixtures, 'protoMutation.js'), '--test', testFixtures];
const child = spawnSync(process.execPath, args);
const args = ['--require', join(testFixtures, 'protoMutation.js'), '--test'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });

const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
Expand Down Expand Up @@ -72,7 +72,7 @@ const testFixtures = fixtures.path('test-runner');

{
// Searches node_modules if specified.
const args = ['--test', join(testFixtures, 'node_modules')];
const args = ['--test', join(testFixtures, 'default-behavior/node_modules')];
const child = spawnSync(process.execPath, args);

assert.strictEqual(child.status, 1);
Expand All @@ -85,7 +85,7 @@ const testFixtures = fixtures.path('test-runner');
{
// The current directory is used by default.
const args = ['--test'];
const options = { cwd: testFixtures };
const options = { cwd: join(testFixtures, 'default-behavior') };
const child = spawnSync(process.execPath, args, options);

assert.strictEqual(child.status, 1);
Expand Down Expand Up @@ -124,7 +124,7 @@ const testFixtures = fixtures.path('test-runner');
// Test combined stream outputs
const args = [
'--test',
'test/fixtures/test-runner/index.test.js',
'test/fixtures/test-runner/default-behavior/index.test.js',
'test/fixtures/test-runner/nested.js',
'test/fixtures/test-runner/invalid-tap.js',
];
Expand Down Expand Up @@ -202,7 +202,7 @@ const testFixtures = fixtures.path('test-runner');
const args = ['--no-warnings',
'--experimental-loader', 'data:text/javascript,',
'--require', fixtures.path('empty.js'),
'--test', join(testFixtures, 'index.test.js')];
'--test', join(testFixtures, 'default-behavior', 'index.test.js')];
const child = spawnSync(process.execPath, args);

assert.strictEqual(child.stderr.toString(), '');
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-runner-exit-code.js
Expand Up @@ -43,7 +43,10 @@ if (process.argv[2] === 'child') {
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);

child = spawnSync(process.execPath, ['--test', fixtures.path('test-runner', 'subdir', 'subdir_test.js')]);
child = spawnSync(process.execPath, [
'--test',
fixtures.path('test-runner', 'default-behavior', 'subdir', 'subdir_test.js'),
]);
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);

Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-runner-inspect.mjs
Expand Up @@ -11,7 +11,11 @@ common.skipIfInspectorDisabled();
tmpdir.refresh();

{
const child = new NodeInstance(['--test', '--inspect-brk=0'], undefined, fixtures.path('test-runner/index.test.js'));
const child = new NodeInstance(
['--test', '--inspect-brk=0'],
undefined,
fixtures.path('test-runner/default-behavior/index.test.js')
);

let stdout = '';
let stderr = '';
Expand Down
116 changes: 102 additions & 14 deletions test/parallel/test-runner-run.mjs
Expand Up @@ -26,15 +26,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should succeed with a file', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs')] });
const stream = run({ files: [join(testFixtures, 'default-behavior/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);
});

it('should run same file twice', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs'), join(testFixtures, 'test/random.cjs')] });
const stream = run({
files: [
join(testFixtures, 'default-behavior/test/random.cjs'),
join(testFixtures, 'default-behavior/test/random.cjs'),
]
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -68,7 +73,9 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should be piped with dot', async () => {
const result = await run({ files: [join(testFixtures, 'test/random.cjs')] }).compose(dot).toArray();
const result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(dot).toArray();
assert.deepStrictEqual(result, [
'.',
'\n',
Expand All @@ -77,15 +84,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {

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 result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(specReporter).toArray();
const stringResults = result.map((bfr) => bfr.toString());
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();
const result = await run({
files: [join(testFixtures, 'default-behavior/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');
Expand All @@ -103,24 +114,44 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should skip tests not matching testNamePatterns - RegExp', async () => {
const result = await run({ files: [join(testFixtures, 'test/skip_by_name.cjs')], testNamePatterns: [/executed/] })
const result = await run({
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
testNamePatterns: [/executed/]
})
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n');
assert.strictEqual(result[5], 'ok 2 - this should be executed\n');
});

it('should skip tests not matching testNamePatterns - string', async () => {
const result = await run({ files: [join(testFixtures, 'test/skip_by_name.cjs')], testNamePatterns: ['executed'] })
const result = await run({
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
testNamePatterns: ['executed']
})
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n');
assert.strictEqual(result[5], 'ok 2 - this should be executed\n');
});

it('should emit "test:watch:drained" event on watch mode', async () => {
const controller = new AbortController();
await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
watch: true,
signal: controller.signal,
}).on('data', function({ type }) {
if (type === 'test:watch:drained') {
controller.abort();
}
});
});

describe('AbortSignal', () => {
it('should stop watch mode when abortSignal aborts', async () => {
const controller = new AbortController();
const result = await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal })
const result = await run({ files: [join(testFixtures, 'default-behavior/test/random.cjs')], watch: true, signal: controller.signal })
.compose(async function* (source) {
for await (const chunk of source) {
if (chunk.type === 'test:pass') {
Expand All @@ -133,14 +164,71 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
assert.deepStrictEqual(result, ['this should pass']);
});

it('should emit "test:watch:drained" event on watch mode', async () => {
const controller = new AbortController();
await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal })
.on('data', function({ type }) {
if (type === 'test:watch:drained') {
controller.abort();
it('should abort when test succeeded', async () => {
const stream = run({
files: [
fixtures.path(
'test-runner',
'aborts',
'successful-test-still-call-abort.js'
),
],
});

let passedTestCount = 0;
let failedTestCount = 0;

let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}

assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(failedTestCount, 0, new Error('no tests should fail'));
assert.strictEqual(passedTestCount, 2);
});

it('should abort when test failed', async () => {
const stream = run({
files: [
fixtures.path(
'test-runner',
'aborts',
'failed-test-still-call-abort.js'
),
],
});

let passedTestCount = 0;
let failedTestCount = 0;

let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}

assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(passedTestCount, 0, new Error('no tests should pass'));
assert.strictEqual(failedTestCount, 2);
});
});

describe('sharding', () => {
Expand Down

0 comments on commit cb816ec

Please sign in to comment.