question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating to next 8 break Date mocks in tests

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
lfadescommented, Jun 20, 2019

Removing corejs does fix the issue, I think it shouldn’t introduce other issues, at least not in test mode:

{
  "env": {
    "development": {
      "presets": ["next/babel"]
    },
    "production": {
      "presets": ["next/babel"]
    },
    "test": {
      "presets": [
        [
          "next/babel",
          {
            "preset-env": {
              "modules": "commonjs"
            },
            "transform-runtime": {
              "corejs": false
            }
          }
        ]
      ]
    }
  }
}

1reaction
SebastienGllmtcommented, Oct 10, 2019

I ran into this problem also today with latest versions of all related libs.

The bug seems be that Date and global.Date get treated differently.

Date.now = jest.spyOn(global.Date, 'now').mockImplementation(() => 5);
console.log(global.Date.now()); // returns 5 as  expected
console.log(Date.now()); // returns 1570401251960 (mock had no affect)

I changed my babel.config.js to

module.exports = function (api) {
  return {
      // other config omitted
      presets: [
        '@babel/plugin-transform-runtime',
        {
            // CoreJS breaks Jest mocks for some reason
            corejs: api.env('jest') ? false : 2,
            helpers: true,
            regenerator: true
        }
      ]
  }
}

and this seems to fixed it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating to next 8 break Date mocks in tests #7050 - GitHub
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()....
Read more >
How do I set a mock date in Jest? - Stack Overflow
Goal is to mock new Date() with a fixed date wherever it's used during the component rendering for test purposes.
Read more >
When should I (not) use mocks in testing? - DEV Community ‍ ‍
Consider breaking it down into smaller dependency-free pieces and covering them with unit tests. You may then test their integration in the ...
Read more >
Stubs and mocks break encapsulation - ploeh blog
I usually try to avoid the terms Mock and Stub since people use them vaguely and inconsistently. The terms Test Double and Fake...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
It requires changing how you think about your unit tests while removing a lot of boilerplate. In this article, we'll cover multiple mock...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found