Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikpukinskis/5eae9d675c004dd1ff660a46dcf5ef47 to your computer and use it in GitHub Desktop.
Save erikpukinskis/5eae9d675c004dd1ff660a46dcf5ef47 to your computer and use it in GitHub Desktop.
/**
* 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