Skip to content

Instantly share code, notes, and snippets.

@thisismydesign
Last active September 6, 2023 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thisismydesign/bc6b198e273d629647658f9721bf7cf7 to your computer and use it in GitHub Desktop.
Save thisismydesign/bc6b198e273d629647658f9721bf7cf7 to your computer and use it in GitHub Desktop.
Mocking per test case with Jest /1
jest.mock("MyOtherComponent", () => {
return require
.requireActual("TestUtils")
.mockOriginalFunctionality("MyOtherComponent");
});
describe("<Component />", () => {
it("works", () => {
// MyOtherComponent works as usual
}):
describe("when something silly happens", () => {
beforeEach(() => {
MyOtherComponent.someFunction.mockImplementationOnce(() => {
throw new Error("oops");
});
});
it("displays error", () => {
// MyOtherComponent.someFunction throws
}):
});
it("still works", () => {
// MyOtherComponent works as usual again
}):
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment