7

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.

Greg B
  • 14,597
  • 18
  • 87
  • 141

2 Answers2

5

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.

Community
  • 1
  • 1
Artem Govorov
  • 903
  • 6
  • 10
  • That's works fine, thank you for the solution. Do you know how to force it clear terminal after each restart? – WHITECOLOR Jul 12 '12 at 13:53
  • The problem is that it restarts tests when any files are changed. But it does not take in to account changes (except changes in test file) – WHITECOLOR Jul 12 '12 at 14:55
  • Haven't try to clear terminal. Picks up all file changes in my environment, though I have been only testing scripts in lib folder without any sub folders. Try having a look into _mocha, file monitoring is set up there if I'm not mistaken. – Artem Govorov Jul 21 '12 at 11:59
1

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

Community
  • 1
  • 1
Eugene Hauptmann
  • 1,255
  • 12
  • 16