Created
December 13, 2023 11:17
-
-
Save spencerfeng/e68db6d21f22355d7f55ee443f6399ba to your computer and use it in GitHub Desktop.
For tutorial: Mastering the Art of Mocking ES Modules in Jest
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
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