Skip to content
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

Sinon FakeTimers are not working with deno, but working in node. #16704

Closed
Lesiuk opened this issue Nov 18, 2022 · 3 comments
Closed

Sinon FakeTimers are not working with deno, but working in node. #16704

Lesiuk opened this issue Nov 18, 2022 · 3 comments
Labels
design limitation something that can't be fixed or is too hard to fix node compat

Comments

@Lesiuk
Copy link

Lesiuk commented Nov 18, 2022

Node.js

import sinon from 'sinon';

const clock = sinon.useFakeTimers();

setTimeout(() => {
    console.log('setTimeout');
}, 1_000_000);

await clock.runAllAsync();

Deno

import sinon from 'npm:sinon';

const clock = sinon.useFakeTimers();

setTimeout(() => {
    console.log('setTimeout');
}, 1_000_000);

await clock.runAllAsync();

Node result:
Displays setTimeout imediately.

Deno result:
Waits for 1 milion miliseconds then displays setTimeout

@dsherret
Copy link
Member

dsherret commented Nov 18, 2022

This one is tricky because the setTimeout global found in the npm packages is different than the setTimeout found in Deno code since Node's setTimeout is not web standard (it returns an object instead of a number). So, the sinon code is not overriding the Deno global setTimeout, but instead only the Node one. I don't think by design there is a solution here to make this work.

In the future, we will probably have the ability to import from node: specifiers, which means to make this work you would need to import setTimeout from Node like so:

// does not work at the moment, but may in the future
import { setTimeout } from "node:timers";
import sinon from 'npm:sinon';

const clock = sinon.useFakeTimers();

setTimeout(() => {
    console.log('setTimeout');
}, 1_000_000);

await clock.runAllAsync();

@dsherret dsherret added design limitation something that can't be fixed or is too hard to fix node compat labels Nov 18, 2022
@Lesiuk
Copy link
Author

Lesiuk commented Nov 18, 2022

Your answer pointed me to the solution.

import sinon from 'npm:sinon';

const clock = sinon.useFakeTimers({
    global: window
});

setTimeout(() => {
    console.log('setTimeout');
}, 10_000_000);

await clock.runAllAsync();

And it works now.
Thank you.

@Lesiuk Lesiuk closed this as completed Nov 18, 2022
@dsherret
Copy link
Member

dsherret commented Nov 18, 2022

Ah, awesome!

Note that you might want to do global: globalThis instead because the window global might be removed in a breaking change in the future (see #13367).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design limitation something that can't be fixed or is too hard to fix node compat
Projects
None yet
Development

No branches or pull requests

2 participants