Skip to content
Myr

cause.js

This file

Path

test/return/cause.js

158 lines · 5.8 KiB · JavaScript

Last changed by

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

History of this file

Take it

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 1–158why, one step back
1import test from 'ava';
2import {execa, execaSync, ExecaError, ExecaSyncError} from '../../index.js';3import {setFixtureDir} from '../helpers/fixtures-dir.js';4import {foobarString} from '../helpers/input.js';5import {getEarlyErrorSubprocess, getEarlyErrorSubprocessSync} from '../helpers/early-error.js';67setFixtureDir();89const testUnusualError = async (t, error, expectedOriginalMessage = String(error)) => {10 const subprocess = execa('empty.js');11 subprocess.emit('error', error);12 const {originalMessage, shortMessage, message} = await t.throwsAsync(subprocess);13 t.is(originalMessage, expectedOriginalMessage);14 t.true(shortMessage.includes(expectedOriginalMessage));15 t.is(message, shortMessage);16};1718test('error instance can be null', testUnusualError, null);19test('error instance can be false', testUnusualError, false);20test('error instance can be a string', testUnusualError, 'test');21test('error instance can be a number', testUnusualError, 0);22test('error instance can be a BigInt', testUnusualError, 0n);23test('error instance can be a symbol', testUnusualError, Symbol('test'));24test('error instance can be a function', testUnusualError, () => {});25test('error instance can be an array', testUnusualError, ['test', 'test']);26// eslint-disable-next-line unicorn/error-message27test('error instance can be an error with an empty message', testUnusualError, new Error(''), '');28test('error instance can be undefined', testUnusualError, undefined, 'undefined');2930test('error instance can be a plain object', async t => {31 const subprocess = execa('empty.js');32 subprocess.emit('error', {message: foobarString});33 await t.throwsAsync(subprocess, {message: new RegExp(foobarString)});34});3536const runAndFail = (t, fixtureName, argument, error) => {37 const subprocess = execa(fixtureName, [argument]);38 subprocess.emit('error', error);39 return t.throwsAsync(subprocess);40};4142const testErrorCopy = async (t, getPreviousArgument, argument = 'two') => {43 const fixtureName = 'empty.js';44 const firstArgument = 'foo';4546 const previousArgument = await getPreviousArgument(t, fixtureName);47 const previousError = await runAndFail(t, fixtureName, firstArgument, previousArgument);48 const error = await runAndFail(t, fixtureName, argument, previousError);49 const message = `Command failed: ${fixtureName} ${argument}\n${foobarString}`;5051 t.not(error, previousError);52 t.is(error.cause, previousError);53 t.is(error.command, `${fixtureName} ${argument}`);54 t.is(error.message, message);55 t.true(error.stack.includes(message));56 t.is(error.shortMessage, message);57 t.is(error.originalMessage, foobarString);58};5960test('error instance can be shared', testErrorCopy, () => new Error(foobarString));61test('error TypeError can be shared', testErrorCopy, () => new TypeError(foobarString));62test('error string can be shared', testErrorCopy, () => foobarString);63test('error copy can be shared', testErrorCopy, (t, fixtureName) => runAndFail(t, fixtureName, 'bar', new Error(foobarString)));64test('error with same message can be shared', testErrorCopy, () => new Error(foobarString), 'foo');6566test('error.cause is not set if error.exitCode is not 0', async t => {67 const {exitCode, cause} = await t.throwsAsync(execa('fail.js'));68 t.is(exitCode, 2);69 t.is(cause, undefined);70});7172test('error.cause is not set if error.isTerminated', async t => {73 const subprocess = execa('forever.js');74 subprocess.kill();75 const {isTerminated, cause} = await t.throwsAsync(subprocess);76 t.true(isTerminated);77 t.is(cause, undefined);78});7980test('error.cause is not set if error.timedOut', async t => {81 const {timedOut, cause} = await t.throwsAsync(execa('forever.js', {timeout: 1}));82 t.true(timedOut);83 t.is(cause, undefined);84});8586test('error.cause is set on error event', async t => {87 const subprocess = execa('empty.js');88 const error = new Error(foobarString);89 subprocess.emit('error', error);90 const {cause} = await t.throwsAsync(subprocess);91 t.is(cause, error);92});9394test('error.cause is set if error.isCanceled', async t => {95 const controller = new AbortController();96 const subprocess = execa('forever.js', {cancelSignal: controller.signal});97 const error = new Error('test');98 controller.abort(error);99 const {isCanceled, isTerminated, cause} = await t.throwsAsync(subprocess);100 t.true(isCanceled);101 t.false(isTerminated);102 t.is(cause.cause, error);103});104105test('error.cause is not set if error.isTerminated with .kill(error)', async t => {106 const subprocess = execa('forever.js');107 const error = new Error('test');108 subprocess.kill(error);109 const {isTerminated, cause} = await t.throwsAsync(subprocess);110 t.true(isTerminated);111 t.is(cause, error);112});113114test('Error is instanceof ExecaError', async t => {115 await t.throwsAsync(execa('fail.js'), {instanceOf: ExecaError});116});117118test('Early error is instanceof ExecaError', async t => {119 await t.throwsAsync(getEarlyErrorSubprocess(), {instanceOf: ExecaError});120});121122test('Error is instanceof ExecaSyncError', t => {123 t.throws(() => {124 execaSync('fail.js');125 }, {instanceOf: ExecaSyncError});126});127128test('Early error is instanceof ExecaSyncError', t => {129 t.throws(() => {130 getEarlyErrorSubprocessSync();131 }, {instanceOf: ExecaSyncError});132});133134test('Pipe error is instanceof ExecaError', async t => {135 await t.throwsAsync(execa('empty.js').pipe(false), {instanceOf: ExecaError});136});137138const assertNameShape = (t, error) => {139 t.false(Object.hasOwn(error, 'name'));140 t.true(Object.hasOwn(Object.getPrototypeOf(error), 'name'));141 t.false(propertyIsEnumerable.call(Object.getPrototypeOf(error), 'name'));142};143144const {propertyIsEnumerable} = Object.prototype;145146test('error.name is properly set', async t => {147 const error = await t.throwsAsync(execa('fail.js'));148 t.is(error.name, 'ExecaError');149 assertNameShape(t, error);150});151152test('error.name is properly set - sync', async t => {153 const error = await t.throws(() => {154 execaSync('fail.js');155 });156 t.is(error.name, 'ExecaSyncError');157 assertNameShape(t, error);158});