Test command line on nodejs


Test command line on Node.js.

$ npm install coffee -g

Coffee is useful for test command line in test frammework (like Mocha).

describe('cat', function() {
  it('should concat input', function(done) {
    var coffee = require('coffee');
    .expect('stdout', '12')
    .expect('code', 0)

You can also use fork for spawning Node processes.

coffee.fork('/path/to/file.js', ['args '])
.expect('stdout', '12\n')
.expect('stderr', '34\n')
.expect('code', 0)

In file.js


Coffee will detect istanbul automatically, and generate coverage-{processId}.json, you should generate reporter by istanbul report.

$ rm -rf coverage
$ istanbul cover --report none --print none node_modules/mocha/bin/_mocha -- -R spec -t 5000
$ istanbul report text-summary json lcov
=============================== Coverage summary ===============================
State## ments   : 98.2% ( 109/111 )
Branches     : 97.37% ( 37/38 )
Functions    : 100% ( 20/20 )
Lines        : 98.18% ( 108/110 )

Run command using child_process.spawn, then return Coffee instance.

Arguments see child_process.spawn

Run command using child_process.fork, then return Coffee instance.

Arguments see child_process.fork

Assertion object

Assert type with expected value, expected value can be string, regular expression, and array.

coffee.spawn('echo', ['abcdefg'])
.expect('stdout', 'abcdefg')
.expect('stdout', /^abc/)
.expect('stdout', ['abcdefg', /abc/])

Accept type: stdout, stderr, code, error

Write data to stdin, see example above.

Callback will be called after completing the assertion, the first argument is Error if throw exception.

Write data to process.stdout and process.stderr for debug

Copyright (c) 2015 popomore. Licensed under the MIT license.