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.

Date.now() cannot be faked when using @babel/runtime-corejs*

See original GitHub issue
  • FakeTimers version : 6.0.1
  • Environment : node v10.19.0, macOS v10.15.4
  • Example URL:
  • Other libraries you are using: See below.

What did you expect to happen?

Date.now() returns 0

What actually happens

Date.now() returns current timestamp.

How to reproduce

  1. Configure project to use @babel/runtime-corejs3:

    package.json
    {
      "name": "test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@babel/core": "^7.9.0",
        "@babel/plugin-transform-runtime": "^7.9.0",
        "@babel/preset-env": "^7.9.0",
        "@babel/register": "^7.9.0"
      },
      "dependencies": {
        "@babel/runtime": "^7.9.2",
        "@babel/runtime-corejs3": "^7.9.2",
        "@sinonjs/fake-timers": "^6.0.1"
      }
    }
    
    babel.config.js
    module.exports = {
      "presets": ["@babel/preset-env"],
      "plugins": [
        ["@babel/plugin-transform-runtime", { "corejs": 3 }]
      ]
    }
    
  2. Create index.js as follows:

    import FakeTimers from '@sinonjs/fake-timers'
    
    FakeTimers.install()
    
    console.log(new Date().getTime())
    console.log(Date.now())
    
  3. Execute the script:

    > node -r @babel/register index.js
    0
    1585877900883
    

The last line of output should be 0, but it is the current timestamp instead.

Note: can also be reproduced using (deprecated) @babel/runtime-corejs2.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fatso83commented, Apr 3, 2020

OK, this was easy. Just run babel to inspect the output and it is immediatly clear:

$ npm i @babel/cli 
...

$ $(npm bin)/babel index.js 
"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _now = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/date/now"));

var _fakeTimers = _interopRequireDefault(require("@sinonjs/fake-timers"));

_fakeTimers["default"].install();

console.log(new Date().getTime());
console.log((0, _now["default"])());

You corejs3 is replacing Date.now by a strictly bound reference to @babel/runtime-corejs3/core-js-stable/date/now. We cannot do anything about that, basically, but maybe you can find a way to configure core-js to not do this.

2reactions
fatso83commented, Apr 3, 2020

Screw that. I decided to just run your test case (very easy to do, so great job, btw!) and I had misunderstood what was going on: fake timers ARE being installed, it seems, it’s just that it does not seem to be working for that one call. The other is fine. I’ll inspect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure `@babel/runtime-corejs3` to exclude es.date.now
now polyfill, but I cannot figure out how to apply this configuration. Example. index.js. import FakeTimers from '@sinonjs/fake-timers' ...
Read more >
How to use the @babel/runtime-corejs3/core-js/date/now ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
@babel/runtime | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
babel/plugin-transform-runtime
A plugin that enables the re-use of Babel's injected helper code to save on codesize.
Read more >
vis-network - UNPKG
node_modules/@babel/runtime-corejs3/core-js-stable/date/now.js",". ... Create object with fake `null` prototype: use ActiveX Object with cleared ...
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