I'm trying to mock an object to test some implementation using jest which can be overridden for each testcase. Please let me know if you have solved similar problem or any other better solution to mock an object using jest.
import {obj} from "./filepath/obj";
jest.mock("./filepath/obj", () => ({
obj:{
search: jest.fn(),
items:[1,2,3]
}
}))
test("test 1", () => {
expect(obj.items.length).toBe(3); // works
})
// now if I try to override, for some other test case that doesn't work
test("test 1", () => {
jest.mock("./filepath/obj", () => ({
obj:{
search: jest.fn(),
items:[]
}
}))
expect(obj.items.length).toBe(0); // doesn't work
})
How can we override the mock implementation for an object which can work in each test case?
more context: the object is a mobx store object exported from the file where we have the store definition. Then, we're using that store object in a component by importing it. Now, while testing the component, we're trying to mock the store object as mentioned above.