mock 함수 예제
function check(predicate, onSuccess, onFail) {
if (predicate()) {
onSuccess('yes');
} else {
onFail('no');
}
}
module.exports = check;
- predicate함수가 true면 onSuccess함수 실행
- predicate함수가 false면 onFail함수 실행
const check = require('../check');
describe('check', () => {
let onSuccess;
let onFail;
beforeEach(() => {
onSuccess = jest.fn();
onFail = jest.fn();
});
it('should call onSuccess when predicate is true', () => {
check(() => true, onSuccess, onFail);
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onSuccess).toHaveBeenCalledWith('yes');
expect(onFail).toHaveBeenCalledTimes(0);
});
it('should call onFail when predicate is false', () => {
check(() => false, onSuccess, onFail);
expect(onFail).toHaveBeenCalledTimes(1);
expect(onFail).toHaveBeenCalledWith('no');
expect(onSuccess).toHaveBeenCalledTimes(0);
});
});
- onSuccess함수와 onFail함수는 jest.fn() mock으로 구성(임의의 함수가 존재한다고 가정)
- check의 첫번째 인자가 true일 때, onSuccess는 1번 실행 되어야 함(toHaveBeenCalledTimes(1))
- check의 첫번째 인자가 true일 때, onSuccess는 yes인자와 함께 호출 되어야 함(toHaveBeenCalledWith('yes')
Mock을 이용한 사용자 로그인 테스트
class UserService {
constructor(userClient) {
this.userClient = userClient;
this.isLogedIn = false;
}
login(id, password) {
if (!this.isLogedIn) {
return this.userClient
.login(id, password)
.then((data) => (this.isLogedIn = true));
}
}
}
module.exports = UserService;
- login매소드는 로그인이 되어 있지 않으면 userClient의 login으로 로그인을 한 뒤 isLogedIn을 true로 바꿈
- 로그인이 되어 있으면 userClient의 login매소드 동작하지 않음
class UserClient {
login(id, password) {
return fetch('http://example.com/login/id+password')
.then((response) => response.json());
}
}
module.exports = UserClient;
const UserService = require("../user_service");
const UserClient = require("../user_client");
jest.mock("../user_client");
describe("UserService", () => {
const login = jest.fn(async () => {
return "success";
});
UserClient.mockImplementation(() => {
return {
login,
};
});
let userService;
beforeEach(() => {
userService = new UserService(new UserClient());
});
it("비로그인 상태에서 첫번째 로그인", async () => {
await userService.login("abc", "abc");
expect(login.mock.calls.length).toBe(1);
});
it("첫번째 로그인 이후 두번째 로그인", async () => {
await userService.login("123", "123");
await userService.login("123", "123");
expect(login.mock.calls.length).toBe(1);
});
});