Skip to content

Instantly share code, notes, and snippets.

@spencerfeng
Created December 13, 2023 11:17
Show Gist options
  • Save spencerfeng/e68db6d21f22355d7f55ee443f6399ba to your computer and use it in GitHub Desktop.
Save spencerfeng/e68db6d21f22355d7f55ee443f6399ba to your computer and use it in GitHub Desktop.
For tutorial: Mastering the Art of Mocking ES Modules in Jest
import { jest } from "@jest/globals"
import * as Dependency from "./dependency.js"
import { functionToTest } from "./functionToTest.js"
describe("functionToTest", () => {
afterEach(() => {
jest.restoreAllMocks()
})
it("should return the correct value 1", () => {
const mockedDependencyValue = "the mocked dependency 1"
jest.spyOn(Dependency, "dependency").mockReturnValue(mockedDependencyValue)
expect(functionToTest()).toBe(mockedDependencyValue)
})
it("should return the correct value 2", () => {
const mockedDependencyValue = "the mocked dependency 2"
jest.spyOn(Dependency, "dependency").mockReturnValue(mockedDependencyValue)
expect(functionToTest()).toBe(mockedDependencyValue)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment