Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No such module" error when trying to get Mocha to watch my project

I'm trying to get Mocha to watch my project for test and constantly run the tests but when I use the -w flag I get an error.

Here the test executes fine:

C:\Foo>mocha

  .

  ? 1 tests complete (3ms)

and here with -w

C:\Foo>mocha -w


node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: No such module
    at EventEmitter.<anonymous> (node.js:392:27)
    at Object.<anonymous> (C:\Users\Greg\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:203:11)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)

I have Mocha installed globally (npm install -g mocha) and should installed locally to the project.

I'm using node v0.6015, Mocha 1.0.1 and Should 0.6.1 on 64bit Windows 7 home premium.

like image 426
Greg B Avatar asked Apr 21 '12 10:04

Greg B


2 Answers

I was able to make it work on windows by changing a couple of mocha source code files. After npm install mocha (in my case I installed it just for my project, not globally):

1) First go to node_modules\mocha\lib\utils.js find and fix watch function as follows:

exports.watch = function(files, fn) {
    var options = { interval: 100 };
    files.forEach(function(file) {
        debug('file %s', file);
        fs.watch(file, options, function(curr, prev) {
            fn(file);
        });
    });
};

I replaced fs.watchFile with fs.watch (see https://github.com/fgnass/node-dev/issues/26 for details) because the first one doesn't seem to work on windows.

2) Now open node_modules\mocha\bin\_mocha and apply following fixes:

a) Find and comment out or remove following code:

process.on('SIGINT', function(){
  showCursor();
  console.log('\n');
  process.exit();
});

Since there's no equivalent of POSIX signals lines above have to be removed (ideally replaced by proper implementation, see What is the Windows equivalent of process.on('SIGINT') in node.js? for more details)

b) Find following code utils.watch(watchFiles, function(){... and replace it with

  var lastRun = new Date();
  utils.watch(watchFiles, function(){
    if (new Date() - lastRun > 300)
    {
        purge();
        stop()
        mocha.suite = mocha.suite.clone();
        ui = interfaces[program.ui](mocha.suite);
        loadAndRun();
        lastRun = new Date();
    }
  });

It throttles excessive callacks from fs.watch.

c) Last change is removing or commenting out this line:

  process.stdout.write('\r' + str);

in function play(arr, interval). It just removes noise.

like image 65
Artem Govorov Avatar answered Oct 05 '22 03:10

Artem Govorov


Try to install mocha locally into the project you're testing, looks like mocha didn't find required modules to use.

Also I think this should be helpful for you too: Mocha requires make. Can't find a make.exe that works on Windows

like image 45
Eugene Hauptmann Avatar answered Oct 05 '22 02:10

Eugene Hauptmann