0

I have written a script using commander. When testing this script for logical throw that cause errors I cannot seem to trap the commander error that gets thrown

Test Logic

    expect.assertions(1)

    try {
      const program = createProgram()
      program.exitOverride()
      await program.parse(['npx', 'bomci', 'artefact-save', 'some-file', 'some-folder', 'some-location' ])
    } catch(err) {
      expect(true).toBe(true)
      // put assertions here
    }

I would expect the error to be caught in the catch statement but instead I get:

CommanderError: bomci artefact-save expects the environment variable {ARTIFACTORY_USER} to be set.
    at Command._exit (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:449:26)
    at Command.error (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1608:10)
    at Command.<anonymous> (/home/mbell/projects/bom-pipeline-utils/src/index.ts:25:21)
    at Command.listener [as _actionHandler] (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:482:17)
    at /home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1300:65
    at Command._chainOrCall (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1197:12)
    at Command._parseCommand (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1300:27)
    at /home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1081:27
    at Command._chainOrCall (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1197:12)
    at Command._dispatchSubcommand (/home/mbell/projects/bom-pipeline-utils/node_modules/commander/lib/command.js:1077:23) {
  code: 'commander.error',
  exitCode: 9,
  nestedError: undefined
}

The line triggering the error is:

      let artiCreds
      try {
        artiCreds = getArtifactoryCredentials('artefact-save')
      } catch (err) {
        program.error(err.message, { exitCode: 9 })
      }

So why is my error not getting caught and how can I trap this error for my testing?

1
  • What are createProgram and getArtifactoryCredentials? Your code is incomplete. Commented Aug 17, 2023 at 2:10

1 Answer 1

1

After a lot of digging around I found that for commander if your action is asynchronous you need to parseAsync instead of parse

The following test now works correctly for me:

    const program = createProgram()
    program.exitOverride()
    expect(() => program.parseAsync(['npx', 'bomci', 'artefact-save', 'some-file', 'some-folder', 'some-location' ]))
      .rejects.toEqual(new CommanderError(9 , '', 'bomci artefact-save expects the environment variable {ARTIFACTORY_USER} to be set.'))
Sign up to request clarification or add additional context in comments.

1 Comment

Note also: if your program has subcommands, the inherited settings like for exitOverride are inherited as the subcommands are added to the program. So calling program.exitOverride() on a program after the subcommands are added only affects the root command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.