Skip to content
Myr

Improve `error.message` (#705)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 6bbd265 · Browse files · 4 files+52 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+52 added−31 removed

  • tests:+36 added−25 removed
  • other:+16 added−6 removed

4 files+52 added−31 removed

modified:lib/error.js+16 added-6 removed
added:test/fixtures/noop-both-fail.js+6 added
modified:test/helpers/stdio.js+2 added-2 removed
modified:test/error.js+28 added-23 removed
1import process from 'node:process';2import test from 'ava';3import {execa, execaSync} from '../index.js';4import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';5import {fullStdio} from './helpers/stdio.js';5import {fullStdio, getStdio} from './helpers/stdio.js';67const isWindows = process.platform === 'win32';89setFixtureDir();
87 unmodified lines
97test('error.message contains the command', async t => {98 await t.throwsAsync(execa('exit.js', ['2', 'foo', 'bar']), {message: /exit.js 2 foo bar/});99});100101const testStdioMessage = async (t, encoding) => {102 const {message} = await t.throwsAsync(execa('echo-fail.js', {...fullStdio, encoding}));103 t.true(message.endsWith('stderr\nstdout\nfd3'));101const testStdioMessage = async (t, encoding, all) => {102 const {message} = await t.throwsAsync(execa('echo-fail.js', {...fullStdio, encoding, all}));103 const output = all ? 'stdout\nstderr' : 'stderr\n\nstdout';104 t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));105};106106test('error.message contains stdout/stderr/stdio if available', testStdioMessage, 'utf8');107test('error.message contains stdout/stderr/stdio even with encoding "buffer"', testStdioMessage, 'buffer');107test('error.message contains stdout/stderr/stdio if available', testStdioMessage, 'utf8', false);108test('error.message contains stdout/stderr/stdio even with encoding "buffer"', testStdioMessage, 'buffer', false);109test('error.message contains all if available', testStdioMessage, 'utf8', true);110test('error.message contains all even with encoding "buffer"', testStdioMessage, 'buffer', true);111109test('error.message does not contain stdout if not available', async t => {110 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: ['pipe', 'ignore', 'pipe', 'pipe']}));111 t.true(message.endsWith('stderr\nfd3'));112});112const testPartialIgnoreMessage = async (t, index, stdioOption, output) => {113 const {message} = await t.throwsAsync(execa('echo-fail.js', getStdio(index, stdioOption, 4)));114 t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));115};116114test('error.message does not contain stderr if not available', async t => {115 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: ['pipe', 'pipe', 'ignore', 'pipe']}));116 t.true(message.endsWith('stdout\nfd3'));117});117test('error.message does not contain stdout if not available', testPartialIgnoreMessage, 1, 'ignore', 'stderr');118test('error.message does not contain stderr if not available', testPartialIgnoreMessage, 2, 'ignore', 'stdout');119119test('error.message does not contain stdout/stderr/stdio if not available', async t => {120 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: 'ignore'}));120const testFullIgnoreMessage = async (t, options, resultProperty) => {121 const {[resultProperty]: message} = await t.throwsAsync(execa('echo-fail.js', options));122 t.false(message.includes('stderr'));123 t.false(message.includes('stdout'));124 t.false(message.includes('fd3'));124});125};126126test('error.shortMessage does not contain stdout/stderr/stdio', async t => {127 const {shortMessage} = await t.throwsAsync(execa('echo-fail.js', fullStdio));128 t.false(shortMessage.includes('stderr'));129 t.false(shortMessage.includes('stdout'));130 t.false(shortMessage.includes('fd3'));131});127test('error.message does not contain stdout/stderr/stdio if not available', testFullIgnoreMessage, {stdio: 'ignore'}, 'message');128test('error.shortMessage does not contain stdout/stderr/stdio', testFullIgnoreMessage, fullStdio, 'shortMessage');129130const testErrorMessageConsistent = async (t, stdout) => {131 const {message} = await t.throwsAsync(execa('noop-both-fail.js', [stdout, 'stderr']));132 t.true(message.endsWith(`noop-both-fail.js ${stdout} stderr\n\nstderr\n\nstdout`));133};134135test('error.message newlines are consistent - no newline', testErrorMessageConsistent, 'stdout');136test('error.message newlines are consistent - newline', testErrorMessageConsistent, 'stdout\n');137138test('Original error.message is kept', async t => {139 const {originalMessage} = await t.throwsAsync(execa('noop.js', {cwd: 1}));140 t.true(originalMessage.startsWith('The "options.cwd" property must be of type string or an instance of Buffer or URL. Received type number'));141});
139 unmodified lines