2017-11-20 8 views
0

와 변수 : 나는 또한 나중에에 필요하기 때문에전달 나는 <code>react-native</code>을 조롱하기 위해 사용하고 모의 객체가 범위 밖 jest.mock

const MyMock = { 
    MockA: { 
     methodA: jest.genMockFn() 
    }, 
    MockB: { 
     ObjectB: { 
      methodA: jest.genMockFn(), 
      methodB: jest.genMockFn(), 
     } 
    } 
}; 


jest.mock('react-native',() => { 
    return MyMock; 
}); 

내가 jest.mock 외부의 객체를 선언하고를 내 테스트 :

The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables. 

문제는 내가 decla이다 :

describe('MyClass',() => { 
    beforeEach(() => { 
     MyMock.MockB.ObjectB.methodA.mockClear(); 
     MyMock.MockB.ObjectB.methodB.mockClear(); 
    }); 
    //some other code 

나는이 오류 MyMock 외부의 jest.mock. 그러나 나는 내가 볼 수있는 한 선택할 수 없다.

따라서 MyMockjest.mock 외부에 유지하면서 코드를 작동하게하려면 어떻게해야합니까?

답변

0

오류 메시지를 완전히 읽지 않았습니다. 내가 예를 mockMyMock에 대한에 MyMock을 변경하는 경우

Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted. 

을 그래서, 그것은 일 : 마지막 줄 (약간 어둡게)에이있다.