Created
March 29, 2020 09:39
-
-
Save benoitguigal/87e59042aeb3c775a18b8ba847575e7e to your computer and use it in GitHub Desktop.
jest mock setup
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
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