Commit 106479d0 authored by Paul Slaughter's avatar Paul Slaughter

Use fake date in spec environment

- This makes sure the fake date is
  truly applied everywhere and
  fixes an issue where jsdom was
  not aware of the fake date.
parent d1834208
// Frida Kahlo's birthday (6 = July)
export const DEFAULT_ARGS = [2020, 6, 6];
const DEFAULT_ARGS = [2020, 6, 6];
const RealDate = Date;
const isMocked = (val) => Boolean(val.mock);
export const createFakeDateClass = (ctorDefault) => {
const createFakeDateClass = (ctorDefault) => {
const FakeDate = new Proxy(RealDate, {
construct: (target, argArray) => {
const ctorArgs = argArray.length ? argArray : ctorDefault;
......@@ -39,11 +39,20 @@ export const createFakeDateClass = (ctorDefault) => {
return FakeDate;
};
export const useFakeDate = (...args) => {
const useFakeDate = (...args) => {
const FakeDate = createFakeDateClass(args.length ? args : DEFAULT_ARGS);
global.Date = FakeDate;
};
export const useRealDate = () => {
const useRealDate = () => {
global.Date = RealDate;
};
// We use commonjs so that the test environment module can pick this up
// eslint-disable-next-line import/no-commonjs
module.exports = {
useFakeDate,
useRealDate,
DEFAULT_ARGS,
createFakeDateClass,
};
......@@ -4,6 +4,7 @@ const path = require('path');
const { ErrorWithStack } = require('jest-util');
const JSDOMEnvironment = require('jest-environment-jsdom');
const { TEST_HOST } = require('./__helpers__/test_constants');
const { useFakeDate } = require('./__helpers__/fake_date');
const ROOT_PATH = path.resolve(__dirname, '../..');
......@@ -12,6 +13,9 @@ class CustomEnvironment extends JSDOMEnvironment {
// Setup testURL so that window.location is setup properly
super({ ...config, testURL: TEST_HOST }, context);
// This lets jsdom use the fake date for things like document.cookie
useFakeDate();
Object.assign(context.console, {
error(...args) {
throw new ErrorWithStack(
......
......@@ -21,8 +21,6 @@ process.on('unhandledRejection', global.promiseRejectionHandler);
setupManualMocks();
useFakeDate();
afterEach(() =>
// give Promises a bit more time so they fail the right test
new Promise(setImmediate).then(() => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment