diff --git a/test/fixtures/test-runner/index.test.js b/test/fixtures/test-runner/combined-stream-output/index.test.js similarity index 100% rename from test/fixtures/test-runner/index.test.js rename to test/fixtures/test-runner/combined-stream-output/index.test.js diff --git a/test/fixtures/test-runner/combined-stream-output/invalid-tap.js b/test/fixtures/test-runner/combined-stream-output/invalid-tap.js new file mode 100644 index 00000000000000..d43fb8ad5cb374 --- /dev/null +++ b/test/fixtures/test-runner/combined-stream-output/invalid-tap.js @@ -0,0 +1 @@ +console.log('invalid tap output'); diff --git a/test/fixtures/test-runner/nested.js b/test/fixtures/test-runner/combined-stream-output/nested.js similarity index 100% rename from test/fixtures/test-runner/nested.js rename to test/fixtures/test-runner/combined-stream-output/nested.js diff --git a/test/fixtures/test-runner/test/random.cjs b/test/fixtures/test-runner/default-behavior/index.test.js similarity index 100% rename from test/fixtures/test-runner/test/random.cjs rename to test/fixtures/test-runner/default-behavior/index.test.js diff --git a/test/fixtures/test-runner/default-behavior/node_modules/test-nm.js b/test/fixtures/test-runner/default-behavior/node_modules/test-nm.js new file mode 100644 index 00000000000000..30024eab1f17e4 --- /dev/null +++ b/test/fixtures/test-runner/default-behavior/node_modules/test-nm.js @@ -0,0 +1,2 @@ +'use strict'; +throw new Error('thrown from node_modules'); diff --git a/test/fixtures/test-runner/default-behavior/random.test.mjs b/test/fixtures/test-runner/default-behavior/random.test.mjs new file mode 100644 index 00000000000000..a87a671d006ab6 --- /dev/null +++ b/test/fixtures/test-runner/default-behavior/random.test.mjs @@ -0,0 +1,5 @@ +import test from 'node:test'; + +test('this should fail', () => { + throw new Error('this is a failing test'); +}); diff --git a/test/fixtures/test-runner/default-behavior/subdir/subdir_test.js b/test/fixtures/test-runner/default-behavior/subdir/subdir_test.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/fixtures/test-runner/default-behavior/test/random.cjs b/test/fixtures/test-runner/default-behavior/test/random.cjs new file mode 100644 index 00000000000000..2a722c504b9fa5 --- /dev/null +++ b/test/fixtures/test-runner/default-behavior/test/random.cjs @@ -0,0 +1,4 @@ +'use strict'; +const test = require('node:test'); + +test('this should pass'); diff --git a/test/fixtures/test-runner/test/skip_by_name.cjs b/test/fixtures/test-runner/default-behavior/test/skip_by_name.cjs similarity index 100% rename from test/fixtures/test-runner/test/skip_by_name.cjs rename to test/fixtures/test-runner/default-behavior/test/skip_by_name.cjs diff --git a/test/fixtures/test-runner/index.js b/test/fixtures/test-runner/index.js deleted file mode 100644 index fcf4b4d8eaa0ad..00000000000000 --- a/test/fixtures/test-runner/index.js +++ /dev/null @@ -1,2 +0,0 @@ -'use strict'; -throw new Error('thrown from index.js'); diff --git a/test/fixtures/test-runner/specific-test-files/failure.test.mjs b/test/fixtures/test-runner/specific-test-files/failure.test.mjs new file mode 100644 index 00000000000000..a87a671d006ab6 --- /dev/null +++ b/test/fixtures/test-runner/specific-test-files/failure.test.mjs @@ -0,0 +1,5 @@ +import test from 'node:test'; + +test('this should fail', () => { + throw new Error('this is a failing test'); +}); diff --git a/test/fixtures/test-runner/specific-test-files/skip_by_name.cjs b/test/fixtures/test-runner/specific-test-files/skip_by_name.cjs new file mode 100644 index 00000000000000..14856df43e50f5 --- /dev/null +++ b/test/fixtures/test-runner/specific-test-files/skip_by_name.cjs @@ -0,0 +1,5 @@ +'use strict'; +const test = require('node:test'); + +test('this should be skipped'); +test('this should be executed'); diff --git a/test/fixtures/test-runner/specific-test-files/success.cjs b/test/fixtures/test-runner/specific-test-files/success.cjs new file mode 100644 index 00000000000000..2a722c504b9fa5 --- /dev/null +++ b/test/fixtures/test-runner/specific-test-files/success.cjs @@ -0,0 +1,4 @@ +'use strict'; +const test = require('node:test'); + +test('this should pass'); diff --git a/test/fixtures/test-runner/specific-test-files/success.test.cjs b/test/fixtures/test-runner/specific-test-files/success.test.cjs new file mode 100644 index 00000000000000..2a722c504b9fa5 --- /dev/null +++ b/test/fixtures/test-runner/specific-test-files/success.test.cjs @@ -0,0 +1,4 @@ +'use strict'; +const test = require('node:test'); + +test('this should pass'); diff --git a/test/fixtures/test-runner/specific-test-files/throwing.js b/test/fixtures/test-runner/specific-test-files/throwing.js new file mode 100644 index 00000000000000..313259039afa34 --- /dev/null +++ b/test/fixtures/test-runner/specific-test-files/throwing.js @@ -0,0 +1,2 @@ +'use strict'; +throw new Error('thrown from throwing.js'); diff --git a/test/parallel/test-runner-cli.js b/test/parallel/test-runner-cli.js index c7fb6a0276aeb8..bb483f7d0b9698 100644 --- a/test/parallel/test-runner-cli.js +++ b/test/parallel/test-runner-cli.js @@ -22,7 +22,7 @@ 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']; - const child = spawnSync(process.execPath, args, { cwd: testFixtures }); + const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') }); assert.strictEqual(child.status, 1); assert.strictEqual(child.signal, null); @@ -39,7 +39,7 @@ const testFixtures = fixtures.path('test-runner'); { // Same but with a prototype mutation in require scripts. const args = ['--require', join(testFixtures, 'protoMutation.js'), '--test']; - const child = spawnSync(process.execPath, args, { cwd: testFixtures }); + const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') }); const stdout = child.stdout.toString(); assert.match(stdout, /ok 1 - this should pass/); @@ -55,14 +55,14 @@ const testFixtures = fixtures.path('test-runner'); { // User specified files that don't match the pattern are still run. - const args = ['--test', join(testFixtures, 'index.js')]; + const args = ['--test', join(testFixtures, 'specific-test-files/throwing.js')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); assert.strictEqual(child.signal, null); assert.strictEqual(child.stderr.toString(), ''); const stdout = child.stdout.toString(); - assert.match(stdout, /not ok 1 - .+index\.js/); + assert.match(stdout, /not ok 1 - .+throwing\.js/); } { @@ -80,7 +80,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); @@ -119,9 +119,9 @@ const testFixtures = fixtures.path('test-runner'); // Test combined stream outputs const args = [ '--test', - 'test/fixtures/test-runner/index.test.js', - 'test/fixtures/test-runner/nested.js', - 'test/fixtures/test-runner/invalid-tap.js', + 'test/fixtures/test-runner/combined-stream-output/index.test.js', + 'test/fixtures/test-runner/combined-stream-output/nested.js', + 'test/fixtures/test-runner/combined-stream-output/invalid-tap.js', ]; const child = spawnSync(process.execPath, args); @@ -197,7 +197,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, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args); assert.strictEqual(child.stderr.toString(), ''); @@ -209,7 +209,7 @@ const testFixtures = fixtures.path('test-runner'); { // --test-shard option validation - const args = ['--test', '--test-shard=1', join(testFixtures, 'index.js')]; + const args = ['--test', '--test-shard=1', join(testFixtures, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); @@ -221,7 +221,7 @@ const testFixtures = fixtures.path('test-runner'); { // --test-shard option validation - const args = ['--test', '--test-shard=1/2/3', join(testFixtures, 'index.js')]; + const args = ['--test', '--test-shard=1/2/3', join(testFixtures, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); @@ -233,7 +233,7 @@ const testFixtures = fixtures.path('test-runner'); { // --test-shard option validation - const args = ['--test', '--test-shard=0/3', join(testFixtures, 'index.js')]; + const args = ['--test', '--test-shard=0/3', join(testFixtures, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); @@ -245,7 +245,7 @@ const testFixtures = fixtures.path('test-runner'); { // --test-shard option validation - const args = ['--test', '--test-shard=0xf/20abcd', join(testFixtures, 'index.js')]; + const args = ['--test', '--test-shard=0xf/20abcd', join(testFixtures, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); @@ -257,7 +257,7 @@ const testFixtures = fixtures.path('test-runner'); { // --test-shard option validation - const args = ['--test', '--test-shard=hello', join(testFixtures, 'index.js')]; + const args = ['--test', '--test-shard=hello', join(testFixtures, 'specific-test-files/success.cjs')]; const child = spawnSync(process.execPath, args, { cwd: testFixtures }); assert.strictEqual(child.status, 1); diff --git a/test/parallel/test-runner-inspect.mjs b/test/parallel/test-runner-inspect.mjs index bdff1ce7ceb84f..f8c915d1f42699 100644 --- a/test/parallel/test-runner-inspect.mjs +++ b/test/parallel/test-runner-inspect.mjs @@ -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/specific-test-files/success.test.cjs') + ); let stdout = ''; let stderr = ''; @@ -31,12 +35,12 @@ tmpdir.refresh(); { - const args = ['--test', '--inspect=0', fixtures.path('test-runner/index.js')]; + const args = ['--test', '--inspect=0', fixtures.path('test-runner/specific-test-files/throwing.js')]; const { stderr, stdout, code, signal } = await common.spawnPromisified(process.execPath, args); assert.match(stderr, /Warning: Using the inspector with --test forces running at a concurrency of 1\. Use the inspectPort option to run with concurrency/); - assert.match(stdout, /not ok 1 - .+index\.js/); + assert.match(stdout, /not ok 1 - .+throwing\.js/); assert.strictEqual(code, 1); assert.strictEqual(signal, null); } diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index b5c41b8ae925de..5c65249b8ca525 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -26,7 +26,7 @@ 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, 'specific-test-files', 'success.cjs')] }); stream.on('test:fail', common.mustNotCall()); stream.on('test:pass', common.mustCall(1)); // eslint-disable-next-line no-unused-vars @@ -34,7 +34,8 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { }); it('should run same file twice', async () => { - const stream = run({ files: [join(testFixtures, 'test/random.cjs'), join(testFixtures, 'test/random.cjs')] }); + const testFile = join(testFixtures, 'specific-test-files', 'success.cjs'); + const stream = run({ files: [testFile, testFile] }); stream.on('test:fail', common.mustNotCall()); stream.on('test:pass', common.mustCall(2)); // eslint-disable-next-line no-unused-vars @@ -68,7 +69,7 @@ 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, 'specific-test-files/success.cjs')] }).compose(dot).toArray(); assert.deepStrictEqual(result, [ '.', '\n', @@ -77,7 +78,9 @@ 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, 'specific-test-files/success.cjs')] + }).compose(specReporter).toArray(); const stringResults = result.map((bfr) => bfr.toString()); assert.match(stringResults[0], /this should pass/); assert.match(stringResults[1], /tests 1/); @@ -85,7 +88,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { }); 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, 'specific-test-files/success.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'); @@ -103,7 +106,7 @@ 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, 'specific-test-files/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'); @@ -111,7 +114,10 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { }); 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, 'specific-test-files/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'); @@ -120,7 +126,11 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { 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, 'specific-test-files/success.cjs')], + watch: true, + signal: controller.signal + }) .compose(async function* (source) { for await (const chunk of source) { if (chunk.type === 'test:pass') { @@ -135,7 +145,11 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { 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 }) + await run({ + files: [join(testFixtures, 'specific-test-files/success.cjs')], + watch: true, + signal: controller.signal + }) .on('data', function({ type }) { if (type === 'test:watch:drained') { controller.abort();