728x90
반응형
Mocking Module
모듈에 대해 Mocking 하는 것
API 호출 등의 모듈을 실제로 동작하지 않고 테스트를 한다
jset.mock(...) 함수를 사용하여 모듈을 모의 테스트 할 수 있다
모듈을 모킹하면 테스트에서 반환 값을 지정할 수 있다
모의 응답을 반환하는 것
test
import axios from 'axios'; // test 할 모듈
class Users {
static all() {
return axios.get('/users.json').then(resp => resp.data);
}
}
export default Users;
import axios from 'axios';
import Users from './users';
jest.mock('axios');
test('should fetch users', () => {
const users = [{name: 'Bob'}];
const resp = {data: users};
axios.get.mockResolvedValue(resp); // 모의 응답
// or you could use the following depending on your use case:
// axios.get.mockImplementation(() => Promise.resolve(resp))
return Users.all().then(data => expect(data).toEqual(users));
});
반응형
댓글