-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
/
setupKarma.js
65 lines (53 loc) · 1.82 KB
/
setupKarma.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* eslint-env mocha */
import { createMochaHooks } from './mochaHooks';
const mochaHooks = createMochaHooks(window.Mocha);
/**
* Emit events from `DispatchingProfiler` as `browser_info` karma events.
*/
function handleReactProfilerResults(event) {
// Only own properties are persisted when sending the object.
const info = { type: event.type, detail: event.detail };
// eslint-disable-next-line no-underscore-dangle -- public: http://karma-runner.github.io/5.2/dev/plugins.html
window.__karma__.info(info);
}
before(function beforeAllHook() {
mochaHooks.beforeAll.forEach((mochaHook) => {
mochaHook.call(this);
});
window.addEventListener('reactProfilerResults', handleReactProfilerResults);
});
after(function afterAllHook() {
window.removeEventListener('reactProfilerResults', handleReactProfilerResults);
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;
}
});