DEV Community

Gergő Móricz
Gergő Móricz

Posted on

How would you write tests for a Node CLI?

I’m writing my new project Candy, and I’m writing a CLI for it.

The main project is tested with mocha and chia, but I’ve never seen anyone test a CLI before, nor any ways to do it.

How would you/did you approach this problem?

Top comments (3)

Collapse
 
orkon profile image
Alex Rudenko

I think this project github.com/sstephenson/bats can come handy! I have never used it myself though. For me, it was always enough to write tests with mocha & chai (i.e. test that arguments are parsed correctly and your internal logic). What exactly do you want to achieve by testing via CLI?

Collapse
 
primariumio profile image
Justin

I think you could probably write another small app if you really need to do testing right at the CLI that know what inputs and outputs to look for. A miniature "postman" for your app. I would submit that you should first make sure everything before the CLI is tested correctly. If you have internal tests for all logic/processing/CRUD in your application there is really very little to test at the CLI. What functionality or behavior are you wanting to test there that can't be tested internally?

Collapse
 
nickytonline profile image
Nick Taylor

I found mock-stdin quite handy.