-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (44 loc) · 1.64 KB
/
index.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
/**
* Mocha helper to convert unhandled rejections into unhandled exceptions.
* Workaround for https://github.com/mochajs/mocha/issues/2640
* Based on https://github.com/mochajs/mocha/issues/1926#issuecomment-180842722
*
* @copyright Copyright 2016-2020 Kevin Locke <[email protected]>
* @license MIT
* @module mocha-ur2ue
*/
'use strict';
// Allow using console, process, and window global variables, when present
/* global console: false, process:false, window:false */
// Allow logging to console to notify user that they may miss rejections.
/* eslint-disable no-console */
exports.mochaHooks =
function mochaHooks() {
if (typeof process !== 'undefined') {
process.on('unhandledRejection', (reason) => {
throw reason;
});
} else if (typeof globalThis !== 'undefined') {
// Note: This event may be emitted natively or by promise libraries
// (e.g. bluebird and when.js)
if (typeof window.addEventListener === 'function') {
globalThis.addEventListener('unhandledrejection', (evt) => {
throw evt.detail.reason;
});
} else {
const oldOHR = globalThis.onunhandledrejection;
globalThis.onunhandledrejection = function(evt, ...args) {
if (typeof oldOHR === 'function') { oldOHR.apply(this, args); }
throw evt.detail.reason;
};
}
} else if (typeof console !== 'undefined'
&& typeof (console.error || console.log) === 'function') {
(console.error || console.log)(
'mocha-ur2ue Warning: Unable to listen for unhandledrejection event.'
+ ' Unhandled rejections will be ignored.',
);
}
// No need to run beforeAll/beforeEach.
return {};
};