lundi 30 janvier 2017

Capture stdout and stderr to test Node.js CLI

Using a technique outlined in another answer I was able to write a test for the --help switch:

const expect = require('chai').expect
const exec = require('child_process').exec
const cli = './cli.js'

describe('help', function () {
  var capturedStdout
  var capturedStderr
  // http://ift.tt/1sR6i2m
  // var cmd = cli + " --help 1>&2"
  var cmd = cli + ' --help'

  before(function (done) {
    exec(cmd, function (error, stdout, stderr) {
      if (error) done(error)
      capturedStdout = stdout
      capturedStderr = stderr
      done()
    })
  })

  it('should succeed', () => {
    expect(capturedStderr).be.empty
  })

  it('should have some usage instructions', () => {
    expect(capturedStdout).to.match(/Usage: words \[options] \[pattern]/)
  })

  it('should show a sematic version number', () => {
    // http://ift.tt/2klxmvH
    expect(capturedStdout).to.match(/v\d+\.\d+\.\d+/)
  })

  it('should have some examples', () => {
    expect(capturedStdout).to.match(/Examples:/)
  })
})

There are two problems I'm having:

  1. It's 45 lines long for one switch.
  2. If I add another describe block for a different switch, for example --version, then I get the following error: Error: done() called multiple times

The solution is to move the test into another file.

Is there a better way to do what I want? All I want to do is repeatedly run my executable while testing stdout, stderr, and the exit status.

Aucun commentaire:

Enregistrer un commentaire