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

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

History of this file

Take it

configure output (#1387)John Geebefore adoptioned7f13e

lines 1–109why, 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});109

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 110why, one step back
110test('when default getOutHelpWidth then help helpWidth from stdout', () => {

configure output (#1387)John Geebefore adoptioned7f13e

lines 111–113why, one step back
111 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 back
114 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 115–122why, one step back
115
116 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 back
123 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 124–128why, one step back
124 return '';
125 }126 });127 program.outputHelp();128

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 129why, one step back
129 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 130–133why, one step back
130 process.stdout.columns = holdColumns;
131 process.stdout.isTTY = holdIsTTY;132});133

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 134why, one step back
134test('when custom getOutHelpWidth then help helpWidth custom', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 135why, one step back
135 const expectedColumns = 123;

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 136why, one step back
136 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 137–141why, one step back
137
138 const program = new commander.Command();139 program140 .configureHelp({141 formatHelp: (cmd, helper) => {

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 142why, one step back
142 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 143–145why, one step back
143 return '';
144 }145 }).configureOutput({

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 146why, one step back
146 getOutHelpWidth: () => expectedColumns

configure output (#1387)John Geebefore adoptioned7f13e

lines 147–149why, one step back
147 });
148 program.outputHelp();149

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 150why, one step back
150 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 151–152why, one step back
151});
152

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 153why, one step back
153test('when default getErrHelpWidth then help error helpWidth from stderr', () => {

configure output (#1387)John Geebefore adoptioned7f13e

lines 154–156why, one step back
154 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 back
157 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 158–164why, one step back
158
159 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 back
165 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 166–170why, one step back
166 return '';
167 }168 });169 program.outputHelp({ error: true });170

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 171why, one step back
171 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 172–175why, one step back
172 process.stderr.isTTY = holdIsTTY;
173 process.stderr.columns = holdColumns;174});175

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 176why, one step back
176test('when custom getErrHelpWidth then help error helpWidth custom', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 177why, one step back
177 const expectedColumns = 123;

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 178why, one step back
178 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 179–183why, one step back
179
180 const program = new commander.Command();181 program182 .configureHelp({183 formatHelp: (cmd, helper) => {

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 184why, one step back
184 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 185–187why, one step back
185 return '';
186 }187 }).configureOutput({

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 188why, one step back
188 getErrHelpWidth: () => expectedColumns

configure output (#1387)John Geebefore adoptioned7f13e

lines 189–191why, one step back
189 });
190 program.outputHelp({ error: true });191

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 192why, one step back
192 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 193–194why, one step back
193});
194

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 195why, one step back
195test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 196why, one step back
196 const expectedColumns = 123;

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 197why, one step back
197 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 198–202why, one step back
198
199 const program = new commander.Command();200 program201 .configureHelp({202 formatHelp: (cmd, helper) => {

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 203why, one step back
203 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 204–205why, one step back
204 return '';
205 },

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 206why, one step back
206 helpWidth: expectedColumns

configure output (#1387)John Geebefore adoptioned7f13e

line 207why, one step back
207 }).configureOutput({

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 208why, one step back
208 getOutHelpWidth: () => 999

configure output (#1387)John Geebefore adoptioned7f13e

lines 209–211why, one step back
209 });
210 program.outputHelp();211

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 212why, one step back
212 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 213–214why, one step back
213});
214

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 215why, one step back
215test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 216why, one step back
216 const expectedColumns = 123;

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 217why, one step back
217 let helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 218–222why, one step back
218
219 const program = new commander.Command();220 program221 .configureHelp({222 formatHelp: (cmd, helper) => {

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 223why, one step back
223 helpWidth = helper.helpWidth;

configure output (#1387)John Geebefore adoptioned7f13e

lines 224–225why, one step back
224 return '';
225 },

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 226why, one step back
226 helpWidth: expectedColumns

configure output (#1387)John Geebefore adoptioned7f13e

line 227why, one step back
227 }).configureOutput({

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 228why, one step back
228 getErrHelpWidth: () => 999

configure output (#1387)John Geebefore adoptioned7f13e

lines 229–231why, one step back
229 });
230 program.outputHelp({ error: true });231

Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787

line 232why, one step back
232 expect(helpWidth).toBe(expectedColumns);

configure output (#1387)John Geebefore adoptioned7f13e

lines 233–238why, one step back
233});
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 back
239 getOutHelpWidth: jest.fn(),
240 getErrHelpWidth: jest.fn(),

configure output (#1387)John Geebefore adoptioned7f13e

lines 241–275why, one step back
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});