Last active
July 13, 2024 09:36
-
-
Save erikpukinskis/5eae9d675c004dd1ff660a46dcf5ef47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check out the blog post, "How to mock a third party ES6 export in Vitest", at: | |
* https://dev.to/erikpuk/how-to-mock-a-third-party-es6-export-in-vitest-38ff | |
*/ | |
// Import the module: | |
import * as Firestore from "firebase/firestore" | |
import { test, vi } from "vitest" | |
type FakeFirestore = { onSnapshot(this: void): void } | |
// Mock the import: | |
vi.mock("firebase/firestore", async (getModule) => { | |
const original: FakeFirestore = await getModule() | |
return { | |
...original, | |
onSnapshot: vi.fn().mockImplementation(original.onSnapshot), | |
} | |
}) | |
test("calls onSnapshot", () => { | |
// Create your spy: | |
const onSnapshot = vi.spyOn(Firestore, "onSnapshot") | |
/* trigger the onSnapshot call here in my code */ | |
// Place expectations on the spy: | |
expect(onSnapshot).toHaveBeenCalled() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment