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

td.replaceEsm() but mocking a global in that module only #507

Closed
rickmed opened this issue Feb 13, 2023 · 2 comments
Closed

td.replaceEsm() but mocking a global in that module only #507

rickmed opened this issue Feb 13, 2023 · 2 comments

Comments

@rickmed
Copy link

rickmed commented Feb 13, 2023

I would want to mock a global variable but only in a selected ESM. Is it possible?
(instead of mocking a module export)

@searls
Copy link
Member

searls commented Feb 13, 2023

I don't think so, since globals are global. If you td.replace(globalThis, 'doodleDart'), you'll be replacing it until you td.reset() for anyone who'll reference it. Rather than get more clever with a mocking tool at this point, I'd encourage you to create a seam (some unit of code that serves as indirection between the thing being tested and the global) that you can safely fake out under strict test control instead.

@searls searls closed this as completed Feb 13, 2023
@rickmed
Copy link
Author

rickmed commented Feb 15, 2023

My use case is mocking something like Date.now().
I could pass it as a dependency in all calls down, yes.
If I don't, I could mock the global. But in concurrent tests it could affect each other.

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