timeout.js
This file
Path
test/exit/timeout.js
75 lines · 2.7 KiB · JavaScript
Last changed by
Split files (#837)ehmickybefore adoption672c2d7
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
lines 1–75why, one step back1import test from 'ava';2import {execa, execaSync} from '../../index.js';3import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';45setFixtureDir();67test('timeout kills the process if it times out', async t => {8 const {isTerminated, signal, timedOut, originalMessage, shortMessage, message} = await t.throwsAsync(execa('forever.js', {timeout: 1}));9 t.true(isTerminated);10 t.is(signal, 'SIGTERM');11 t.true(timedOut);12 t.is(originalMessage, '');13 t.is(shortMessage, 'Command timed out after 1 milliseconds: forever.js');14 t.is(message, shortMessage);15});1617test('timeout kills the process if it times out, in sync mode', async t => {18 const {isTerminated, signal, timedOut, originalMessage, shortMessage, message} = await t.throws(() => {19 execaSync('node', ['forever.js'], {timeout: 1, cwd: FIXTURES_DIR});20 });21 t.true(isTerminated);22 t.is(signal, 'SIGTERM');23 t.true(timedOut);24 t.is(originalMessage, 'spawnSync node ETIMEDOUT');25 t.is(shortMessage, `Command timed out after 1 milliseconds: node forever.js\n${originalMessage}`);26 t.is(message, shortMessage);27});2829test('timeout does not kill the process if it does not time out', async t => {30 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});31 t.false(timedOut);32});3334test('timeout uses killSignal', async t => {35 const {isTerminated, signal, timedOut} = await t.throwsAsync(execa('forever.js', {timeout: 1, killSignal: 'SIGINT'}));36 t.true(isTerminated);37 t.is(signal, 'SIGINT');38 t.true(timedOut);39});4041const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;4243const testTimeoutValidation = (t, timeout, execaMethod) => {44 t.throws(() => {45 execaMethod('empty.js', {timeout});46 }, {message: INVALID_TIMEOUT_REGEXP});47};4849test('timeout must not be negative', testTimeoutValidation, -1, execa);50test('timeout must be an integer', testTimeoutValidation, false, execa);51test('timeout must not be negative - sync', testTimeoutValidation, -1, execaSync);52test('timeout must be an integer - sync', testTimeoutValidation, false, execaSync);5354test('timedOut is false if timeout is undefined', async t => {55 const {timedOut} = await execa('noop.js');56 t.false(timedOut);57});5859test('timedOut is false if timeout is 0', async t => {60 const {timedOut} = await execa('noop.js', {timeout: 0});61 t.false(timedOut);62});6364test('timedOut is false if timeout is undefined and exit code is 0 in sync mode', t => {65 const {timedOut} = execaSync('noop.js');66 t.false(timedOut);67});6869test('timedOut is true if the timeout happened after a different error occurred', async t => {70 const childProcess = execa('forever.js', {timeout: 1e3});71 const error = new Error('test');72 childProcess.emit('error', error);73 t.is(await t.throwsAsync(childProcess), error);74 t.true(error.timedOut);75});