kill-error.js
This file
Path
test/terminate/kill-error.js
131 lines · 4.7 KiB · JavaScript
Last changed by
Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–3why, one step back2import {once} from 'node:events';3import {setImmediate} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 5why, one step back5import isRunning from 'is-running';TweaksSindre Sorhusbefore adoption698a1b8
line 6why, one step back6import {assertRejects} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–8why, one step back7import {execa} from '../../index.js';8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 9why, one step back9import {foobarString} from '../helpers/input.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 10–12why, one step back1011setFixtureDirectory();12TweaksSindre Sorhusbefore adoption698a1b8
line 13why, one step back13test('.kill(error) propagates error', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 14–16why, one step back14 const subprocess = execa('forever.js');15 const originalMessage = 'test';16 const cause = new Error(originalMessage);TweaksSindre Sorhusbefore adoption698a1b8
lines 17–26why, one step back17 assert.ok(subprocess.kill(cause));18 const error = await assertRejects(subprocess);19 assert.equal(error.cause, cause);20 assert.ok(cause.stack.includes(import.meta.url));21 assert.equal(error.exitCode, undefined);22 assert.equal(error.signal, 'SIGTERM');23 assert.equal(error.isTerminated, true);24 assert.equal(error.originalMessage, originalMessage);25 assert.ok(error.message.includes(originalMessage));26 assert.ok(error.message.includes('was killed with SIGTERM'));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–28why, one step back27});28TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29test('.kill(error) uses killSignal', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–32why, one step back30 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});31 const cause = new Error('test');32 subprocess.kill(cause);TweaksSindre Sorhusbefore adoption698a1b8
lines 33–35why, one step back33 const error = await assertRejects(subprocess);34 assert.equal(error.cause, cause);35 assert.equal(error.signal, 'SIGINT');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 36–37why, one step back36});37TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38test('.kill(signal, error) uses signal', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 39–41why, one step back39 const subprocess = execa('forever.js');40 const cause = new Error('test');41 subprocess.kill('SIGINT', cause);TweaksSindre Sorhusbefore adoption698a1b8
lines 42–44why, one step back42 const error = await assertRejects(subprocess);43 assert.equal(error.cause, cause);44 assert.equal(error.signal, 'SIGINT');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 45–46why, one step back45});46Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 47–85why, one step back47// Their `Object.prototype.toString()` is not `[object Error]`48class TaggedError extends Error {49 get [Symbol.toStringTag]() {50 return 'TaggedError';51 }52}5354const testKillErrorSubclass = async cause => {55 const subprocess = execa('forever.js');56 assert.ok(subprocess.kill(cause));57 const error = await assertRejects(subprocess);58 assert.equal(error.cause, cause);59 assert.equal(error.signal, 'SIGTERM');60 assert.equal(error.originalMessage, cause.message);61 assert.ok(error.shortMessage.startsWith('Command was killed with SIGTERM'));62 return error;63};6465test('.kill(error) works with DOMException', async () => {66 const error = await testKillErrorSubclass(AbortSignal.abort().reason);67 // `DOMException` has a legacy numeric `code`, which is not an error code like `ENOENT`68 assert.equal(error.code, undefined);69});7071test('.kill(error) works with Error subclasses with a custom Symbol.toStringTag', () => testKillErrorSubclass(new TaggedError('test')));7273test('.kill(error) ignores numeric error codes', async () => {74 const error = await testKillErrorSubclass(Object.assign(new Error('test'), {code: 42}));75 assert.equal(error.code, undefined);76 assert.ok(!error.shortMessage.includes('42'));77});7879test('.kill(error) keeps string error codes', async () => {80 const subprocess = execa('forever.js');81 subprocess.kill(Object.assign(new Error('test'), {code: 'ECUSTOM'}));82 const error = await assertRejects(subprocess);83 assert.equal(error.code, 'ECUSTOM');84});85TweaksSindre Sorhusbefore adoption698a1b8
line 86why, one step back86test('.kill(error) is a noop if subprocess already exited', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 87–88why, one step back87 const subprocess = execa('empty.js');88 await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 89–90why, one step back89 assert.ok(!isRunning(subprocess.pid));90 assert.ok(!subprocess.kill(new Error('test')));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 91–92why, one step back91});92TweaksSindre Sorhusbefore adoption698a1b8
line 93why, one step back93test('.kill(error) terminates but does not change the error if the subprocess already errored but did not exit yet', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 94–98why, one step back94 const subprocess = execa('forever.js');95 const cause = new Error('first');96 subprocess.stdout.destroy(cause);97 await setImmediate();98 const secondError = new Error('second');TweaksSindre Sorhusbefore adoption698a1b8
lines 99–105why, one step back99 assert.ok(subprocess.kill(secondError));100 const error = await assertRejects(subprocess);101 assert.equal(error.cause, cause);102 assert.equal(error.exitCode, undefined);103 assert.equal(error.signal, 'SIGTERM');104 assert.equal(error.isTerminated, true);105 assert.ok(!error.message.includes(secondError.message));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 106–107why, one step back106});107TweaksSindre Sorhusbefore adoption698a1b8
line 108why, one step back108test('.kill(error) twice in a row', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 109–113why, one step back109 const subprocess = execa('forever.js');110 const cause = new Error('first');111 subprocess.kill(cause);112 const secondCause = new Error('second');113 subprocess.kill(secondCause);TweaksSindre Sorhusbefore adoption698a1b8
lines 114–116why, one step back114 const error = await assertRejects(subprocess);115 assert.equal(error.cause, cause);116 assert.ok(!error.message.includes(secondCause.message));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 117–118why, one step back117});118TweaksSindre Sorhusbefore adoption698a1b8
line 119why, one step back119test('.kill(error) does not emit the "error" event', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 120–122why, one step back120 const subprocess = execa('forever.js');121 const cause = new Error('test');122 subprocess.kill(cause);TweaksSindre Sorhusbefore adoption698a1b8
lines 123–124why, one step back123 const error = await Promise.race([assertRejects(subprocess), once(subprocess.nodeChildProcess, 'error')]);124 assert.equal(error.cause, cause);Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 125–130why, one step back125});126127test('.kill(error) returns false once the subprocess has settled', async () => {128 const subprocess = execa('noop.js', [foobarString]);129 await subprocess;130 assert.ok(!subprocess.kill(new Error('test')));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 131why, one step back131});