Skip to content

Commit

Permalink
[test] Karma should fail if errors are thrown (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Nov 6, 2020
1 parent b5bd56f commit e3ab096
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
3 changes: 2 additions & 1 deletion test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ module.exports = function setKarmaConfig(config) {
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('test'),
NODE_ENV: '"test"',
CI: JSON.stringify(process.env.CI),
KARMA: 'true',
},
}),
],
Expand Down
52 changes: 50 additions & 2 deletions test/karma.tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
/* eslint-env mocha */
import './utils/init';
import consoleError from './utils/consoleError';
import { createMochaHooks } from '@material-ui/monorepo/test/utils/mochaHooks';

consoleError();
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;
}
});

const packagesContext = require.context('../packages', true, /\.test\.tsx$/);
packagesContext.keys().forEach(packagesContext);
17 changes: 0 additions & 17 deletions test/utils/consoleError.js

This file was deleted.

4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2735,8 +2735,8 @@
resolved "https://github.com/mui-org/material-ui.git#41898e1318b0315e5a61e2df7780b6019f3b44f6"

"@material-ui/monorepo@https://github.com/mui-org/material-ui.git#next":
version "5.0.0-alpha.13"
resolved "https://github.com/mui-org/material-ui.git#13f3e9d8db81b40e1b08f43e4d45e37f3926c37d"
version "5.0.0-alpha.15"
resolved "https://github.com/mui-org/material-ui.git#c500fc3faf873142b9124319cebf728dd9a12cc5"

"@material-ui/styles@^4.10.0":
version "4.10.0"
Expand Down

0 comments on commit e3ab096

Please sign in to comment.