Skip to content
Myr

command.configureOutput.test.js

This file

Path

tests/command.configureOutput.test.js

275 lines · 7.7 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 20cef03 point at it.

Update supported details for 7.x (#1419)John Geebefore adoption20cef03

lines 1–275why, one step back
1const commander = require('../');
23test('when default writeErr() then error on stderr', () => {4 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });5 const program = new commander.Command();6 program.exitOverride();78 try {9 program.parse(['--unknown'], { from: 'user' });10 } catch (err) {11 }1213 expect(writeSpy).toHaveBeenCalledTimes(1);14 writeSpy.mockRestore();15});1617test('when custom writeErr() then error on custom output', () => {18 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });19 const customWrite = jest.fn();20 const program = new commander.Command();21 program22 .exitOverride()23 .configureOutput({ writeErr: customWrite });2425 try {26 program.parse(['--unknown'], { from: 'user' });27 } catch (err) {28 }2930 expect(writeSpy).toHaveBeenCalledTimes(0);31 expect(customWrite).toHaveBeenCalledTimes(1);32 writeSpy.mockRestore();33});3435test('when default write() then version on stdout', () => {36 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });37 const program = new commander.Command();38 program39 .exitOverride()40 .version('1.2.3');4142 expect(() => {43 program.parse(['--version'], { from: 'user' });44 }).toThrow();4546 expect(writeSpy).toHaveBeenCalledTimes(1);47 writeSpy.mockRestore();48});4950test('when custom write() then version on custom output', () => {51 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });52 const customWrite = jest.fn();53 const program = new commander.Command();54 program55 .exitOverride()56 .version('1.2.3')57 .configureOutput({ writeOut: customWrite });5859 expect(() => {60 program.parse(['--version'], { from: 'user' });61 }).toThrow();6263 expect(writeSpy).toHaveBeenCalledTimes(0);64 expect(customWrite).toHaveBeenCalledTimes(1);65 writeSpy.mockRestore();66});6768test('when default write() then help on stdout', () => {69 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });70 const program = new commander.Command();71 program.outputHelp();7273 expect(writeSpy).toHaveBeenCalledTimes(1);74 writeSpy.mockRestore();75});7677test('when custom write() then help error on custom output', () => {78 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });79 const customWrite = jest.fn();80 const program = new commander.Command();81 program.configureOutput({ writeOut: customWrite });82 program.outputHelp();8384 expect(writeSpy).toHaveBeenCalledTimes(0);85 expect(customWrite).toHaveBeenCalledTimes(1);86 writeSpy.mockRestore();87});8889test('when default writeErr then help error on stderr', () => {90 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });91 const program = new commander.Command();92 program.outputHelp({ error: true });9394 expect(writeSpy).toHaveBeenCalledTimes(1);95 writeSpy.mockRestore();96});9798test('when custom writeErr then help error on custom output', () => {99 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });100 const customWrite = jest.fn();101 const program = new commander.Command();102 program.configureOutput({ writeErr: customWrite });103 program.outputHelp({ error: true });104105 expect(writeSpy).toHaveBeenCalledTimes(0);106 expect(customWrite).toHaveBeenCalledTimes(1);107 writeSpy.mockRestore();108});109110test('when default getOutHelpWidth then help helpWidth from stdout', () => {111 const expectedColumns = 123;112 const holdIsTTY = process.stdout.isTTY;113 const holdColumns = process.stdout.columns;114 let helpWidth;115116 process.stderr.isTTY = true;117 process.stdout.columns = expectedColumns;118 process.stdout.isTTY = true;119 const program = new commander.Command();120 program121 .configureHelp({122 formatHelp: (cmd, helper) => {123 helpWidth = helper.helpWidth;124 return '';125 }126 });127 program.outputHelp();128129 expect(helpWidth).toBe(expectedColumns);130 process.stdout.columns = holdColumns;131 process.stdout.isTTY = holdIsTTY;132});133134test('when custom getOutHelpWidth then help helpWidth custom', () => {135 const expectedColumns = 123;136 let helpWidth;137138 const program = new commander.Command();139 program140 .configureHelp({141 formatHelp: (cmd, helper) => {142 helpWidth = helper.helpWidth;143 return '';144 }145 }).configureOutput({146 getOutHelpWidth: () => expectedColumns147 });148 program.outputHelp();149150 expect(helpWidth).toBe(expectedColumns);151});152153test('when default getErrHelpWidth then help error helpWidth from stderr', () => {154 const expectedColumns = 123;155 const holdIsTTY = process.stderr.isTTY;156 const holdColumns = process.stderr.columns;157 let helpWidth;158159 process.stderr.isTTY = true;160 process.stderr.columns = expectedColumns;161 const program = new commander.Command();162 program163 .configureHelp({164 formatHelp: (cmd, helper) => {165 helpWidth = helper.helpWidth;166 return '';167 }168 });169 program.outputHelp({ error: true });170171 expect(helpWidth).toBe(expectedColumns);172 process.stderr.isTTY = holdIsTTY;173 process.stderr.columns = holdColumns;174});175176test('when custom getErrHelpWidth then help error helpWidth custom', () => {177 const expectedColumns = 123;178 let helpWidth;179180 const program = new commander.Command();181 program182 .configureHelp({183 formatHelp: (cmd, helper) => {184 helpWidth = helper.helpWidth;185 return '';186 }187 }).configureOutput({188 getErrHelpWidth: () => expectedColumns189 });190 program.outputHelp({ error: true });191192 expect(helpWidth).toBe(expectedColumns);193});194195test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => {196 const expectedColumns = 123;197 let helpWidth;198199 const program = new commander.Command();200 program201 .configureHelp({202 formatHelp: (cmd, helper) => {203 helpWidth = helper.helpWidth;204 return '';205 },206 helpWidth: expectedColumns207 }).configureOutput({208 getOutHelpWidth: () => 999209 });210 program.outputHelp();211212 expect(helpWidth).toBe(expectedColumns);213});214215test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => {216 const expectedColumns = 123;217 let helpWidth;218219 const program = new commander.Command();220 program221 .configureHelp({222 formatHelp: (cmd, helper) => {223 helpWidth = helper.helpWidth;224 return '';225 },226 helpWidth: expectedColumns227 }).configureOutput({228 getErrHelpWidth: () => 999229 });230 program.outputHelp({ error: true });231232 expect(helpWidth).toBe(expectedColumns);233});234235test('when set configureOutput then get configureOutput', () => {236 const outputOptions = {237 writeOut: jest.fn(),238 writeErr: jest.fn(),239 getOutHelpWidth: jest.fn(),240 getErrHelpWidth: jest.fn(),241 outputError: jest.fn()242 };243 const program = new commander.Command();244 program.configureOutput(outputOptions);245 expect(program.configureOutput()).toEqual(outputOptions);246});247248test('when custom outputErr and error then outputErr called', () => {249 const outputError = jest.fn();250 const program = new commander.Command();251 program252 .exitOverride()253 .configureOutput({254 outputError255 });256257 expect(() => {258 program.parse(['--unknownOption'], { from: 'user' });259 }).toThrow();260 expect(outputError).toHaveBeenCalledWith("error: unknown option '--unknownOption'\n", program._outputConfiguration.writeErr);261});262263test('when custom outputErr and writeErr and error then outputErr passed writeErr', () => {264 const writeErr = () => jest.fn();265 const outputError = jest.fn();266 const program = new commander.Command();267 program268 .exitOverride()269 .configureOutput({ writeErr, outputError });270271 expect(() => {272 program.parse(['--unknownOption'], { from: 'user' });273 }).toThrow();274 expect(outputError).toHaveBeenCalledWith("error: unknown option '--unknownOption'\n", writeErr);275});