New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fake timers don't work with vue (sinon 7.5.0, @vue/cli 3.12.0) #2155
Comments
Of course, Sinon works fine. It's your setup that doesn't. Now I took 20 minutes to dig into this for you, as I assume there are plenty of people with some combination of JSDOM and Webpack issues that will stumble upon this. Your setup has a lot of "magic", using a lot of implicit dependencies. The first thing I was thinking about was that you probably had some kind of DOM-thing going on that would shadow the original Date function in some way or mess up what environment Sinon thinks it's running in. I was right and I found out like this, by just printing out some values from your test. I replaced the contents of your test with the following:
The important takeaway are the last two lines, which prints
OK, so I know there are two We also know that since there is a So the key take away here is that you want to target a different context. This is slightly more exotic than the usual test case, but you already accepted voodoism when mixing Babel, Webpack and JSDOM, so why not add some extra spice 🔥 Lolex, which is the library Sinon is using, have had support for this for ages, but it's not mentioned in the Sinon docs other than "Please visit the lolex.install documentation for the full feature set.". If you did visit the lolex.install docs you would see that you can "hijack timers in another context" by passing the const clock = sinon.useFakeTimers({ target: global }); Eh, voila! The test passed. Actually, while researching this, I found that @LouisBrunner actually added some extra code in #1935 to address an issue (#1852) which is a near duplicate. The problem is that it actually adds code to achieve something that was already possible. It's know possible to use both |
Describe the bug
sinon.useFakeTimers does not affect new Date() when used with vue-cli webpack setup.
To Reproduce
npm run test:unit
first commit is clean vue cli setup, second is my minimal reproducer.
Expected behavior
Test passes - no matter how much real time passes,
new Date().getTime()
always returns the same valueScreenshots
n/a
Context (please complete the following information):
Additional context
vue cli version itself is outdated in this setup, but I also briefly tried with newest vue cli, with the same results.
Depending on the machine it might be needed to tweak number of iterations in my reproducer.
The text was updated successfully, but these errors were encountered: