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

API for Mocking Globals with Isolation #524

Open
helloimalastair opened this issue Feb 14, 2025 · 2 comments
Open

API for Mocking Globals with Isolation #524

helloimalastair opened this issue Feb 14, 2025 · 2 comments

Comments

@helloimalastair
Copy link

Hi y'all!

I wanted to ask, what is the stance of the team on providing a way to set mocks on global timing-related APIs that can be locked to a given scope? For example:

import FakeTimers from "@sinonjs/fake-timers";

const timer = FakeTimers.install({ toFake: ['Date'] });

function blockA() {
	return timer.useSystemTime(0, () => `Current timetstamp is ${Date.now()}`);
}

function blockB() {
	return `Current timetstamp is ${Date.now()}`;
}

// Run concurrently, blockA returns timestamp of 0,
// while blockB returns actual timestamp

This is related to vitest#5750

@fatso83
Copy link
Contributor

fatso83 commented Feb 17, 2025

This sounds very interesting indeed, and I did go to checkout the example vitest sandbox. I only saw a "Upgrade Required" in the output, which might explain the failing test 😄 I did catch the gist of the concept though, and I absolutely find the concept of keeping async contexts worth considering.

Are there anyone outside of Node doing something similar to AsyncContext?

It would be fine to have a node-only feature to start out.

@helloimalastair
Copy link
Author

In my case specifically, I'm writing tests for a project built on Workers, but in that case, you still have access to AsyncContext because of the platform's Node Compatibility Mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants