command.error.test.js
This file
Path
tests/command.error.test.js
59 lines · 1.7 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960const commander = require('../');const { test, describe } = require('node:test');const assert = require('node:assert/strict');
describe('Command.error()', () => { test('when error called with message then message displayed on stderr', (t) => { t.mock.method(process, 'exit', () => {}); const stderrSpy = t.mock.method(process.stderr, 'write', () => {});
const program = new commander.Command(); const message = 'Goodbye'; program.error(message);
const callArgs = stderrSpy.mock.calls[0].arguments; assert.equal(callArgs[0], `${message}\n`); });
test('when error called with no exitCode then process.exit(1)', (t) => { const exitSpy = t.mock.method(process, 'exit', () => {});
const program = new commander.Command(); program.configureOutput({ writeErr: () => {}, });
program.error('Goodbye');
const callArgs = exitSpy.mock.calls[0].arguments; assert.equal(callArgs[0], 1); });
test('when error called with exitCode 2 then process.exit(2)', (t) => { const exitSpy = t.mock.method(process, 'exit', () => {});
const program = new commander.Command(); program.configureOutput({ writeErr: () => {}, }); program.error('Goodbye', { exitCode: 2 });
const callArgs = exitSpy.mock.calls[0].arguments; assert.equal(callArgs[0], 2); });
test('when error called with code and exitOverride then throws with code', () => { const program = new commander.Command(); program.exitOverride().configureOutput({ writeErr: () => {}, });
const code = 'commander.mytest'; assert.throws( () => { program.error('Goodbye', { code }); }, { code }, ); });});