Skip to content

Commit

Permalink
[test] Catch error between tests
Browse files Browse the repository at this point in the history
The issue was found in mui/mui-x#543.
I also create a new setupKarma.js file so the X repo can import it directly.
  • Loading branch information
oliviertassinari committed Nov 7, 2020
1 parent 09dfbab commit 323b350
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
29 changes: 1 addition & 28 deletions test/karma.tests.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
/* eslint-env mocha */
import './utils/init';
import { createMochaHooks } from './utils/mochaHooks';

const mochaHooks = createMochaHooks(window.Mocha);

before(function beforeAllHook() {
mochaHooks.beforeAll.forEach((mochaHook) => {
mochaHook.call(this);
});
});

after(function afterAllHook() {
mochaHooks.afterAll.forEach((mochaHook) => {
mochaHook.call(this);
});
});

beforeEach(function beforeEachHook() {
mochaHooks.beforeEach.forEach((mochaHook) => {
mochaHook.call(this);
});
});

afterEach(function afterEachHook() {
mochaHooks.afterEach.forEach((mochaHook) => {
mochaHook.call(this);
});
});
import './utils/setupKarma';

const integrationContext = require.context(
'../packages/material-ui/test/integration',
Expand Down
51 changes: 51 additions & 0 deletions test/utils/setupKarma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-env mocha */
import { createMochaHooks } from './mochaHooks';

const mochaHooks = createMochaHooks(window.Mocha);

before(function beforeAllHook() {
mochaHooks.beforeAll.forEach((mochaHook) => {
mochaHook.call(this);
});
});

after(function afterAllHook() {
mochaHooks.afterAll.forEach((mochaHook) => {
mochaHook.call(this);
});
});

beforeEach(function beforeEachHook() {
mochaHooks.beforeEach.forEach((mochaHook) => {
mochaHook.call(this);
});
});

afterEach(function afterEachHook() {
mochaHooks.afterEach.forEach((mochaHook) => {
mochaHook.call(this);
});
});

// Ensure that uncaught exceptions between tests result in the tests failing.
// This works around an issue with mocha / karma-mocha, see
// https://github.com/karma-runner/karma-mocha/issues/227
let pendingError = null;
let pendingErrorNotice = null;

window.addEventListener('error', (event) => {
pendingError = event.error;
pendingErrorNotice = 'An uncaught exception was thrown between tests';
});

window.addEventListener('unhandledrejection', (event) => {
pendingError = event.reason;
pendingErrorNotice = 'An uncaught promise rejection occurred between tests';
});

afterEach(() => {
if (pendingError) {
console.error(pendingErrorNotice);
throw pendingError;
}
});

0 comments on commit 323b350

Please sign in to comment.