Prevent monkey patching

This doesn't affect globals (describe, it, expect, etc). Those belong to
the user and Jasmine doesn't depend on them.
This commit is contained in:
Steve Gravrock
2026-02-16 11:06:27 -08:00
parent 281c0d1ee8
commit 63ac7da082
23 changed files with 263 additions and 9 deletions
+38
View File
@@ -141,6 +141,28 @@ const getJasmineRequireObj = (function() {
private$.loadedAsBrowserEsm = loadedAsBrowserEsm;
// Prevent monkey patching of existing properties but allow adding new ones.
// jasmine-html.js needs to be able to add to the jasmine namespace.
// jasmine-ajax also installs itself this way.
const writeable = [
'DEFAULT_TIMEOUT_INTERVAL',
'MAX_PRETTY_PRINT_ARRAY_LENGTH',
'MAX_PRETTY_PRINT_CHARS',
'MAX_PRETTY_PRINT_DEPTH'
];
const descriptors = Object.getOwnPropertyDescriptors(j$);
for (const [k, d] of Object.entries(descriptors)) {
if (!writeable.includes(k)) {
Object.defineProperty(j$, k, {
value: d.value,
enumerable: d.enumerable,
configurable: false,
writable: false
});
}
}
return { jasmine: j$, private: private$ };
};
@@ -241,6 +263,7 @@ getJasmineRequireObj().base = function(j$, private$, jasmineGlobal) {
*/
let DEFAULT_TIMEOUT_INTERVAL = 5000;
Object.defineProperty(j$, 'DEFAULT_TIMEOUT_INTERVAL', {
enumerable: true,
get: function() {
return DEFAULT_TIMEOUT_INTERVAL;
},
@@ -2077,6 +2100,8 @@ getJasmineRequireObj().Env = function(j$, private$) {
this.cleanup_ = function() {
uninstallGlobalErrors();
};
Object.freeze(this);
}
function indirectCallerFilename(depth) {
@@ -2087,6 +2112,8 @@ getJasmineRequireObj().Env = function(j$, private$) {
return frames[depth] && frames[depth].file;
}
Object.freeze(Env);
Object.freeze(Env.prototype);
return Env;
};
@@ -3009,6 +3036,8 @@ callbacks to execute _before_ running the next one.
setInterval[IsMockClockTimingFn] = true;
clearInterval[IsMockClockTimingFn] = true;
Object.freeze(this);
return this;
// Advances the Clock's time until the mode changes.
@@ -3162,6 +3191,8 @@ callbacks to execute _before_ running the next one.
};
Clock.IsMockClockTimingFn = IsMockClockTimingFn;
Object.freeze(Clock);
Object.freeze(Clock.prototype);
return Clock;
};
@@ -8038,9 +8069,12 @@ getJasmineRequireObj().ParallelReportDispatcher = function(j$, private$) {
for (const eventName of private$.reporterEvents) {
this[eventName] = dispatcher[eventName].bind(dispatcher);
}
Object.freeze(this);
}
}
Object.freeze(ParallelReportDispatcher.prototype);
return ParallelReportDispatcher;
};
@@ -11698,8 +11732,12 @@ getJasmineRequireObj().Timer = function() {
this.elapsed = function() {
return now() - startTime;
};
Object.freeze(this);
}
Object.freeze(Timer);
Object.freeze(Timer.prototype);
return Timer;
};