Skip to content

Commit c7bcc1b

Browse files
arturovtdylhunn
authored andcommittedMar 24, 2022
fix(zone.js): check if process is defined when patching the GlobalErrors.install (#45392)
Jasmine checks internally if `process` and `process.on` is defined. Otherwise, it installs the browser rejection handler through the `global.addEventListener`. This code may be run in the browser environment where `process` is not defined, and this will lead to a runtime exception since Webpack 5 removed automatic Node.js polyfills. PR Close #42260 PR Close #45392
1 parent dc72f30 commit c7bcc1b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
 

‎packages/zone.js/lib/jasmine/jasmine.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
6666
if (originalInstall && !instance[symbol('install')]) {
6767
instance[symbol('install')] = originalInstall;
6868
instance.install = function() {
69-
const originalHandlers = process.listeners('unhandledRejection');
70-
const r = originalInstall.apply(this, arguments);
71-
process.removeAllListeners('unhandledRejection');
69+
const isNode = typeof process !== 'undefined' && !!process.on;
70+
// Note: Jasmine checks internally if `process` and `process.on` is defined. Otherwise,
71+
// it installs the browser rejection handler through the `global.addEventListener`.
72+
// This code may be run in the browser environment where `process` is not defined, and
73+
// this will lead to a runtime exception since Webpack 5 removed automatic Node.js
74+
// polyfills. Note, that events are named differently, it's `unhandledRejection` in
75+
// Node.js and `unhandledrejection` in the browser.
76+
const originalHandlers: any[] = isNode ? process.listeners('unhandledRejection') :
77+
global.eventListeners('unhandledrejection');
78+
const result = originalInstall.apply(this, arguments);
79+
isNode ? process.removeAllListeners('unhandledRejection') :
80+
global.removeAllListeners('unhandledrejection');
7281
if (originalHandlers) {
73-
originalHandlers.forEach(h => process.on('unhandledRejection', h));
82+
originalHandlers.forEach(handler => {
83+
if (isNode) {
84+
process.on('unhandledRejection', handler);
85+
} else {
86+
global.addEventListener('unhandledrejection', handler);
87+
}
88+
});
7489
}
75-
return r;
90+
return result;
7691
};
7792
}
7893
return instance;

0 commit comments

Comments
 (0)