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

@std/expect/fn implement 'jest compatible' mock function #6290

Open
anion155 opened this issue Dec 22, 2024 · 1 comment
Open

@std/expect/fn implement 'jest compatible' mock function #6290

anion155 opened this issue Dec 22, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@anion155
Copy link

anion155 commented Dec 22, 2024

Is your feature request related to a problem? Please describe.

We do have 'Jest compatible' expect module, but it's fn implementation isn't really 'Jest compatible'. We don't have any api to support dynamic change of mock function implementation, and it seems like one of the main parts of being 'Jest compatible' in my opinion. And currently any migration of project from node to deno will require manual changes in this aspect.

Describe the solution you'd like

Provide same functionality described for Jest's mock function for @stc/expect's fn, like:

  • mockImplementation() and mockImplementationOnce(), with all of the sugarcoated mockReturnValue() etc
  • withImplementation().

Would be nice to return resource with [Symbol.dispose] from withImplementation() when scope callback wasn't provided.

Multiple implementation args of fn could be sugarcoated version of doing fn().mockImplementationOnce().mockImplementationOnce().

To be backward compatible and implement .mockRestore() we either could use first argument function as default implementation or provide .mockRestore() only argument instead of multiple

Also would be nice to have spyOn implementation compatible with 'Jest' version.

Describe alternatives you've considered

Come with any other better api for dynamically changing implementation

PS

I'm willing to start working on this issue my self in my free time, if that's ok

PS

Referencing my other issue about incompatibility with @std/testing/mock as I think they are closely related

Just wanted to mention issue about @std/expect's fn not being type safe here as it seems related in my opinion

@kt3k
Copy link
Member

kt3k commented Dec 23, 2024

Seems reasonable suggestion to me. PRs are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants