New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prohibit faking of faked timers #426
Changes from 1 commit
fb58a72
c83aa3b
797512c
41d52bd
c0e2a0a
3b9250a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -53,6 +53,69 @@ const utilPromisifyAvailable = promisePresent && utilPromisify; | |||||
const timeoutResult = global.setTimeout(NOOP, 0); | ||||||
const addTimerReturnsObject = typeof timeoutResult === "object"; | ||||||
|
||||||
describe("issue #2449: perminent loss of native functions", () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
typo-fix. Also Mocha relies heavily on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Also, I forgot to run |
||||||
it("should not fake faked timers", () => { | ||||||
const currentTime = new Date().getTime(); | ||||||
const date1 = new Date("2015-09-25"); | ||||||
const date2 = new Date("2015-09-26"); | ||||||
let clock = FakeTimers.install({ now: date1 }); | ||||||
assert.same(clock.now, date1.getTime()); | ||||||
assert.same(new Date().getTime(), 1443139200000); | ||||||
assert.exception(function () { | ||||||
FakeTimers.install({ now: date2 }); | ||||||
}); | ||||||
clock.uninstall(); | ||||||
clock = FakeTimers.install({ now: date2 }); | ||||||
assert.same(clock.now, date2.getTime()); | ||||||
clock.uninstall(); | ||||||
assert.greater(new Date().getTime(), currentTime, true); | ||||||
}); | ||||||
|
||||||
it("should not fake faked timers on a custom target", () => { | ||||||
const setTimeoutFake = sinon.fake(); | ||||||
const context = { | ||||||
Date: Date, | ||||||
setTimeout: setTimeoutFake, | ||||||
clearTimeout: sinon.fake(), | ||||||
}; | ||||||
let clock = FakeTimers.withGlobal(context).install(); | ||||||
assert.equals(setTimeoutFake.callCount, 1); | ||||||
clock.setTimeout(NOOP, 0); | ||||||
assert.equals(setTimeoutFake.callCount, 1); | ||||||
assert.exception(function () { | ||||||
clock = FakeTimers.withGlobal(context).install(); | ||||||
}); | ||||||
clock.uninstall(); | ||||||
}); | ||||||
|
||||||
it("should allow a fake on a custom target if the global is faked", () => { | ||||||
const globalClock = FakeTimers.install(); | ||||||
const setTimeoutFake = sinon.fake(); | ||||||
const context = { | ||||||
Date: Date, | ||||||
setTimeout: setTimeoutFake, | ||||||
clearTimeout: sinon.fake(), | ||||||
}; | ||||||
const clock = FakeTimers.withGlobal(context).install(); | ||||||
|
||||||
clock.uninstall(); | ||||||
globalClock.uninstall(); | ||||||
}); | ||||||
|
||||||
it("should allow a fake on the global if a fake on a customer target is already defined", () => { | ||||||
const setTimeoutFake = sinon.fake(); | ||||||
const context = { | ||||||
Date: Date, | ||||||
setTimeout: setTimeoutFake, | ||||||
clearTimeout: sinon.fake(), | ||||||
}; | ||||||
const clock = FakeTimers.withGlobal(context).install(); | ||||||
const globalClock = FakeTimers.install(); | ||||||
|
||||||
globalClock.uninstall(); | ||||||
clock.uninstall(); | ||||||
}); | ||||||
}); | ||||||
describe("issue #59", function () { | ||||||
it("should install and uninstall the clock on a custom target", function () { | ||||||
const setTimeoutFake = sinon.fake(); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not fancy the implicitness of this. It also won't cover a bunch of scenarios where we do not fake
Date
. fake-timers support faking a single timer or many the APIs. See thetoFake
config option: https://github.com/sinonjs/fake-timers#var-clock--faketimersinstallconfigI would much rather be specific in testing for a property on the various timers, along the lines of what Luxon
DateTime.isDatetime(obj)
is doing. This would only mean adding a booleanisFakeTimer
to Date and the timers. And the test would be explicit!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noted that the faked timers can't be used unless
install
is first called, so I tried to put the property on the global only wheninstall
is called. I tried to leave the global object clean afteruninstall
.