Skip to content

Commit 67a8124

Browse files
JoshuaKGoldbergprustvoxpelli
authored
fix: include stack in browser uncaught error reporting (#5107)
* Include stack in browser uncaught error reporting * Attempt at testing (failing due to uncaught exception) * Revert "Attempt at testing (failing due to uncaught exception)" This reverts commit 30c7ffb. * implemented test in throw.spec.js & got it passing * Apply suggestions from code review * Update test/unit/throw.spec.js Co-authored-by: Pelle Wessman <[email protected]> * test: fix up throw.spec.js --------- Co-authored-by: Peter Rust <[email protected]> Co-authored-by: Pelle Wessman <[email protected]>
1 parent e030115 commit 67a8124

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

browser-entry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ process.listenerCount = function (name) {
7171

7272
process.on = function (e, fn) {
7373
if (e === 'uncaughtException') {
74-
global.onerror = function (err, url, line) {
75-
fn(new Error(err + ' (' + url + ':' + line + ')'));
74+
global.onerror = function (msg, url, line, col, err) {
75+
fn(err || new Error(msg + ' (' + url + ':' + line + ':' + col + ')'));
7676
return !mocha.options.allowUncaught;
7777
};
7878
uncaughtExceptionHandlers.push(fn);

test/unit/throw.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/* eslint no-throw-literal: off */
44

5+
var sinon = require('sinon');
56
var Mocha = require('../../lib/mocha');
67
var Suite = Mocha.Suite;
78
var Test = Mocha.Test;
@@ -29,6 +30,7 @@ describe('a test that throws', function () {
2930
uncaughtHandlers.forEach(function (listener) {
3031
process.on('uncaughtException', listener);
3132
});
33+
sinon.restore();
3234
});
3335

3436
describe('non-extensible', function () {
@@ -172,4 +174,35 @@ describe('a test that throws', function () {
172174
runner.run();
173175
});
174176
});
177+
178+
describe('stack', function() {
179+
it('should include the stack when throwing async', function(done) {
180+
var test = new Test('im async and throw null async', function(done2) {
181+
process.nextTick(function throwError() {
182+
throw new Error('test error');
183+
});
184+
});
185+
suite.addTest(test);
186+
runner = new Runner(suite);
187+
sinon.stub(runner, 'fail');
188+
189+
runner.on(EVENT_RUN_END, function() {
190+
try {
191+
expect(runner.fail, 'to have all calls satisfying', [
192+
expect.it('to be a', Runnable),
193+
expect.it('to be an', Error).and('to satisfy', {
194+
message: /test error/i,
195+
stack: /throwError/i,
196+
uncaught: true
197+
})
198+
]).and('was called once');
199+
} catch (err) {
200+
return done(err);
201+
}
202+
203+
done();
204+
});
205+
runner.run();
206+
});
207+
});
175208
});

0 commit comments

Comments
 (0)