kill-error.js
This file
Path
test/terminate/kill-error.js
83 lines · 2.8 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–5why, one step back1import {once} from 'node:events';2import {setImmediate} from 'node:timers/promises';3import test from 'ava';4import isRunning from 'is-running';5import {execa} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 6why, one step back6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
line 7why, one step back7Upgrade xo (#980)ehmickybefore adoptionccfa412
line 8why, one step back8setFixtureDirectory();Rename files (#977)ehmickybefore adoption4b2e316
lines 9–83why, one step back910test('.kill(error) propagates error', async t => {11 const subprocess = execa('forever.js');12 const originalMessage = 'test';13 const cause = new Error(originalMessage);14 t.true(subprocess.kill(cause));15 const error = await t.throwsAsync(subprocess);16 t.is(error.cause, cause);17 t.true(cause.stack.includes(import.meta.url));18 t.is(error.exitCode, undefined);19 t.is(error.signal, 'SIGTERM');20 t.true(error.isTerminated);21 t.is(error.originalMessage, originalMessage);22 t.true(error.message.includes(originalMessage));23 t.true(error.message.includes('was killed with SIGTERM'));24});2526test('.kill(error) uses killSignal', async t => {27 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});28 const cause = new Error('test');29 subprocess.kill(cause);30 const error = await t.throwsAsync(subprocess);31 t.is(error.cause, cause);32 t.is(error.signal, 'SIGINT');33});3435test('.kill(signal, error) uses signal', async t => {36 const subprocess = execa('forever.js');37 const cause = new Error('test');38 subprocess.kill('SIGINT', cause);39 const error = await t.throwsAsync(subprocess);40 t.is(error.cause, cause);41 t.is(error.signal, 'SIGINT');42});4344test('.kill(error) is a noop if subprocess already exited', async t => {45 const subprocess = execa('empty.js');46 await subprocess;47 t.false(isRunning(subprocess.pid));48 t.false(subprocess.kill(new Error('test')));49});5051test('.kill(error) terminates but does not change the error if the subprocess already errored but did not exit yet', async t => {52 const subprocess = execa('forever.js');53 const cause = new Error('first');54 subprocess.stdout.destroy(cause);55 await setImmediate();56 const secondError = new Error('second');57 t.true(subprocess.kill(secondError));58 const error = await t.throwsAsync(subprocess);59 t.is(error.cause, cause);60 t.is(error.exitCode, undefined);61 t.is(error.signal, 'SIGTERM');62 t.true(error.isTerminated);63 t.false(error.message.includes(secondError.message));64});6566test('.kill(error) twice in a row', async t => {67 const subprocess = execa('forever.js');68 const cause = new Error('first');69 subprocess.kill(cause);70 const secondCause = new Error('second');71 subprocess.kill(secondCause);72 const error = await t.throwsAsync(subprocess);73 t.is(error.cause, cause);74 t.false(error.message.includes(secondCause.message));75});7677test('.kill(error) does not emit the "error" event', async t => {78 const subprocess = execa('forever.js');79 const cause = new Error('test');80 subprocess.kill(cause);81 const error = await Promise.race([t.throwsAsync(subprocess), once(subprocess, 'error')]);82 t.is(error.cause, cause);83});