command.parse.test.js
This file
Path
tests/command.parse.test.js
164 lines · 5.7 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than ff7658c point at it.
Add date for 8.0.0-0 (#1527)John Geebefore adoptionff7658c
lines 1–74why, one step back1const 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-readonly67describe('.parse() args from', () => {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 });1617 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 });2728 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 });6364 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 });70});7172describe('return type', () => {73 test('when call .parse then returns program', () => {74 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 75why, one step back75 program.action(() => {});Add date for 8.0.0-0 (#1527)John Geebefore adoptionff7658c
lines 76–80why, one step back7677 const result = program.parse(['node', 'test']);78 expect(result).toBe(program);79 });80Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 81why, one step back81 test('when await .parseAsync then returns program', async () => {Add date for 8.0.0-0 (#1527)John Geebefore adoptionff7658c
line 82why, one step back82 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 83why, one step back83 program.action(() => {});Add date for 8.0.0-0 (#1527)John Geebefore adoptionff7658c
lines 84–96why, one step back8485 const result = await program.parseAsync(['node', 'test']);86 expect(result).toBe(program);87 });88});8990// Easy mistake to make when writing unit tests91test('when parse strings instead of array then throw', () => {92 const program = new commander.Command();93 expect(() => {94 program.parse('node', 'test');95 }).toThrow();96});Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 97–132why, one step back9798describe('parse parameter is treated as readonly, per TypeScript declaration', () => {99 test('when parse called then parameter does not change', () => {100 const program = new commander.Command();101 program.option('--debug');102 const original = ['node', '--debug', 'arg'];103 const param = original.slice();104 program.parse(param);105 expect(param).toEqual(original);106 });107108 test('when parse called and parsed args later changed then parameter does not change', () => {109 const program = new commander.Command();110 program.option('--debug');111 const original = ['node', '--debug', 'arg'];112 const param = original.slice();113 program.parse(param);114 program.args.length = 0;115 program.rawArgs.length = 0;116 expect(param).toEqual(original);117 });118119 test('when parse called and param later changed then parsed args do not change', () => {120 const program = new commander.Command();121 program.option('--debug');122 const param = ['node', '--debug', 'arg'];123 program.parse(param);124 const oldArgs = program.args.slice();125 const oldRawArgs = program.rawArgs.slice();126 param.length = 0;127 expect(program.args).toEqual(oldArgs);128 expect(program.rawArgs).toEqual(oldRawArgs);129 });130});131132describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 133why, one step back133 test('when parse called then parameter does not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 134–141why, one step back134 const program = new commander.Command();135 program.option('--debug');136 const original = ['node', '--debug', 'arg'];137 const param = original.slice();138 await program.parseAsync(param);139 expect(param).toEqual(original);140 });141Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 142why, one step back142 test('when parseAsync called and parsed args later changed then parameter does not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 143–152why, one step back143 const program = new commander.Command();144 program.option('--debug');145 const original = ['node', '--debug', 'arg'];146 const param = original.slice();147 await program.parseAsync(param);148 program.args.length = 0;149 program.rawArgs.length = 0;150 expect(param).toEqual(original);151 });152Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 153why, one step back153 test('when parseAsync called and param later changed then parsed args do not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 154–164why, one step back154 const program = new commander.Command();155 program.option('--debug');156 const param = ['node', '--debug', 'arg'];157 await program.parseAsync(param);158 const oldArgs = program.args.slice();159 const oldRawArgs = program.rawArgs.slice();160 param.length = 0;161 expect(program.args).toEqual(oldArgs);162 expect(program.rawArgs).toEqual(oldRawArgs);163 });164});