Skip to content

Commit

Permalink
Workaround: process unhandled exceptions in tests
Browse files Browse the repository at this point in the history
Karma-mocha has an issue[1]: when unhandled exception is thrown right
after a test ends, the mocha doesn't executu other tests, but Karma
reports success. With the fix, the exception is rethrown correctly
after all tests and Karma fails (as expected).

[1] karma-runner/karma-mocha#227

Change-Id: Iae031a01ead3a9004b0c290559f0b070c58a6919
  • Loading branch information
gdmfilippov committed Sep 25, 2020
1 parent 84a30c4 commit 91b3507
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions polygerrit-ui/app/test/common-test-setup-karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ import 'chai/chai.js';
self.assert = window.chai.assert;
self.expect = window.chai.expect;

// Workaround for https://github.com/karma-runner/karma-mocha/issues/227
let unhandledError = null;

window.addEventListener('error', e => {
// For uncaught error mochajs doesn't print the full stack trace.
// We should print it ourselves.
console.error('Uncaught error:');
console.error(e.error.stack.toString());
unhandledError = e;
});

let originalOnBeforeUnload;
Expand All @@ -50,6 +55,9 @@ suiteSetup(() => {
suiteTeardown(() => {
// This suiteTeardown() method is called only once after all tests
window.onbeforeunload = originalOnBeforeUnload;
if (unhandledError) {
throw unhandledError;
}
});

// Tests can use fake timers (sandbox.useFakeTimers)
Expand Down

0 comments on commit 91b3507

Please sign in to comment.