time-fast-forward
Version:
Fake the system time (Date, hrtime) in your tests, without freezing it (unlike in many other libraries). Freeze only when you need.
24 lines (23 loc) • 774 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetTime = exports.jumpToTime = exports.shiftTimeBy = void 0;
const FakeDate_1 = require("./FakeDate");
exports.shiftTimeBy = (milliseconds) => {
FakeDate_1.fakeDate();
FakeDate_1.setTimeShift(FakeDate_1.getCurrentTimeShift() + milliseconds);
};
exports.jumpToTime = (value) => {
if (typeof value.getTime === 'function') {
value = value.getTime();
}
else if (typeof value === 'string') {
value = new Date(value).getTime();
}
FakeDate_1.fakeDate();
const jumpTo = value;
const now = Date.now();
FakeDate_1.setTimeShift(jumpTo - now + FakeDate_1.getCurrentTimeShift());
};
exports.resetTime = () => {
FakeDate_1.restoreDate();
};