0

I'm trying to run a test that waits for a promise to be returned, but jasmine is exiting before completing the test, and I can't seem to figure out why.

My tests is

describe("npm packages",function(){
it("should get npm packages", function(){
     //remove node passport directory so we can check it gets added
    var flag = false;
    exec_child_process('rimraf node_modules/passport', function(err){
        if(err!==null){
            console.log('problem removing passport directory');
        } else {
            console.log('passport directory removed');
            npmInstallPackagePromise();
        } 
    });

    function npmInstallPackagePromise(){
        mm.installNodeModules()
        .then(function(){
           fs.readdir('node_modules/passport',function(err){
               if(err!==null){
                    console.log('error reading node module: '+err);   
               } else {
                    flag=true;
               }
            });
        });
    }

    waitsFor(function(){
        return flag;
    },'resolved promise',6000);

    runs(function(){
       expect(flag).toBe(true); 
    });


});

});

and the code the test is running is

module.exports = function(config){
    return {
        getInstalledNodeModules: function(){
            var defer = q.defer();
            exec('npm ls --json', function(err,stdout,stderr){
                console.log(err);
                if(err) return defer.reject(console.log(err)); 
                console.log(stdout);
                defer.resolve(stdout);
            });
           return defer.promise;
        },
        installNodeModules: function(){
            //go through each node module
            this.getInstalledNodeModules().then(function(modules_json){
                console.log(modules_json);
                for(m in config.node_modules){
                    //check if the module is a local module already
                    console.log(modules_json[m]);
                };
            })
        }

I know some people say "just use a mock, don't actually check that the files are written", but because I'm running npm via the exec function, I actually want to check that that is running correctly. So please don't offer that as a solution.

pedalpete
  • 21,076
  • 45
  • 128
  • 239
  • Really. Get a mock of NPM. Actually, e.g. `getInstalledNodeModules` does not return anything. Are you sure, it is problem with the test, not the code? – kirilloid Mar 05 '14 at 05:35
  • You are right @kirilloid, I didn't have a return statement from `getInstalledNodeModules`. I updated that, but still no improvement. A mock of npm does not tell me that `exec('npm...` is doing what I need it to. I'm trying to run npm in a script rather than from the command line, so that is the actual functionality I am trying to test for. I don't like mocks in those situations where I'm relying on something where I'm not completely sure it is going to work. – pedalpete Mar 05 '14 at 06:47

0 Answers0