Testing child_process.exec stdout

后端 未结 2 1734
南旧
南旧 2021-01-11 18:54

I\'m trying to test the output of child process with mocha.

My test looks like this:

var should = require(\"should\"),
    exec = require(\"child_pro         


        
2条回答
  •  甜味超标
    2021-01-11 19:48

    Years later, mocha has matured and supports testing asynchronous code.

    Simply add a callback (usually named done) to it() and Mocha will know that it should wait for this function to be called to complete the test.

    const should = require("should"),
        { exec } = require("child_process");
    
    describe('users', function() {
      describe('andrei', function() {
        it('should be part of group dev', function(done) {
          exec('id andrei', function(error, stdout, stderr) {
            stdout.should.containEql('dev');
            done();
          })
        })
      })
    })
    

提交回复
热议问题