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

Vitest testing #294

Open
kevmul opened this issue Jul 29, 2022 · 4 comments
Open

Vitest testing #294

kevmul opened this issue Jul 29, 2022 · 4 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@kevmul
Copy link

kevmul commented Jul 29, 2022

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

@kevmul kevmul added triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Jul 29, 2022
@usefulthink usefulthink added priority: p3 Desirable enhancement or fix. May not be included in next release. and removed triage me I really want to be triaged. labels Sep 29, 2023
@usefulthink
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

usefulthink commented Dec 14, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

5 participants