Skip to content

Updating to next 8 break Date mocks in tests #7050

@ssylvia

Description

@ssylvia

Bug report

Describe the bug

Upgrading nextJS from 7.0.2 to the latest version of next 8.0.4 break tests that rely on mocking Date.now(). These tests are written in files that do not import nextjs

To Reproduce

Clone sample repo: https://github.com/ssylvia/next-test-date-mock

NextJS Update Breaks Date Mocks

Steps to reproduce:

  1. yarn install
  2. Run yarn test --no-cache.
  3. Notice that all test pass
  4. Upgrade to nextjs 8
  5. Run yarn test --no-cache.
  6. See failing tests because Date mock no longer works

Test that fails

Source method (src/utils/index.ts)

export const getCurrentTime = () => {
  const time = Date.now();
  return time;
};

Test (src/utils/index.ts)

test("getCurrentTime", () => {
  jest.spyOn(Date, "now").mockImplementation(() => 0);
  expect(getCurrentTime()).toBe(0);
});

Expected behavior

Date.now() should continue to be mocked

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: MacOS
  • Version of Next.js: 8.0.4
  • Node: v10.13.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions