Skip to content
Myr

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 file

Take it

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

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–3why, one step back
2import {once} from 'node:events';
3import {setImmediate} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 4why, one step back
4import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 5why, one step back
5import isRunning from 'is-running';

TweaksSindre Sorhusbefore adoption698a1b8

line 6why, one step back
6import {assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–8why, one step back
7import {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 back
9import {foobarString} from '../helpers/input.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 10–12why, one step back
10
11setFixtureDirectory();12

TweaksSindre Sorhusbefore adoption698a1b8

line 13why, one step back
13test('.kill(error) propagates error', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 14–16why, one step back
14 const subprocess = execa('forever.js');
15 const originalMessage = 'test';16 const cause = new Error(originalMessage);

TweaksSindre Sorhusbefore adoption698a1b8

lines 17–26why, one step back
17 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 back
27});
28

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29test('.kill(error) uses killSignal', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 30–32why, one step back
30 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 back
33 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 back
36});
37

TweaksSindre Sorhusbefore adoption698a1b8

line 38why, one step back
38test('.kill(signal, error) uses signal', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 39–41why, one step back
39 const subprocess = execa('forever.js');
40 const cause = new Error('test');41 subprocess.kill('SIGINT', cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 42–44why, one step back
42 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 back
45});
46

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 47–85why, one step back
47// 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});85

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86test('.kill(error) is a noop if subprocess already exited', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 87–88why, one step back
87 const subprocess = execa('empty.js');
88 await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

lines 89–90why, one step back
89 assert.ok(!isRunning(subprocess.pid));
90 assert.ok(!subprocess.kill(new Error('test')));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 91–92why, one step back
91});
92

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93test('.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 back
94 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 back
99 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 back
106});
107

TweaksSindre Sorhusbefore adoption698a1b8

line 108why, one step back
108test('.kill(error) twice in a row', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 109–113why, one step back
109 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 back
114 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 back
117});
118

TweaksSindre Sorhusbefore adoption698a1b8

line 119why, one step back
119test('.kill(error) does not emit the "error" event', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 120–122why, one step back
120 const subprocess = execa('forever.js');
121 const cause = new Error('test');122 subprocess.kill(cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 123–124why, one step back
123 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 back
125});
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 back
131});