I have a mock object that I am using to mock react-native:
const MyMock = {
MockA: {
methodA: jest.genMockFn()
},
MockB: {
ObjectB: {
methodA: jest.genMockFn(),
methodB: jest.genMockFn(),
}
}
};
jest.mock('react-native', () => {
return MyMock;
});
I am declaring the object outside of jest.mock because I also need it later on in my tests:
describe('MyClass', () => {
beforeEach(() => {
MyMock.MockB.ObjectB.methodA.mockClear();
MyMock.MockB.ObjectB.methodB.mockClear();
});
//some other code
I get this error:
The module factory of
jest.mock()is not allowed to reference any out-of-scope variables.
The problem is that I declare MyMock outside of jest.mock. But I have no choice as far as I can see.
So how can I make the code work while keeping MyMock outside of jest.mock?