command.parse.test.js
This file
Path
tests/command.parse.test.js
74 lines · 2.5 KiB · JavaScript
Last changed by
Add parse result test (#1188)John Geebefore adoption733047f
History of this fileTake it
Parse argument conventions (#1172)John Geebefore adoption30ae4ac
lines 1–54why, 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() user args', () => {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 // implicit also supports detecting electron but more implementation knowledge required than useful to test1819 test('when args then app/script/args', () => {20 const program = new commander.Command();21 program.parse('node script.js user'.split(' '));22 expect(program.args).toEqual(['user']);23 });2425 test('when args from "node" then app/script/args', () => {26 const program = new commander.Command();27 program.parse('node script.js user'.split(' '), { from: 'node' });28 expect(program.args).toEqual(['user']);29 });3031 test('when args from "electron" and not default app then app/args', () => {32 const program = new commander.Command();33 const hold = process.defaultApp;34 process.defaultApp = undefined;35 program.parse('customApp user'.split(' '), { from: 'electron' });36 process.defaultApp = hold;37 expect(program.args).toEqual(['user']);38 });3940 test('when args from "electron" and default app then app/script/args', () => {41 const program = new commander.Command();42 const hold = process.defaultApp;43 process.defaultApp = true;44 program.parse('electron script user'.split(' '), { from: 'electron' });45 process.defaultApp = hold;46 expect(program.args).toEqual(['user']);47 });4849 test('when args from "user" then args', () => {50 const program = new commander.Command();51 program.parse('user'.split(' '), { from: 'user' });52 expect(program.args).toEqual(['user']);53 });54});Add parse result test (#1188)John Geebefore adoption733047f
lines 55–74why, one step back5556describe('return type', () => {57 test('when call .parse then returns program', () => {58 const program = new commander.Command();59 program60 .action(() => { });6162 const result = program.parse(['node', 'test']);63 expect(result).toBe(program);64 });6566 test('when await .parseAsync then returns program', async() => {67 const program = new commander.Command();68 program69 .action(() => { });7071 const result = await program.parseAsync(['node', 'test']);72 expect(result).toBe(program);73 });74});