|
2 | 2 |
|
3 | 3 | require('../common'); |
4 | 4 | const fixtures = require('../common/fixtures.js'); |
5 | | -const assert = require('assert'); |
6 | | -const { spawnSync } = require('child_process'); |
| 5 | +const tmpdir = require('../common/tmpdir'); |
| 6 | +const assert = require('node:assert'); |
| 7 | +const fs = require('node:fs'); |
| 8 | +const { spawnSync } = require('node:child_process'); |
7 | 9 | const { describe, it } = require('node:test'); |
8 | | -const path = require('path'); |
| 10 | +const path = require('node:path'); |
| 11 | + |
| 12 | +const fixtureDir = fixtures.path('test-runner', 'flag-propagation'); |
| 13 | +const runner = path.join(fixtureDir, 'runner.mjs'); |
9 | 14 |
|
10 | | -// Test flag propagation to child test processes |
11 | | -// This validates that certain flags are/aren't propagated to child test processes |
12 | | -// based on the specification in the Node.js documentation |
13 | 15 | describe('test runner flag propagation', () => { |
14 | | - const flagPropagationTests = [ |
15 | | - ['--experimental-config-file', 'node.config.json', ''], |
16 | | - ['--experimental-default-config-file', '', false], |
17 | | - ['--env-file', '.env', '.env'], |
18 | | - ['--env-file-if-exists', '.env', '.env'], |
19 | | - ['--test-concurrency', '2', '2'], |
20 | | - // ['--test-force-exit', '', true], // <-- this test fails as is forces exit |
21 | | - // ['--test-only', '', true], // <-- this needs to be investigated |
22 | | - ['--test-timeout', '5000', '5000'], |
23 | | - ['--test-coverage-branches', '100', '100'], |
24 | | - ['--test-coverage-functions', '100', '100'], |
25 | | - ['--test-coverage-lines', '100', '100'], |
26 | | - ['--experimental-test-coverage', '', false], |
27 | | - ['--test-coverage-exclude', 'test/**', 'test/**'], |
28 | | - ['--test-coverage-include', 'src/**', 'src/**'], |
29 | | - ['--test-update-snapshots', '', true], |
30 | | - ['--import', './index.js', './index.js'], |
31 | | - ['--require', './index.js', './index.js'], |
32 | | - ]; |
| 16 | + describe('via command line', () => { |
| 17 | + const flagPropagationTests = [ |
| 18 | + ['--experimental-config-file', 'node.config.json', ''], |
| 19 | + ['--experimental-default-config-file', '', false], |
| 20 | + ['--env-file', '.env', '.env'], |
| 21 | + ['--env-file-if-exists', '.env', '.env'], |
| 22 | + ['--test-concurrency', '2', '2'], |
| 23 | + ['--test-timeout', '5000', '5000'], |
| 24 | + ['--test-coverage-branches', '100', '100'], |
| 25 | + ['--test-coverage-functions', '100', '100'], |
| 26 | + ['--test-coverage-lines', '100', '100'], |
| 27 | + ['--experimental-test-coverage', '', false], |
| 28 | + ['--test-coverage-exclude', 'test/**', 'test/**'], |
| 29 | + ['--test-coverage-include', 'src/**', 'src/**'], |
| 30 | + ['--test-update-snapshots', '', true], |
| 31 | + ['--import', './index.js', './index.js'], |
| 32 | + ['--require', './index.js', './index.js'], |
| 33 | + ]; |
| 34 | + |
| 35 | + for (const [flagName, testValue, expectedValue] of flagPropagationTests) { |
| 36 | + const testDescription = `should propagate ${flagName} to child tests as expected`; |
| 37 | + |
| 38 | + it(testDescription, () => { |
| 39 | + const args = [ |
| 40 | + '--test-reporter=tap', |
| 41 | + '--no-warnings', |
| 42 | + '--expose-internals', |
| 43 | + // We need to pass the flag that will be propagated to the child test |
| 44 | + testValue ? `${flagName}=${testValue}` : flagName, |
| 45 | + // Use the runner fixture |
| 46 | + runner, |
| 47 | + // Pass parameters to the fixture |
| 48 | + `--flag=${flagName}`, |
| 49 | + `--expected=${expectedValue}`, |
| 50 | + `--description="${testDescription}"`, |
| 51 | + ].filter(Boolean); |
| 52 | + |
| 53 | + const child = spawnSync( |
| 54 | + process.execPath, |
| 55 | + args, |
| 56 | + { |
| 57 | + cwd: fixtureDir, |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + assert.strictEqual(child.status, 0, `Flag propagation test failed for ${flagName}.`); |
| 62 | + const stdout = child.stdout.toString(); |
| 63 | + assert.match(stdout, /tests 1/, `Test should execute for ${flagName}`); |
| 64 | + assert.match(stdout, /pass 1/, `Test should pass for ${flagName} propagation check`); |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + describe('via config file', () => { |
| 70 | + const configFilePropagationTests = [ |
| 71 | + ['--test-concurrency', 2, 2, 'testRunner'], |
| 72 | + ['--test-timeout', 5000, 5000, 'testRunner'], |
| 73 | + ['--test-coverage-branches', 100, 100, 'testRunner'], |
| 74 | + ['--test-coverage-functions', 100, 100, 'testRunner'], |
| 75 | + ['--test-coverage-lines', 100, 100, 'testRunner'], |
| 76 | + ['--experimental-test-coverage', true, false, 'testRunner'], |
| 77 | + ['--test-coverage-exclude', 'test/**', 'test/**', 'testRunner'], |
| 78 | + ['--test-coverage-include', 'src/**', 'src/**', 'testRunner'], |
| 79 | + ['--test-update-snapshots', true, true, 'testRunner'], |
| 80 | + ['--test-concurrency', 3, 3, 'testRunner'], |
| 81 | + ['--test-timeout', 2500, 2500, 'testRunner'], |
| 82 | + ['--test-coverage-branches', 90, 90, 'testRunner'], |
| 83 | + ['--test-coverage-functions', 85, 85, 'testRunner'], |
| 84 | + ]; |
| 85 | + |
| 86 | + for (const [flagName, configValue, expectedValue, namespace] of configFilePropagationTests) { |
| 87 | + const testDescription = `should propagate ${flagName} from config file (${namespace}) to child tests`; |
| 88 | + |
| 89 | + it(testDescription, () => { |
| 90 | + tmpdir.refresh(); |
33 | 91 |
|
34 | | - // Path to the static fixture |
35 | | - const fixtureDir = fixtures.path('test-runner', 'flag-propagation'); |
36 | | - const runner = path.join(fixtureDir, 'runner.mjs'); |
| 92 | + // Create a temporary config file |
| 93 | + const configFile = path.join(tmpdir.path, 'test-config.json'); |
| 94 | + const configContent = { |
| 95 | + [namespace]: { |
| 96 | + [flagName.replace('--', '')]: configValue |
| 97 | + } |
| 98 | + }; |
37 | 99 |
|
38 | | - for (const [flagName, testValue, expectedValue] of flagPropagationTests) { |
39 | | - const testDescription = `should propagate ${flagName} to child tests as expected`; |
| 100 | + fs.writeFileSync(configFile, JSON.stringify(configContent, null, 2)); |
40 | 101 |
|
41 | | - it(testDescription, () => { |
42 | | - const args = [ |
43 | | - '--test-reporter=tap', |
44 | | - '--no-warnings', |
45 | | - '--expose-internals', |
46 | | - // We need to pass the flag that will be propagated to the child test |
47 | | - testValue ? `${flagName}=${testValue}` : flagName, |
48 | | - // Use the runner fixture |
49 | | - runner, |
50 | | - // Pass parameters to the fixture |
51 | | - `--flag=${flagName}`, |
52 | | - `--expected=${expectedValue}`, |
53 | | - `--description="${testDescription}"`, |
54 | | - ].filter(Boolean); |
| 102 | + const args = [ |
| 103 | + '--test-reporter=tap', |
| 104 | + '--no-warnings', |
| 105 | + '--expose-internals', |
| 106 | + `--experimental-config-file=${configFile}`, |
| 107 | + runner, |
| 108 | + `--flag=${flagName}`, |
| 109 | + `--expected=${expectedValue}`, |
| 110 | + `--description="${testDescription}"`, |
| 111 | + ]; |
55 | 112 |
|
56 | | - const child = spawnSync( |
57 | | - process.execPath, |
58 | | - args, |
59 | | - { |
60 | | - cwd: fixtureDir, |
61 | | - }, |
62 | | - ); |
| 113 | + const child = spawnSync( |
| 114 | + process.execPath, |
| 115 | + args, |
| 116 | + { |
| 117 | + cwd: fixtureDir, |
| 118 | + }, |
| 119 | + ); |
63 | 120 |
|
64 | | - assert.strictEqual(child.status, 0, `Flag propagation test failed for ${flagName}.`); |
65 | | - const stdout = child.stdout.toString(); |
66 | | - assert.match(stdout, /tests 1/, `Test should execute for ${flagName}`); |
67 | | - assert.match(stdout, /pass 1/, `Test should pass for ${flagName} propagation check`); |
68 | | - }); |
69 | | - } |
| 121 | + assert.strictEqual(child.status, 0, `Config file propagation test failed for ${flagName}.`); |
| 122 | + const stdout = child.stdout.toString(); |
| 123 | + assert.match(stdout, /tests 1/, `Test should execute for config file ${flagName}`); |
| 124 | + assert.match(stdout, /pass 1/, `Test should pass for config file ${flagName} propagation check`); |
| 125 | + }); |
| 126 | + } |
| 127 | + }); |
70 | 128 | }); |
0 commit comments