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
-
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 }] ] }
-
Create
index.js
as follows:import FakeTimers from '@sinonjs/fake-timers' FakeTimers.install() console.log(new Date().getTime()) console.log(Date.now())
-
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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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/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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK, this was easy. Just run babel to inspect the output and it is immediatly clear:
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.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.