Open kevmul opened 2 years ago
I agree that it would be nice to support compatible testing-libraries as much as possible, but in this case this could be a bit too much.
The only jest-function we actually use is jest.fn()
, which looks like it has a compatible replacement with vi.fn()
in vitest.
If those are actually compatible in the long term (idk if it is supposed to stay that way?) we could use an alias instead that uses either jest.fn
or vi.fn
based on what is globally available.
Alternatively, it should be possible to inject a simple window.jest = window.vi
into the vitest-environment, which should also do the trick.
Easy to fix:
import { vi } from "vitest";
globalThis.jest = vi;
... = await import("@googlemaps/jest-mocks");
(Do not use package @anshulsanghi/googlemaps-vitest-mocks it's a poor solution: no need to fork @googlemaps/jest-mocks
for 2 simple lines of code + it doesn't seem to be maintained anymore)
This is what ended up working for us:
setupTests.ts
(top-level)vi.stubGlobal('jest', vi)
At that point you can actually just proceed to import and use the module as advertised in the original docs
Ie
import { initialize } from "@googlemaps/jest-mocks";
beforeEach(() => {
initialize();
});
My team and I have used your
Jest Mocks
when we started doing unit tests withJest
. But we are switching over to useVite
andVitest
which has very similar apis. But we cannot usejest
andvitest
together.Describe the solution you'd like It would be nice to have a package like this targeting the
vitest
testing library (I think just changing overjest.whatever
tovi.whater
might be enough?)Describe alternatives you've considered I thought about forking this repo for Vitest, but didn't want to maintain such a thing.
Additional context N/A