IanVS / vitest-fetch-mock

Vitest mock for fetch, forked from jest-fetch-mock
MIT License
68 stars 9 forks source link

feat: decouple from cross-fetch #24

Closed dirkluijk closed 1 month ago

dirkluijk commented 1 month ago

All right, this PR includes:

dirkluijk commented 1 month ago

@IanVS I changed the workflow a bit, so you will have to approve it before it runs :)

dirkluijk commented 1 month ago

Pushes a new commit, changes:

dirkluijk commented 1 month ago

I notice one final change here that I want to ask about. Previously it was up to the user to provide vi to the createFetchMock function. Now, you're importing it straight from vitest. This makes me slightly nervous because it means we are relying on both this package and the user's code pulling the same version of vitest. We have a peer dependency on vitest, so theoretically it should work fine, but I've struggled more than I like to recall with different versions of packages being used by different dependencies causing very subtle and hard-to-troubleshoot bugs.

Can you talk a little about what you see as the pros & cons of this new approach here vs the explicit dependency injection we currently have? And furthermore, are you maybe willing to move that change to a separate PR (can group together with the auto-enabling if you'd like), so that we can keep this particular PR focused on decoupling from cross-fetch?

The signature change was one of the many changes I initially made. At that point it was more of a simple proof-of-concept, written from scratch, using this library as inspiration. When I started adding more and more back in, I started to consider it as some kind of fork (but still, with many different design decisions). Only later I decided to contribute this as a pull request to this project, and with that approach we have all the reason to be careful. I see no reason to break this interface now. Thatโ€™s why reverted the change. ๐Ÿ˜‰

Is there a difference? Well, that depends.

With the peer dependency approach, there is not really any value over providing the vi instance as argument compared to importing it ourselves. The result is exactly the same - both in runtime behavior and in compiletime type-checking. That's why I removed it initially, but reverting is better to not break the interface. Alternatively, we could also mark it as optional.

dirkluijk commented 1 month ago

Added another small fix that prevented types/test.ts from type-checking before. That led me to making isMocking public again, and adjusting the ErrorOrFunction type-definition a bit.

I can't find any remaining issues anymore. I'm quite confident about this PR.

IanVS commented 1 month ago

whoops, spoke too soon, looks like there's a minor linting issue.

dirkluijk commented 1 month ago

fixed!

IanVS commented 1 month ago

Thanks for contributing this as a PR even though I'm sure it was much more annoying to do than just making a fork or new project.

If you'd like to make more breaking change PRs, for things like auto-enable and relying on peer dependencies, I'd be happy to review/merge those as well before making a 0.4 release (or maybe we just go to 1.0.0 now).

I think I'm on board with the peer dependency, though there are some edge cases that could still bite us, like npm automatically installing a peer dependency when it doesn't find one, and yarn doing weird things in monorepos sometimes. But I think in our case it's probably pretty low risk.

dirkluijk commented 1 month ago

Thanks for contributing this as a PR even though I'm sure it was much more annoying to do than just making a fork or new project.

No worries, it actually reminds me to care about breaking changes, which is absolutely a good thing!

If you'd like to make more breaking change PRs, for things like auto-enable and relying on peer dependencies, I'd be happy to review/merge those as well before making a 0.4 release (or maybe we just go to 1.0.0 now).

My feeling right now is to release this as 0.4, then add new feature(s) in 0.5, and then keep 1.0 for when things are stable and maybe for introducing some breaking changes and/or mark some things as deprecated to clean things up (and remove them for real in 2.0).

If you agree, I'd say, let's release and publish the new version. ๐ŸŽ‰

I think I'm on board with the peer dependency, though there are some edge cases that could still bite us, like npm automatically installing a peer dependency when it doesn't find one, and yarn doing weird things in monorepos sometimes. But I think in our case it's probably pretty low risk.

As far as I can see, nothing changed there. Vitest was already a peer dependency before ๐Ÿ˜‰