简体   繁体   中英

Mocha does not rerun my tests after changes

Using a test environment with

  • chokidar (to trigger test files on changes with a watcher chokidar variable),
  • node.js (to output tests result in browser),
  • mocha+chai (test suite),
  • using a custom reporter (as described in Using Mocha programmatically )

As the test file is changed, watcher catch the event, and should rerun the test.

Problem : My test is executed only once !

When trying to run my test for a second time (when the file has changed on disk the second time), mocha do not reprocess the test as in the first time. I suspect that I should reset some internal runner properties, but looking in the mocha github repository, I do not seem to find the correct method to call. Note that my reporter "class" only received 1 event from mocha runner.on("end", ...) on the second watcher wakup.

watcher.on('change', function(path, stats) {
   // ...
   console.log('-----File ' + path + ' has been changed ' + "[" + stats.size + "b]" );
   Mocha             = require('mocha');
   Mocha.prototype.printFiles = function(){
     console.log("Existing files = ");
     console.log( this.files );
     return this;
   };   
   var mocha = new Mocha({
    bail: false
   });
   mocha.reporter("/dirs/MyReporter.js");
   mocha.addFile("/dirs/my-test.js");
   mocha.run(function(failures){
   });
   // ...
});

How can I ask (method call) Mocha to reset its assumptions of my precedent run to rerun my test as the first time ?

//添加 mocha -w 到你的测试脚本 "scripts": { "test": "mocha -w" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM