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: Align fn function signature with Jest's fn type #6247

Open
PutziSan opened this issue Dec 9, 2024 · 1 comment
Open

@std/expect: Align fn function signature with Jest's fn type #6247

PutziSan opened this issue Dec 9, 2024 · 1 comment

Comments

@PutziSan
Copy link

PutziSan commented Dec 9, 2024

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

The current fn signature in the Deno std library is:

function fn(...stubs: Function[]): Function

This lacks type safety and flexibility compared to Jest's fn:

fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;

See https://github.com/jestjs/jest/blob/main/packages/jest-mock/src/index.ts#L1082

It makes mocking less intuitive and harder to work with in a type-safe way.

Describe the solution you'd like

Update the fn function signature to match Jest's API for better type inference and usability:

fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;

Describe alternatives you've considered

  • Keeping the current implementation: This is simpler but sacrifices type safety and consistency with Jest.
  • Writing custom wrappers: This adds unnecessary complexity and redundancy for users.

I would be happy to contribute this change myself and submit a pull request to update the implementation and associated documentation.

@andrewthauer
Copy link
Contributor

andrewthauer commented Dec 9, 2024

I fully support this. The current implementation of typing fn to Function requires constant casting and also goes against official TS recommendation to avoid using Function as a type in any interfaces.

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