Prevent mock clock timing fns from being spied on

Fixes #826
This commit is contained in:
Steve Gravrock
2025-09-25 20:56:19 -07:00
parent 979e4a5d0f
commit 190a13ed96
5 changed files with 73 additions and 0 deletions
+35
View File
@@ -408,6 +408,41 @@ describe('Clock', function() {
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
});
it('identifies its timing functions', function() {
const fakeSetTimeout = jasmine.createSpy('global setTimeout');
const fakeGlobal = { setTimeout: fakeSetTimeout };
const delayedFunctionScheduler = jasmine.createSpyObj(
'delayedFunctionScheduler',
['scheduleFunction']
);
const mockDate = {
install: function() {},
tick: function() {},
uninstall: function() {}
};
const clock = new jasmineUnderTest.Clock(
fakeGlobal,
function() {
return delayedFunctionScheduler;
},
mockDate
);
clock.install();
expect(
fakeGlobal.setTimeout[jasmineUnderTest.Clock.IsMockClockTimingFn]
).toEqual(true);
expect(
fakeGlobal.clearTimeout[jasmineUnderTest.Clock.IsMockClockTimingFn]
).toEqual(true);
expect(
fakeGlobal.setInterval[jasmineUnderTest.Clock.IsMockClockTimingFn]
).toEqual(true);
expect(
fakeGlobal.clearInterval[jasmineUnderTest.Clock.IsMockClockTimingFn]
).toEqual(true);
});
describe('setTimeout', function() {
it('schedules the delayed function with the fake timer', function() {
const fakeSetTimeout = jasmine.createSpy('setTimeout'),
+12
View File
@@ -135,6 +135,18 @@ describe('SpyRegistry', function() {
target.spiedFunc();
expect(originalFunctionWasCalled).toBe(false);
});
it('throws if the method is a mock clock method', function() {
const spyRegistry = new jasmineUnderTest.SpyRegistry({
createSpy: createSpy
});
const target = { spiedFunc: function() {} };
target.spiedFunc[jasmineUnderTest.Clock.IsMockClockTimingFn] = true;
expect(function() {
spyRegistry.spyOn(target, 'spiedFunc');
}).toThrowError("Mock clock timing functions can't be spied on");
});
});
describe('#spyOnProperty', function() {