Skip to content

Instantly share code, notes, and snippets.

@benoitguigal
Created March 29, 2020 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benoitguigal/87e59042aeb3c775a18b8ba847575e7e to your computer and use it in GitHub Desktop.
Save benoitguigal/87e59042aeb3c775a18b8ba847575e7e to your computer and use it in GitHub Desktop.
jest mock setup
const spyReturns = returnValue => jest.fn(() => returnValue);
describe("scenario", () => {
const setup = (mockOverrides) => {
const mockedFunctions = {
a: spyReturns(true),
b: spyReturns(true),
...mockOverrides
}
return {
mockedModule: jest.doMock('../myModule', () => mockedFunctions)
}
}
it("should return true for module a", () => {
const { mockedModule } = setup();
expect(mockedModule.a()).toEqual(true)
});
it("should return override for module a", () => {
const EXPECTED_VALUE = "override"
const { mockedModule } = setup({ a: spyReturns(EXPECTED_VALUE)});
expect(mockedModule.a()).toEqual(EXPECTED_VALUE)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment