简体   繁体   中英

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://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
  // 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://rubular.com/r/lTC1wu95jq
    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.

not to answer your entire question but you are missing a return statement

before(function (done) {
    exec(cmd, function (error, stdout, stderr) {
      if (error) { 
        return done(error)
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM