mocha: 🐛 Bug: Mocha throws error if it is running a spec more than once

taken from https://cdnjs.com/libraries/mocha

> mocha.run();

everything ok!

> mocha.run()

TypeError: Cannot read property 'call' of undefined
    at r (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.min.js:2:8563)
    at r.run (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.min.js:2:9635)
    at n (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.min.js:2:13371)
    at https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.min.js:2:13710
    at i (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.min.js:1:572)

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 21 (3 by maintainers)

Most upvoted comments

This worked for me, if anyone is still looking for a solution

const purge = function (allFiles) {
  allFiles.forEach(file => {
    delete require.cache[file];
  });
};
// use this function when rerunning tests
const rerun = function () {
  purge(allFiles);
  mocha.suite = mocha.suite.clone();
  mocha.suite.ctx = new Mocha.Context();
  mocha.ui("bdd");
  mocha.files = allFiles;
  mocha.run()
};

I have same problem. I just tried to set up mocha programmatically and have just one test is expect(1).toBe(1) . No fancy stuff, like react or enzyme is loaded.

I load one test file mocha.add('my.test.js')

then i run the tests.

mocha.run();
setTimeout(()=>{ mocha.run(); }, 5000);

First time it runs fine, after 5s it throws me an error. This is the full error stack TypeError: Cannot read property 'call' of undefined at callFn ([__dirname]\node_modules\mocha\lib\runnable.js:348:20) at Test.Runnable.run ([__dirname]\node_modules\mocha\lib\runnable.js:340:7) at Runner.runTest ([__dirname]\node_modules\mocha\lib\runner.js:443:10) at [__dirname]\node_modules\mocha\lib\runner.js:549:12 at next ([__dirname]\node_modules\mocha\lib\runner.js:361:14) at [__dirname]\node_modules\mocha\lib\runner.js:371:7 at next ([__dirname]\node_modules\mocha\lib\runner.js:295:14) at Immediate.<anonymous> ([__dirname]\node_modules\mocha\lib\runner.js:339:5) at runCallback (timers.js:800:20) at tryOnImmediate (timers.js:762:5)