Skip to content
Myr

timeout.js

This file

Path

test/terminate/timeout.js

97 lines · 4.2 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertThrows, assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 4–8why, one step back
4import {execa, execaSync} from '../../index.js';
5import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';67setFixtureDirectory();8

TweaksSindre Sorhusbefore adoption698a1b8

lines 9–16why, one step back
9test('timeout kills the subprocess if it times out', async () => {
10 const {isTerminated, signal, timedOut, originalMessage, shortMessage, message} = await assertRejects(execa('forever.js', {timeout: 1}));11 assert.equal(isTerminated, true);12 assert.equal(signal, 'SIGTERM');13 assert.equal(timedOut, true);14 assert.equal(originalMessage, undefined);15 assert.equal(shortMessage, 'Command timed out after 1 milliseconds: forever.js');16 assert.equal(message, shortMessage);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 17–18why, one step back
17});
18

TweaksSindre Sorhusbefore adoption698a1b8

lines 19–20why, one step back
19test('timeout kills the subprocess if it times out, in sync mode', async () => {
20 const {isTerminated, signal, timedOut, originalMessage, shortMessage, message} = await assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–22why, one step back
21 execaSync('node', ['forever.js'], {timeout: 1, cwd: FIXTURES_DIRECTORY});
22 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 23–25why, one step back
23 assert.equal(isTerminated, true);
24 assert.equal(signal, 'SIGTERM');25 assert.equal(timedOut, true);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

line 26why, one step back
26 // On Windows, the command is spawned using the absolute path resolved via `PATHEXT`, so it appears in Node.js' own error message

TweaksSindre Sorhusbefore adoption698a1b8

lines 27–29why, one step back
27 assert.match(originalMessage, /^spawnSync .*node(?:\.[A-Za-z]+)? ETIMEDOUT$/);
28 assert.equal(shortMessage, `Command timed out after 1 milliseconds: node forever.js\n${originalMessage}`);29 assert.equal(message, shortMessage);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 30–31why, one step back
30});
31

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32test('timeout does not kill the subprocess if it does not time out', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 33why, one step back
33 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34 assert.equal(timedOut, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 35–36why, one step back
35});
36

TweaksSindre Sorhusbefore adoption698a1b8

lines 37–41why, one step back
37test('timeout uses killSignal', async () => {
38 const {isTerminated, signal, timedOut} = await assertRejects(execa('forever.js', {timeout: 1, killSignal: 'SIGINT'}));39 assert.equal(isTerminated, true);40 assert.equal(signal, 'SIGINT');41 assert.equal(timedOut, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 42–45why, one step back
42});
4344const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;45

TweaksSindre Sorhusbefore adoption698a1b8

lines 46–47why, one step back
46const testTimeoutValidation = (timeout, execaMethod) => {
47 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 48–51why, one step back
48 execaMethod('empty.js', {timeout});
49 }, {message: INVALID_TIMEOUT_REGEXP});50};51

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

lines 52–53why, one step back
52test('timeout must be an integer', () => testTimeoutValidation(100.5, execa));
53test('timeout must be an integer - sync', () => testTimeoutValidation(100.5, execaSync));

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('timeout must not be negative', () => testTimeoutValidation(-1, execa));

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

line 55why, one step back
55test('timeout must be a number', () => testTimeoutValidation(false, execa));

TweaksSindre Sorhusbefore adoption698a1b8

line 56why, one step back
56test('timeout must not be negative - sync', () => testTimeoutValidation(-1, execaSync));

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

lines 57–60why, one step back
57test('timeout must be a number - sync', () => testTimeoutValidation(false, execaSync));
58// Larger delays make timers fire right away59test('timeout must not be larger than the maximum timer delay', () => testTimeoutValidation(2 ** 31, execa));60test('timeout must not be larger than the maximum timer delay - sync', () => testTimeoutValidation(2 ** 31, execaSync));

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 61–65why, one step back
61test('timeout must not be null', () => {
62 assertThrows(() => {63 execa('empty.js', {timeout: null});64 }, {message: /got `null` \(null\)/});65});

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

lines 66–73why, one step back
66
67const testMaxTimeout = async execaMethod => {68 const {timedOut} = await execaMethod('noop.js', {timeout: (2 ** 31) - 1});69 assert.equal(timedOut, false);70};7172test('timeout can be the maximum timer delay', () => testMaxTimeout(execa));73test('timeout can be the maximum timer delay - sync', () => testMaxTimeout(execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 74why, one step back
74

TweaksSindre Sorhusbefore adoption698a1b8

line 75why, one step back
75test('timedOut is false if timeout is undefined', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 76why, one step back
76 const {timedOut} = await execa('noop.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77 assert.equal(timedOut, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 78–79why, one step back
78});
79

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80test('timedOut is false if timeout is 0', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 81why, one step back
81 const {timedOut} = await execa('noop.js', {timeout: 0});

TweaksSindre Sorhusbefore adoption698a1b8

line 82why, one step back
82 assert.equal(timedOut, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 83–84why, one step back
83});
84

TweaksSindre Sorhusbefore adoption698a1b8

line 85why, one step back
85test('timedOut is false if timeout is undefined and exit code is 0 in sync mode', () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 86why, one step back
86 const {timedOut} = execaSync('noop.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 87why, one step back
87 assert.equal(timedOut, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 88–89why, one step back
88});
89

TweaksSindre Sorhusbefore adoption698a1b8

line 90why, one step back
90test('timedOut is false if the timeout happened after a different error occurred', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 91–92why, one step back
91 const subprocess = execa('forever.js', {timeout: 1e3});
92 const cause = new Error('test');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 93why, one step back
93 subprocess.nodeChildProcess.emit('error', cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 94–96why, one step back
94 const error = await assertRejects(subprocess);
95 assert.equal(error.cause, cause);96 assert.equal(error.timedOut, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 97why, one step back
97});