Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Fixes maximum call stack error

Tingan Ho1a68cc2f102d
diff --git a/lib/runner.js b/lib/runner.js
index f6a8fc24..a7c65cad 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -71,7 +71,8 @@ function Runner(suite, delay) {
this.on('hook end', function(hook) {
self.checkGlobals(hook);
});
- this.grep(/.*/);
+ this._defaultGrep = /.*/;
+ this.grep(this._defaultGrep);
this.globals(this.globalProps().concat(extraGlobals()));
}
@@ -470,7 +471,20 @@ Runner.prototype.runTests = function(suite, fn) {
match = !match;
}
if (!match) {
- return next();
+ // Run immediately only if we have defined a grep. When we
+ // define a grep — It can cause maximum callstack error if
+ // the grep is doing a large recursive loop by neglecting
+ // all tests. The run immediately function also comes with
+ // a performance cost. So we don't want to run immediately
+ // if we run the whole test suite, because running the whole
+ // test suite don't do any immediate recursive loops. Thus,
+ // allowing a JS runtime to breathe.
+ if (self._grep !== self._defaultGrep) {
+ Runner.immediately(next);
+ } else {
+ next();
+ }
+ return;
}
// pending
@@ -563,7 +577,17 @@ Runner.prototype.runSuite = function(suite, fn) {
if (!curr) {
return done();
}
- self.runSuite(curr, next);
+
+ // Avoid grep neglecting large number of tests causing a
+ // huge recursive loop and thus a maximum call stack error.
+ // See comment in `this.runTests()` for more information.
+ if (self._grep !== self._defaultGrep) {
+ Runner.immediately(function() {
+ self.runSuite(curr, next);
+ });
+ } else {
+ self.runSuite(curr, next);
+ }
}
function done(errSuite) {