tbremer
tbremer

Reputation: 693

Node CLI Unit Testing

I have a node module I am working on, and I want to write unit tests for it, however I am confused on how I would pass arguments (required for the CLI) to node through a testing suite.

Lets assume (for brevity) the module name is J, so I would call it like...

$ j --file test.js --file test2.js

how do i recreate those --file calls when I am writing my testing suite?

Upvotes: 3

Views: 1981

Answers (1)

Keenan Lidral-Porter
Keenan Lidral-Porter

Reputation: 1636

You can use node's child process module to run additional command line processes. This link can give you more info on syntax; I recommend also checking out the promised version of child-process.

var spawn = require('child_process').spawn;

spawn('j', ['--file', 'test.js', '--file', 'test2.js'])
  .progress(function(childProcess){
    // any logic you want to do here while process is running
  })
  .then(function(result){
    // command was executed
    // write tests here
  })
  .fail(function(err){
    // maybe 1 last test to make sure there was no test
  });

As far as unit-testing suites, I expect anything your comfortable with would work (mocha/chai etc.)

Upvotes: 2

Related Questions