|
1 | 1 | import * as common from '../common/index.mjs'; |
2 | 2 | import * as fixtures from '../common/fixtures.mjs'; |
3 | 3 | import { basename, join } from 'node:path'; |
| 4 | +import { spawn } from 'node:child_process'; |
| 5 | +import { once } from 'node:events'; |
| 6 | +import { finished } from 'node:stream/promises'; |
4 | 7 | import { describe, it, run } from 'node:test'; |
5 | 8 | import { dot, spec, tap } from 'node:test/reporters'; |
6 | 9 | import consumers from 'node:stream/consumers'; |
@@ -590,6 +593,12 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { |
590 | 593 | assert.deepStrictEqual(executedTestFiles.sort(), [...shardsTestsFiles].sort()); |
591 | 594 | }); |
592 | 595 |
|
| 596 | + it('should only allow boolean in options,handleSignals', () => { |
| 597 | + [Symbol(), {}, [], () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([])] |
| 598 | + .forEach((handleSignals) => assert.throws(() => run({ handleSignals }), { |
| 599 | + code: 'ERR_INVALID_ARG_TYPE', |
| 600 | + })); |
| 601 | + }); |
593 | 602 | }); |
594 | 603 |
|
595 | 604 | describe('randomization', () => { |
@@ -819,6 +828,74 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { |
819 | 828 | assert.strictEqual(diagnostics.includes(entry), true); |
820 | 829 | } |
821 | 830 | }); |
| 831 | + |
| 832 | + async function runHandleSignalsFixture(mode) { |
| 833 | + if (common.isWindows) { |
| 834 | + common.printSkipMessage('signals are not supported on Windows'); |
| 835 | + return null; |
| 836 | + } |
| 837 | + |
| 838 | + let stdout = ''; |
| 839 | + let sentSignal = false; |
| 840 | + |
| 841 | + const child = spawn(process.execPath, [ |
| 842 | + fixtures.path('test-runner', 'run-handle-signals.mjs'), |
| 843 | + mode, |
| 844 | + fixtures.path('test-runner', 'never_ending_async.js'), |
| 845 | + ]); |
| 846 | + |
| 847 | + const timeout = setTimeout(() => { |
| 848 | + child.kill('SIGINT'); |
| 849 | + }, common.platformTimeout(5000)); |
| 850 | + |
| 851 | + child.stdout.setEncoding('utf8'); |
| 852 | + child.stdout.on('data', (chunk) => { |
| 853 | + stdout += chunk; |
| 854 | + |
| 855 | + if (!sentSignal && stdout.includes('READY')) { |
| 856 | + sentSignal = true; |
| 857 | + child.kill('SIGINT'); |
| 858 | + } |
| 859 | + }); |
| 860 | + |
| 861 | + const [code, signal] = await once(child, 'exit'); |
| 862 | + clearTimeout(timeout); |
| 863 | + await finished(child.stdout); |
| 864 | + |
| 865 | + return { code, signal, stdout }; |
| 866 | + } |
| 867 | + |
| 868 | + describe('handleSignals', () => { |
| 869 | + it('should handle SIGINT when handleSignals is true', async () => { |
| 870 | + const result = await runHandleSignalsFixture('handle-signals'); |
| 871 | + if (result === null) return; |
| 872 | + |
| 873 | + assert.strictEqual(result.signal, null); |
| 874 | + assert.strictEqual(result.code, 1); |
| 875 | + assert.match(result.stdout, /Interrupted while running:/); |
| 876 | + assert.match(result.stdout, /never_ending_async\.js/); |
| 877 | + }); |
| 878 | + |
| 879 | + it('should not handle SIGINT by default in run() API', async () => { |
| 880 | + const result = await runHandleSignalsFixture('default'); |
| 881 | + if (result === null) return; |
| 882 | + |
| 883 | + assert.strictEqual(result.signal, null); |
| 884 | + assert.strictEqual(result.code, 0); |
| 885 | + assert.match(result.stdout, /user SIGINT handler/); |
| 886 | + assert.doesNotMatch(result.stdout, /Interrupted while running/); |
| 887 | + }); |
| 888 | + |
| 889 | + it('should not handle SIGINT when handleSignals is false', async () => { |
| 890 | + const result = await runHandleSignalsFixture('no-handle-signals'); |
| 891 | + if (result === null) return; |
| 892 | + |
| 893 | + assert.strictEqual(result.signal, null); |
| 894 | + assert.strictEqual(result.code, 0); |
| 895 | + assert.match(result.stdout, /user SIGINT handler/); |
| 896 | + assert.doesNotMatch(result.stdout, /Interrupted while running/); |
| 897 | + }); |
| 898 | + }); |
822 | 899 | }); |
823 | 900 |
|
824 | 901 | describe('env', () => { |
|
0 commit comments