From 016cdebfaca9350abcf2fdb0a4440bbd5aa12348 Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Tue, 11 Jun 2019 13:10:33 -0700 Subject: [PATCH 1/7] Include stack in browser uncaught error reporting --- browser-entry.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser-entry.js b/browser-entry.js index 3e9cbbaf90..feab8b76ff 100644 --- a/browser-entry.js +++ b/browser-entry.js @@ -58,8 +58,8 @@ process.removeListener = function(e, fn) { process.on = function(e, fn) { if (e === 'uncaughtException') { - global.onerror = function(err, url, line) { - fn(new Error(err + ' (' + url + ':' + line + ')')); + global.onerror = function(msg, url, line, col, err) { + fn(err || new Error(msg + ' (' + url + ':' + line + ')')); return !mocha.options.allowUncaught; }; uncaughtExceptionHandlers.push(fn); From 76804e643e26ff9346482ec6d7ee99efff6ec7f3 Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Wed, 12 Jun 2019 09:42:13 -0700 Subject: [PATCH 2/7] Attempt at testing (failing due to uncaught exception) --- karma.conf.js | 6 ++++- .../uncaught-exception.spec.mjs | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/browser-specific/uncaught-exception.spec.mjs diff --git a/karma.conf.js b/karma.conf.js index b0c713856c..a5e8d15973 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -145,7 +145,11 @@ module.exports = config => { pattern: 'test/browser-specific/fixtures/esm.fixture.mjs', type: 'module' }, - {pattern: 'test/browser-specific/esm.spec.mjs', type: 'module'} + {pattern: 'test/browser-specific/esm.spec.mjs', type: 'module'}, + { + pattern: 'test/browser-specific/uncaught-exception.spec.mjs', + type: 'module' + } ]; break; default: diff --git a/test/browser-specific/uncaught-exception.spec.mjs b/test/browser-specific/uncaught-exception.spec.mjs new file mode 100644 index 0000000000..54b852f875 --- /dev/null +++ b/test/browser-specific/uncaught-exception.spec.mjs @@ -0,0 +1,27 @@ +import '/base/mocha.js'; + +var Mocha = window.Mocha; +var Suite = Mocha.Suite; +var Runner = Mocha.Runner; + +mocha.allowUncaught() + +it('should include the stack of uncaught exceptions', function(done) { + var suite = new Suite('Suite', 'root'); + var runner = new Runner(suite); + runner.allowUncaught = true; + var err; + runner.fail = function(e) { + err = e; + }; + + setTimeout(function throwTestError() { + throw new Error('test error'); + }, 1); + + setTimeout(function() { + expect(err, 'to be an', Error); + expect(err.stack, 'to contain', 'at throwTestError') + done(); + }, 2); +}); From 5edce1949c13b1fa79dd9675fcbac13e1f4963dc Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Wed, 12 Jun 2019 12:05:37 -0700 Subject: [PATCH 3/7] Revert "Attempt at testing (failing due to uncaught exception)" This reverts commit 30c7ffb8005937707ee894623d28b054eb621fd8. --- karma.conf.js | 6 +---- .../uncaught-exception.spec.mjs | 27 ------------------- 2 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 test/browser-specific/uncaught-exception.spec.mjs diff --git a/karma.conf.js b/karma.conf.js index a5e8d15973..b0c713856c 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -145,11 +145,7 @@ module.exports = config => { pattern: 'test/browser-specific/fixtures/esm.fixture.mjs', type: 'module' }, - {pattern: 'test/browser-specific/esm.spec.mjs', type: 'module'}, - { - pattern: 'test/browser-specific/uncaught-exception.spec.mjs', - type: 'module' - } + {pattern: 'test/browser-specific/esm.spec.mjs', type: 'module'} ]; break; default: diff --git a/test/browser-specific/uncaught-exception.spec.mjs b/test/browser-specific/uncaught-exception.spec.mjs deleted file mode 100644 index 54b852f875..0000000000 --- a/test/browser-specific/uncaught-exception.spec.mjs +++ /dev/null @@ -1,27 +0,0 @@ -import '/base/mocha.js'; - -var Mocha = window.Mocha; -var Suite = Mocha.Suite; -var Runner = Mocha.Runner; - -mocha.allowUncaught() - -it('should include the stack of uncaught exceptions', function(done) { - var suite = new Suite('Suite', 'root'); - var runner = new Runner(suite); - runner.allowUncaught = true; - var err; - runner.fail = function(e) { - err = e; - }; - - setTimeout(function throwTestError() { - throw new Error('test error'); - }, 1); - - setTimeout(function() { - expect(err, 'to be an', Error); - expect(err.stack, 'to contain', 'at throwTestError') - done(); - }, 2); -}); From 56e2232bc43aaa9a9feaea5921fae860db0d3b3a Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Wed, 12 Jun 2019 12:06:47 -0700 Subject: [PATCH 4/7] implemented test in throw.spec.js & got it passing --- test/unit/throw.spec.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/unit/throw.spec.js b/test/unit/throw.spec.js index 2dc3c8a759..f7d14c070b 100644 --- a/test/unit/throw.spec.js +++ b/test/unit/throw.spec.js @@ -2,6 +2,7 @@ /* eslint no-throw-literal: off */ +var sinon = require('sinon'); var Mocha = require('../../lib/mocha'); var Suite = Mocha.Suite; var Test = Mocha.Test; @@ -13,11 +14,13 @@ var STATE_FAILED = Runnable.constants.STATE_FAILED; describe('a test that throws', function() { var suite; var runner; + var sandbox; var uncaughtHandlers; beforeEach(function() { suite = new Suite('Suite', 'root'); runner = new Runner(suite); + sandbox = sinon.createSandbox(); // see https://github.com/mochajs/mocha/pull/2983#issuecomment-350428522 uncaughtHandlers = process.listeners('uncaughtException') || []; @@ -169,4 +172,35 @@ describe('a test that throws', function() { runner.run(); }); }); + + describe('stack', function() { + it('should include the stack when throwing async', function(done) { + var test = new Test('im async and throw null async', function(done2) { + process.nextTick(function throwError() { + throw new Error('test error'); + }); + }); + suite.addTest(test); + runner = new Runner(suite); + sandbox.stub(runner, 'fail'); + + runner.on(EVENT_RUN_END, function() { + try { + expect(runner.fail, 'to have all calls satisfying', [ + expect.it('to be a', Runnable), + expect.it('to be an', Error).and('to satisfy', { + message: /test error/i, + stack: /throwError/i, + uncaught: true + }) + ]).and('was called once'); + } catch (err) { + return done(err); + } + + done(); + }); + runner.run(); + }); + }); }); From adc04cb3cd57c02bc0bd3dbeddbeb1f5e8ff785a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Wed, 28 Feb 2024 16:57:04 -0500 Subject: [PATCH 5/7] Apply suggestions from code review --- browser-entry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser-entry.js b/browser-entry.js index aaff5f456a..4e6f9e939a 100644 --- a/browser-entry.js +++ b/browser-entry.js @@ -72,7 +72,7 @@ process.listenerCount = function (name) { process.on = function (e, fn) { if (e === 'uncaughtException') { global.onerror = function (msg, url, line, col, err) { - fn(err || new Error(msg + ' (' + url + ':' + line + ')')); + fn(err || new Error(msg + ' (' + url + ':' + line + ':' + col + ')')); return !mocha.options.allowUncaught; }; uncaughtExceptionHandlers.push(fn); From d6b4ce3b01e398248510ed395de78a8eecf282e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Sat, 22 Jun 2024 19:05:12 -0400 Subject: [PATCH 6/7] Update test/unit/throw.spec.js Co-authored-by: Pelle Wessman --- test/unit/throw.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/unit/throw.spec.js b/test/unit/throw.spec.js index 9fb11de6ea..d293fbacc2 100644 --- a/test/unit/throw.spec.js +++ b/test/unit/throw.spec.js @@ -14,13 +14,11 @@ var STATE_FAILED = Runnable.constants.STATE_FAILED; describe('a test that throws', function () { var suite; var runner; - var sandbox; var uncaughtHandlers; beforeEach(function () { suite = new Suite('Suite', 'root'); runner = new Runner(suite); - sandbox = sinon.createSandbox(); // see https://github.com/mochajs/mocha/pull/2983#issuecomment-350428522 uncaughtHandlers = process.listeners('uncaughtException') || []; From e1aa60d697d4e711e8d57db1360a289015f45add Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 22 Jun 2024 19:09:07 -0400 Subject: [PATCH 7/7] test: fix up throw.spec.js --- test/unit/throw.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/throw.spec.js b/test/unit/throw.spec.js index d293fbacc2..cafadaa4bc 100644 --- a/test/unit/throw.spec.js +++ b/test/unit/throw.spec.js @@ -30,6 +30,7 @@ describe('a test that throws', function () { uncaughtHandlers.forEach(function (listener) { process.on('uncaughtException', listener); }); + sinon.restore(); }); describe('non-extensible', function () { @@ -183,7 +184,7 @@ describe('a test that throws', function () { }); suite.addTest(test); runner = new Runner(suite); - sandbox.stub(runner, 'fail'); + sinon.stub(runner, 'fail'); runner.on(EVENT_RUN_END, function() { try {