From 17398993aa55dbac6c824de7ff038482ef16fb97 Mon Sep 17 00:00:00 2001 From: juergba Date: Sun, 2 Feb 2020 19:08:27 +0100 Subject: [PATCH 1/2] browser: fix --allow-uncaught --- browser-entry.js | 2 +- lib/runner.js | 3 ++- test/unit/runner.spec.js | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/browser-entry.js b/browser-entry.js index 3f770fac49..3e9cbbaf90 100644 --- a/browser-entry.js +++ b/browser-entry.js @@ -60,7 +60,7 @@ process.on = function(e, fn) { if (e === 'uncaughtException') { global.onerror = function(err, url, line) { fn(new Error(err + ' (' + url + ':' + line + ')')); - return !mocha.allowUncaught; + return !mocha.options.allowUncaught; }; uncaughtExceptionHandlers.push(fn); } diff --git a/lib/runner.js b/lib/runner.js index ceb1a24a4f..f96b020320 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -800,7 +800,8 @@ Runner.prototype.uncaught = function(err) { if (err instanceof Pending) { return; } - if (this.allowUncaught) { + // browser does not exit script when throwing in global.onerror() + if (this.allowUncaught && !process.browser) { throw err; } diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index 92b41c5fc4..cc65453b31 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -709,6 +709,8 @@ describe('Runner', function() { describe('when allow-uncaught is set to true', function() { it('should propagate error and throw', function() { + if (process.browser) this.skip(); + var err = new Error('should rethrow err'); runner.allowUncaught = true; expect( From 0bb3e5ff1d1792763685c48ce8821338a559d22d Mon Sep 17 00:00:00 2001 From: juergba Date: Sun, 2 Feb 2020 19:23:48 +0100 Subject: [PATCH 2/2] update docs --- docs/index.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/docs/index.md b/docs/index.md index ef3f462f1f..cf0f226259 100644 --- a/docs/index.md +++ b/docs/index.md @@ -24,7 +24,6 @@ Mocha is a feature-rich JavaScript test framework running on [Node.js][] and in - [javascript API for running tests](#more-information) - proper exit status for CI support etc - [auto-detects and disables coloring for non-ttys](#reporters) -- [maps uncaught exceptions to the correct test case](#browser-specific-methods) - [async test timeout support](#delayed-root-suite) - [test retry support](#retry-tests) - [test-specific timeouts](#test-level) @@ -666,7 +665,7 @@ describe('outer', function() { }); ``` -> _Updated in v7.0.0. Skipping a test within an "after all" hook is disallowed and will throw an exception. Use a return statement or other means to abort hook execution._ +> _Updated in v7.0.0:_ skipping a test within an "after all" hook is disallowed and will throw an exception. Use a return statement or other means to abort hook execution. Before Mocha v3.0.0, `this.skip()` was not supported in asynchronous tests and hooks. @@ -1543,12 +1542,6 @@ Alias: `HTML`, `html` Mocha runs in the browser. Every release of Mocha will have new builds of `./mocha.js` and `./mocha.css` for use in the browser. -### Browser-specific methods - -The following method(s) _only_ function in a browser context: - -`mocha.allowUncaught()` : If called, uncaught errors will not be absorbed by the error handler. - A typical setup might look something like the following, where we call `mocha.setup('bdd')` to use the **BDD** interface before loading the test scripts, running them `onload` with `mocha.run()`. ```html @@ -1600,6 +1593,7 @@ mocha.setup({ // Examples of options: mocha.setup({ + allowUncaught: true, asyncOnly: true, bail: true, checkLeaks: true,