Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nest-commander-testing

jmcdo29205.5kMIT3.4.0TypeScript support: included

A testing utility for nest-commander. It builds on top of ideas from @nestjs/testing and is not tied to any test framework directly.

cli, nestjs, application, command, command-line, nest, decorator, testing

readme

NestJS Commander Testing

So you;'ve built a CLI application, but you want to test it, and you want to be able to do your usual NestJS DI mocking. Well, here's your solution :fireworks:

Installation

Before you get started, you'll need to install a few packages. First and foremost, this one: nest-commander-testing (name pending). You'll also need to install @nestjs/testing as this package makes use of them under the hood, but doesn't want to tie you down to a specific version, yay peerDependencies!

npm i nest-commander-testing @nestjs/testing
# OR
yarn add nest-commander-testing @nestjs/testing
# OR
pnpm i nest-commander-testing @nestjs/testing

Testing With Mocks

So what's the use of writing a super awesome command line script if you can't test it super easily, right? Fortunately, nest-commander has some utilities you can make use of that fits in perfectly with the NestJS ecosystem, it'll feel right at home to any Nestlings out there. Instead of using the CommandFactory for building the command in test mode, you can use CommandTestFactory and pass in your metadata, very similarly to how Test.createTestingModule from @nestjs/testing works. In fact, it uses this package under the hood. You're also still able to chain on the overrideProvider methods before calling compile() so you can swap out DI pieces right in the test. A nice example of this can be seen in the basic.command.factory.spec.ts file.

Testing Inquirer Questions

If you are making use of the InquirerService, you can use CommandTestFactory.setAnswers method and pass either a single answer or multiple answers depending on how many answers you need to mock. In doing so, a mock inquirer service will act similarly to inquirer without needing to modify process.stdin or make use of any user input, allowing smooth testing in your CI pipelines. For an example of this, please check the pizza command integration test.

changelog

nest-commander-testing

3.4.0

Minor Changes

  • ce3d4f4: Support NestJS v11

3.3.0

Minor Changes

  • 519018e: Add ability to set outputConfiguration.

    Now CommandFactory.run(), CommandFactory.runWithoutClosing() and CommandFactory.createWithoutRunning() accept the option outputConfiguration.

3.2.0

Minor Changes

  • 1fa92a0: Support NestJS v10

3.1.0

Minor Changes

  • c75ca13: Add runWithoutClosing for testing

3.0.1

Patch Changes

  • 23b2f48: Add 3.0.0 to peer deps

3.0.0

Major Changes

  • d6ebe0e: Migrate CommandRunner from interface to abstract class and add .command

    This change was made so that devs could access this.command inside the CommandRunner instance and have access to the base command object from commander. This allows for access to the help commands in a programatic fashion.

    To update to this version, any implements CommandRunner should be changed to extends CommandRunner. If there is a constructor to the CommandRunner then it should also use super().

Minor Changes

  • 3d2aa9e: Update NestJS package to version 9

2.0.1

Patch Changes

  • 3831e52: Adds a new @Help() decorator for custom commander help output

    nest-commander-testing now also uses a hex instead of utf-8 encoding when creating a random js file name during the CommandTestFactory command. This is to help create more predictable output names.

2.0.0

Major Changes

  • ee001cc: Upgrade all Nest dependencies to version 8

    WHAT: Upgrade @nestjs/ dependencies to v8 and RxJS to v7 WHY: To support the latest version of Nest HOW: upgrading to Nest v8 should be all that's necessary (along with rxjs to v7)

1.2.0

Minor Changes

  • f3f687b: Allow for commands to be run indefinitely

    There is a new runWithoutClosing method in the CommandFactory class. This command allows for not having the created Nest Application get closed immediately, which should allow for the use of indefinitely runnable commands.