Skip to content
Myr

command.parse.test.js

This file

Path

tests/command.parse.test.js

166 lines · 5.7 KiB · JavaScript

Last changed by

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

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–6why, one step back
1const commander = require('../');
23// Testing some Electron conventions but not directly using Electron to avoid overheads.4// https://github.com/electron/electron/issues/4690#issuecomment-2174352225// https://www.electronjs.org/docs/api/process#processdefaultapp-readonly6

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

line 7why, one step back
7describe('.parse() args from', () => {

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

lines 8–16why, one step back
8 test('when no args then use process.argv and app/script/args', () => {
9 const program = new commander.Command();10 const hold = process.argv;11 process.argv = 'node script.js user'.split(' ');12 program.parse();13 process.argv = hold;14 expect(program.args).toEqual(['user']);15 });16

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 17–26why, one step back
17 test('when no args and electron properties and not default app then use process.argv and app/args', () => {
18 const program = new commander.Command();19 const holdArgv = process.argv;20 process.versions.electron = '1.2.3';21 process.argv = 'node user'.split(' ');22 program.parse();23 delete process.versions.electron;24 process.argv = holdArgv;25 expect(program.args).toEqual(['user']);26 });

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

lines 27–62why, one step back
27
28 test('when args then app/script/args', () => {29 const program = new commander.Command();30 program.parse('node script.js user'.split(' '));31 expect(program.args).toEqual(['user']);32 });3334 test('when args from "node" then app/script/args', () => {35 const program = new commander.Command();36 program.parse('node script.js user'.split(' '), { from: 'node' });37 expect(program.args).toEqual(['user']);38 });3940 test('when args from "electron" and not default app then app/args', () => {41 const program = new commander.Command();42 const hold = process.defaultApp;43 process.defaultApp = undefined;44 program.parse('customApp user'.split(' '), { from: 'electron' });45 process.defaultApp = hold;46 expect(program.args).toEqual(['user']);47 });4849 test('when args from "electron" and default app then app/script/args', () => {50 const program = new commander.Command();51 const hold = process.defaultApp;52 process.defaultApp = true;53 program.parse('electron script user'.split(' '), { from: 'electron' });54 process.defaultApp = hold;55 expect(program.args).toEqual(['user']);56 });5758 test('when args from "user" then args', () => {59 const program = new commander.Command();60 program.parse('user'.split(' '), { from: 'user' });61 expect(program.args).toEqual(['user']);62 });

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 63–69why, one step back
63
64 test('when args from "silly" then throw', () => {65 const program = new commander.Command();66 expect(() => {67 program.parse(['node', 'script.js'], { from: 'silly' });68 }).toThrow();69 });

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

lines 70–89why, one step back
70});
7172describe('return type', () => {73 test('when call .parse then returns program', () => {74 const program = new commander.Command();75 program76 .action(() => { });7778 const result = program.parse(['node', 'test']);79 expect(result).toBe(program);80 });8182 test('when await .parseAsync then returns program', async() => {83 const program = new commander.Command();84 program85 .action(() => { });8687 const result = await program.parseAsync(['node', 'test']);88 expect(result).toBe(program);89 });

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 90–97why, one step back
90});
9192// Easy mistake to make when writing unit tests93test('when parse strings instead of array then throw', () => {94 const program = new commander.Command();95 expect(() => {96 program.parse('node', 'test');97 }).toThrow();

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

line 98why, one step back
98});

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 99–166why, one step back
99
100describe('parse parameter is treated as readonly, per TypeScript declaration', () => {101 test('when parse called then parameter does not change', () => {102 const program = new commander.Command();103 program.option('--debug');104 const original = ['node', '--debug', 'arg'];105 const param = original.slice();106 program.parse(param);107 expect(param).toEqual(original);108 });109110 test('when parse called and parsed args later changed then parameter does not change', () => {111 const program = new commander.Command();112 program.option('--debug');113 const original = ['node', '--debug', 'arg'];114 const param = original.slice();115 program.parse(param);116 program.args.length = 0;117 program.rawArgs.length = 0;118 expect(param).toEqual(original);119 });120121 test('when parse called and param later changed then parsed args do not change', () => {122 const program = new commander.Command();123 program.option('--debug');124 const param = ['node', '--debug', 'arg'];125 program.parse(param);126 const oldArgs = program.args.slice();127 const oldRawArgs = program.rawArgs.slice();128 param.length = 0;129 expect(program.args).toEqual(oldArgs);130 expect(program.rawArgs).toEqual(oldRawArgs);131 });132});133134describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {135 test('when parse called then parameter does not change', async() => {136 const program = new commander.Command();137 program.option('--debug');138 const original = ['node', '--debug', 'arg'];139 const param = original.slice();140 await program.parseAsync(param);141 expect(param).toEqual(original);142 });143144 test('when parseAsync called and parsed args later changed then parameter does not change', async() => {145 const program = new commander.Command();146 program.option('--debug');147 const original = ['node', '--debug', 'arg'];148 const param = original.slice();149 await program.parseAsync(param);150 program.args.length = 0;151 program.rawArgs.length = 0;152 expect(param).toEqual(original);153 });154155 test('when parseAsync called and param later changed then parsed args do not change', async() => {156 const program = new commander.Command();157 program.option('--debug');158 const param = ['node', '--debug', 'arg'];159 await program.parseAsync(param);160 const oldArgs = program.args.slice();161 const oldRawArgs = program.rawArgs.slice();162 param.length = 0;163 expect(program.args).toEqual(oldArgs);164 expect(program.rawArgs).toEqual(oldRawArgs);165 });166});