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

我们两清 提交于 2019-12-10 04:06:23

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10258248/no-such-module-error-when-trying-to-get-mocha-to-watch-my-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!