2017-12-14 37 views
0

Jest를 처음 사용 했으므로 함수를 호출했는지 테스트하기 위해 Jest를 사용하려고합니다. 나는 mock.calls.length가 모든 테스트를 위해 리셋하지 않고 누적되는 것을 알아 챘다. 모든 검사 전에 어떻게 0으로 만들 수 있습니까? 나는 나의 다음 시험이 이전의 결과에 의존하기를 원하지 않는다.Jest mock 함수를 재설정하는 방법 모든 테스트 전에 호출 횟수가 계산됩니다.

나는 이전에 조취가 있다는 것을 알고있다 - 나는 그것을 사용해야합니까? mock.calls.length를 재설정하는 가장 좋은 방법은 무엇입니까? 고맙습니다.

코드 예제 :

Sum.js : 각 테스트 후 모의 기능을 취소 : 나는 그것을 처리하는 것으로

import local from 'api/local'; 

export default { 
    addNumbers(a, b) { 
    if (a + b <= 10) { 
     local.getData(); 
    } 
    return a + b; 
    }, 
}; 

Sum.test.js

import sum from 'api/sum'; 
import local from 'api/local'; 
jest.mock('api/local'); 

// For current implementation, there is a difference 
// if I put test 1 before test 2. I want it to be no difference 

// test 1 
test('should not to call local if sum is more than 10',() => { 
    expect(sum.addNumbers(5, 10)).toBe(15); 
    expect(local.getData.mock.calls.length).toBe(0); 
}); 

// test 2 
test('should call local if sum <= 10',() => { 
    expect(sum.addNumbers(1, 4)).toBe(5); 
    expect(local.getData.mock.calls.length).toBe(1); 
}); 

답변

0

한 가지 방법 :

Sum.test.js에 추가하려면 :

afterEach(() => { 
    local.getData.mockClear(); 
});