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) // Frida Kahlo's birthday (6 = July)
export const DEFAULT_ARGS = [2020, 6, 6]; const DEFAULT_ARGS = [2020, 6, 6];
const RealDate = Date; const RealDate = Date;
const isMocked = (val) => Boolean(val.mock); const isMocked = (val) => Boolean(val.mock);
export const createFakeDateClass = (ctorDefault) => { const createFakeDateClass = (ctorDefault) => {
const FakeDate = new Proxy(RealDate, { const FakeDate = new Proxy(RealDate, {
construct: (target, argArray) => { construct: (target, argArray) => {
const ctorArgs = argArray.length ? argArray : ctorDefault; const ctorArgs = argArray.length ? argArray : ctorDefault;
...@@ -39,11 +39,20 @@ export const createFakeDateClass = (ctorDefault) => { ...@@ -39,11 +39,20 @@ export const createFakeDateClass = (ctorDefault) => {
return FakeDate; return FakeDate;
}; };
export const useFakeDate = (...args) => { const useFakeDate = (...args) => {
const FakeDate = createFakeDateClass(args.length ? args : DEFAULT_ARGS); const FakeDate = createFakeDateClass(args.length ? args : DEFAULT_ARGS);
global.Date = FakeDate; global.Date = FakeDate;
}; };
export const useRealDate = () => { const useRealDate = () => {
global.Date = RealDate; 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'); ...@@ -4,6 +4,7 @@ const path = require('path');
const { ErrorWithStack } = require('jest-util'); const { ErrorWithStack } = require('jest-util');
const JSDOMEnvironment = require('jest-environment-jsdom'); const JSDOMEnvironment = require('jest-environment-jsdom');
const { TEST_HOST } = require('./__helpers__/test_constants'); const { TEST_HOST } = require('./__helpers__/test_constants');
const { useFakeDate } = require('./__helpers__/fake_date');
const ROOT_PATH = path.resolve(__dirname, '../..'); const ROOT_PATH = path.resolve(__dirname, '../..');
...@@ -12,6 +13,9 @@ class CustomEnvironment extends JSDOMEnvironment { ...@@ -12,6 +13,9 @@ class CustomEnvironment extends JSDOMEnvironment {
// Setup testURL so that window.location is setup properly // Setup testURL so that window.location is setup properly
super({ ...config, testURL: TEST_HOST }, context); super({ ...config, testURL: TEST_HOST }, context);
// This lets jsdom use the fake date for things like document.cookie
useFakeDate();
Object.assign(context.console, { Object.assign(context.console, {
error(...args) { error(...args) {
throw new ErrorWithStack( throw new ErrorWithStack(
......
...@@ -21,8 +21,6 @@ process.on('unhandledRejection', global.promiseRejectionHandler); ...@@ -21,8 +21,6 @@ process.on('unhandledRejection', global.promiseRejectionHandler);
setupManualMocks(); setupManualMocks();
useFakeDate();
afterEach(() => afterEach(() =>
// give Promises a bit more time so they fail the right test // give Promises a bit more time so they fail the right test
new Promise(setImmediate).then(() => { 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