Skip to content

Command instance re-using old arguments on subsequent calls in test env #1819

@yamalight

Description

@yamalight

I've encountered an odd behavior where subcommands in test env will re-use optional arguments from previous calls.
At the top level - you have a program and a subcommand with optional flags. Following calls behave oddly:

  1. Call myapp subcommand --init (correctly passes init as argument to handler)
  2. Call myapp subcommand --other 1234 (correctly passes other as argument to handler, but also includes init from first call)
  3. Call myapp subscommand (passes previous values for init and other instead of empty args)

Is this behavior intended? If yes, how would one reset args between tests? The only way I've found so far was to monkey-patch subcommand _optionValues which feels wrong 😅

I've created a small repro case in stackblitz: https://stackblitz.com/edit/node-nsyb1h?file=app.test.js
Calling npm test executes it.
Uncommenting monkey-patching in the last test case makes it behave as expected (i.e. not pass any args when none given to parse function).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions