googlemaps / js-jest-mocks

Jest mocks for Google Maps Platform
Apache License 2.0
34 stars 30 forks source link

Vitest testing #294

Open kevmul opened 2 years ago

kevmul commented 2 years ago

My team and I have used your Jest Mocks when we started doing unit tests with Jest. But we are switching over to use Vite and Vitest which has very similar apis. But we cannot use jest and vitest 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 over jest.whatever to vi.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

usefulthink commented 1 year 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.

tkrotoff commented 1 year ago

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)

usefulthink commented 10 months ago
Reply to a now-deleted comment not sure what `setup-env` is... Is that a file you specified to vitest via the setupFiles option? Also you can `@ts-ignore` those type-errors (or provide the file as js), we know they aren't 100% compatible, but it should be enough for our use-case. According to the docs here: https://vitest.dev/config/#setupfiles, it should also be `globalThis` instead of `global`. Beisdes that, I never used vitest, so I can't really tell...

robbienohra commented 2 months ago

This is what ended up working for us:

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();
});