command.parse.test.js
This file
Path
tests/command.parse.test.js
98 lines · 3.2 KiB · JavaScript
Last changed by
Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
History of this fileTake it
Parse argument conventions (#1172)John Geebefore adoption30ae4ac
lines 1–6why, 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-readonly6Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
line 7why, one step back7describe('.parse() args from', () => {Parse argument conventions (#1172)John Geebefore adoption30ae4ac
lines 8–16why, one step back8 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 });16Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 17–26why, one step back17 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 });Parse argument conventions (#1172)John Geebefore adoption30ae4ac
lines 27–62why, one step back2728 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 back6364 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 });Parse argument conventions (#1172)John Geebefore adoption30ae4ac
line 70why, one step back70});Add parse result test (#1188)John Geebefore adoption733047f
lines 71–89why, one step back7172describe('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 back90});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();Add parse result test (#1188)John Geebefore adoption733047f
line 98why, one step back98});