Mocha and node.exec_child

169 views
Skip to first unread message

Anthony Green

unread,
May 21, 2013, 3:23:32 PM5/21/13
to moc...@googlegroups.com
I'm attempting to test a small node library with mocha:

var util = require('util'),
    exec = require('child_process').exec;

var seleniumLocator = function(command) {
  
  return function(callback) {
      
    var pathToSeleniumServer = './'  
      , pathToMatch =/\/[\w\-\/]+\/selenium-server-standalone-\d+\.\d+\.\d+\.jar/m
      , child;

    
    child = exec(command, function(error, stdout, stderr) {
              
        if (!error) {
          pathToSeleniumServer = (stdout.match(pathToMatch) || [])[0];
          callback(pathToSeleniumServer);
        }

    });
    
  };
  
}

var homebrewLocator = seleniumLocator('brew info selenium-server-standalone');

module.exports.homebrewLocator = homebrewLocator;

when I run the code from command line the callback can be seen to work however my test in mocha fails to identify the callback was invoked.

var should = require('should')
  , sinon = require('sinon') 
  , homebrewLocator = require('./../lib/locators/homebrew_locator').homebrewLocator;  


describe('seleniumLocator', function() {
  
  describe('when using Homebrew', function() {

    it('should calls a callback with the location of selenium', function() {
      
      var callback = sinon.spy();
      
      homebrewLocator(callback);
      callback.called.should.be.true;
      
    });
    
  });
  
});

Is there something about exec and scope I'm not getting?

Anthony Green

unread,
May 21, 2013, 5:12:39 PM5/21/13
to moc...@googlegroups.com
I realise now async code needs async tests

 it('should calls a callback with the location of selenium', function(done) {
      
      var callback = sinon.spy();
      
      homebrewLocator(function() { 
        callback(); 
        callback.called.should.be.true;
        done(); 
      });
Reply all
Reply to author
Forward
0 new messages