command.configureOutput.test.js
This file
Path
tests/command.configureOutput.test.js
275 lines · 7.7 KiB · JavaScript
Last changed by
Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
History of this fileTake it
configure output (#1387)John Geebefore adoptioned7f13e
lines 1–109why, one step back1const 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});109Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 110why, one step back110test('when default getOutHelpWidth then help helpWidth from stdout', () => {configure output (#1387)John Geebefore adoptioned7f13e
lines 111–113why, one step back111 const expectedColumns = 123;112 const holdIsTTY = process.stdout.isTTY;113 const holdColumns = process.stdout.columns;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 114why, one step back114 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 115–122why, one step back115116 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) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 123why, one step back123 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 124–128why, one step back124 return '';125 }126 });127 program.outputHelp();128Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 129why, one step back129 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 130–133why, one step back130 process.stdout.columns = holdColumns;131 process.stdout.isTTY = holdIsTTY;132});133Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 134why, one step back134test('when custom getOutHelpWidth then help helpWidth custom', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 135why, one step back135 const expectedColumns = 123;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 136why, one step back136 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 137–141why, one step back137138 const program = new commander.Command();139 program140 .configureHelp({141 formatHelp: (cmd, helper) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 142why, one step back142 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 143–145why, one step back143 return '';144 }145 }).configureOutput({Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 146why, one step back146 getOutHelpWidth: () => expectedColumnsconfigure output (#1387)John Geebefore adoptioned7f13e
lines 147–149why, one step back147 });148 program.outputHelp();149Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 150why, one step back150 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 151–152why, one step back151});152Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 153why, one step back153test('when default getErrHelpWidth then help error helpWidth from stderr', () => {configure output (#1387)John Geebefore adoptioned7f13e
lines 154–156why, one step back154 const expectedColumns = 123;155 const holdIsTTY = process.stderr.isTTY;156 const holdColumns = process.stderr.columns;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 157why, one step back157 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 158–164why, one step back158159 process.stderr.isTTY = true;160 process.stderr.columns = expectedColumns;161 const program = new commander.Command();162 program163 .configureHelp({164 formatHelp: (cmd, helper) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 165why, one step back165 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 166–170why, one step back166 return '';167 }168 });169 program.outputHelp({ error: true });170Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 171why, one step back171 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 172–175why, one step back172 process.stderr.isTTY = holdIsTTY;173 process.stderr.columns = holdColumns;174});175Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 176why, one step back176test('when custom getErrHelpWidth then help error helpWidth custom', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 177why, one step back177 const expectedColumns = 123;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 178why, one step back178 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 179–183why, one step back179180 const program = new commander.Command();181 program182 .configureHelp({183 formatHelp: (cmd, helper) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 184why, one step back184 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 185–187why, one step back185 return '';186 }187 }).configureOutput({Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 188why, one step back188 getErrHelpWidth: () => expectedColumnsconfigure output (#1387)John Geebefore adoptioned7f13e
lines 189–191why, one step back189 });190 program.outputHelp({ error: true });191Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 192why, one step back192 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 193–194why, one step back193});194Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 195why, one step back195test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 196why, one step back196 const expectedColumns = 123;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 197why, one step back197 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 198–202why, one step back198199 const program = new commander.Command();200 program201 .configureHelp({202 formatHelp: (cmd, helper) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 203why, one step back203 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 204–205why, one step back204 return '';205 },Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 206why, one step back206 helpWidth: expectedColumnsconfigure output (#1387)John Geebefore adoptioned7f13e
line 207why, one step back207 }).configureOutput({Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 208why, one step back208 getOutHelpWidth: () => 999configure output (#1387)John Geebefore adoptioned7f13e
lines 209–211why, one step back209 });210 program.outputHelp();211Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 212why, one step back212 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 213–214why, one step back213});214Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 215why, one step back215test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 216why, one step back216 const expectedColumns = 123;Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 217why, one step back217 let helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 218–222why, one step back218219 const program = new commander.Command();220 program221 .configureHelp({222 formatHelp: (cmd, helper) => {Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 223why, one step back223 helpWidth = helper.helpWidth;configure output (#1387)John Geebefore adoptioned7f13e
lines 224–225why, one step back224 return '';225 },Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 226why, one step back226 helpWidth: expectedColumnsconfigure output (#1387)John Geebefore adoptioned7f13e
line 227why, one step back227 }).configureOutput({Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 228why, one step back228 getErrHelpWidth: () => 999configure output (#1387)John Geebefore adoptioned7f13e
lines 229–231why, one step back229 });230 program.outputHelp({ error: true });231Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 232why, one step back232 expect(helpWidth).toBe(expectedColumns);configure output (#1387)John Geebefore adoptioned7f13e
lines 233–238why, one step back233});234235test('when set configureOutput then get configureOutput', () => {236 const outputOptions = {237 writeOut: jest.fn(),238 writeErr: jest.fn(),Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
lines 239–240why, one step back239 getOutHelpWidth: jest.fn(),240 getErrHelpWidth: jest.fn(),configure output (#1387)John Geebefore adoptioned7f13e
lines 241–275why, one step back241 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});