Last active
May 24, 2021 11:11
-
-
Save bengry/2175edcc13ba910f1edc2f331fcec46b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timezonedDate from "timezoned-date"; | |
export interface MockDateSetup { | |
reset(): void; | |
set(options: { offset?: number | keyof typeof offsetsByName; isoDate?: string }): void; | |
} | |
const originalDate = Date; | |
export function setupMockDate(): MockDateSetup { | |
function reset() { | |
Date = originalDate; | |
} | |
function set({ isoDate, offset }: { offset?: number; isoDate?: string }) { | |
const getMockDate = (): typeof import("mockdate") => { | |
let MockDate: typeof import("mockdate") | undefined; | |
jest.isolateModules(() => { | |
MockDate = require("mockdate"); | |
}); | |
return MockDate!; | |
}; | |
if (offset !== undefined) { | |
Date = timezonedDate.makeConstructor(offset); | |
} | |
if (isoDate !== undefined) { | |
getMockDate().set(isoDate); | |
} | |
} | |
return { reset, set }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment