Jest 4 Mock Fn

Jest 4 - mock function

Jest์—์„œ ๊ฐ€์žฅ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. snapshot์—์„œ ์ง€์† ์ ์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฐ’์„ ๊ณ ์ •์‹œํ‚ฌ์ˆ˜๋„ ์žˆ๊ณ  ๊ฐ€์ƒ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด db๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ฑฐ๋‚˜ ํ•„์š”ํ•œ ๊ฐ’์„ ์ง€์ •ํ• ์ˆ˜ ์žˆ์–ด์„œ, ํ…Œ์ŠคํŠธํ•˜๋ฉด์„œ ๋ถˆํ•„์š”ํ•œ ์‹œ๊ฐ„์„ ์•„๋‚„์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Mock


fn()

์ง์ ‘ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•จ
  const test = jest.fn(() => '์ด ํ•จ์ˆ˜๋Š” ํ•ด๋‹น ์ŠคํŠธ๋ง์„ return ํ•ฉ๋‹ˆ๋‹ค.')

  test('return test', () => {
    expect(test).toReturn("์ด ํ•จ์ˆ˜๋Š” ํ•ด๋‹น ์ŠคํŠธ๋ง์„ return ํ•ฉ๋‹ˆ๋‹ค.")

    test.mockImplementation(() => '์ด๋ ‡๊ฒŒ mockImplementation๋ฅผ ์ด์šฉํ•ด ์ˆ˜์ •๋„ ๊ฐ€๋Šฅ')
    expect(test).toReturn("์ด๋ ‡๊ฒŒ mockImplementation๋ฅผ ์ด์šฉํ•ด ์ˆ˜์ •๋„ ๊ฐ€๋Šฅ")
    test.mockImplementationOnce(() => '์ด๋ ‡๊ฒŒ js์— yield์ฒ˜๋Ÿผ')
    .mockImplementationOnce(() => 'ํ•œ๋ฒˆ ํ˜ธ์ถœ ํ• ๋•Œ ๋งˆ๋‹ค')
    .mockImplementationOnce(() => '๋‹ค์Œ ๋‹ค์Œ์œผ๋กœ ๋„˜๊ธฐ๊ฒŒ ํ• ์ˆ˜๋„ ์žˆ๋‹ค.')

    test.mockReturnValue('์ด๋ ‡๊ฒŒ ์ง€์ •ํ•ด๋„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ฐ’์ด๋‹ค')

    // 1,2,3 ํ˜ธ์ถœํ• ๋•Œ๋งค๋‹ค ์‹คํ–‰ ๋˜๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ์ €์žฅ๋จ
  })

mock()

๋ชจ๋“ˆ์— ์žˆ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ mockํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์คŒ
  // users.js
  // 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('return test', () => {
    // resolve์‹œ ํ•ด๋‹น ๊ฐ’์œผ๋กœ ์„ค์ •
    const resp = { data: users };
    axios.get.mockResolvedValue(resp);

    // users.js์— ์žˆ๋Š” ํ•จ์ˆ˜ all์—์„œ ์‹คํ–‰ ์ค‘์ธ axios์˜ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์ง€์ •ํ•ด์„œ ๊ฒ€์‚ฌ
    // axios.get์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„๋„ ๋ผ์„œ server๋ฅผ ๋ถ™์—ฌ์„œ ์ผ์ผํžˆ ํ…Œ์ŠคํŠธ ํ•˜์ง€ ์•Š์•„๋„ ๋จ
    return Users.all().then(data => expect(data).toEqual(users));
  })

mock prop

  const mockFn = jest.fn()

  // ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌ๋œ ์ธ์ž ๊ฐ’
  mockFn.mock.calls()
  // ํ˜ธ์ถœ์˜ ๋ฐ˜ํ™˜๊ฐ’, throw ์˜ค๋ฅ˜
  mockFn.mock.results()
  // mockํ•จ์ˆ˜์˜ instance ๊ฐ’
  mockFn.mock.instance()

  // ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ํŠน์ • ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋„๋ก ์„ค์ •
  mockFn.mockReturnValue(value)
  // promise resolve๊ฐ’์„ ์„ค์ •
  mockFn.mockResolvedValue(value)
  // promise reject๊ฐ’์„ ์„ค์ •
  mockFn.mockRejectedValue(error)

  // mockํ•จ์ˆ˜ ์ดˆ๊ธฐํ™”
  mockFn.mockImplementation(fn)
  // mockํ•จ์ˆ˜ ์ดˆ๊ธฐํ™”(ํ˜ธ์ถœ ํ• ๋•Œ ๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ์ฒด์ด๋‹์„ ํ• ์ˆ˜ ์žˆ์Œ yield์™€ ๋น„์Šท)
  mockFn.mockImplementationOnce(fn)

  // ํ˜ธ์ถœ ๊ธฐ๋ก์„ ์ดˆ๊ธฐํ™”
  mockFn.mockClear() 
  // ํ˜ธ์ถœ ๊ธฐ๋ก,mock ๊ตฌํ˜„์„ ์ดˆ๊ธฐํ™”
  mockFn.mockReset()
  // mock ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋ณต์› (์ฃผ๋กœ jest.spyOn์œผ๋กœ ์ƒ์„ฑ๋œ mock ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ)
  mockFn.mockRestore()

spyOn

๊ธฐ์กด mockํ•จ์ˆ˜๋Š” mockํ•จ์ˆ˜๋กœ test๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด ๋ณ€ํ˜•๋˜์–ด ์‹ค์ œํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ mockํ•จ์ˆ˜๋กœ ๊ฒ€์‚ฌ๋ฅผ ํ•œ๋‹ค, ์‹ค์ œ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ • ์—†์ด ํ…Œ์ŠคํŠธ ํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ spyOn์ด๋‹ค.
const video = {
  play() {
    return true;
  },
};
// spy์—†์ด ์ง์ ‘ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Matcher error: received value must be a mock or spy function ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜จ๋‹ค
expect(video.play).toHaveBeenCalled();
expect(video.play()).toBe(true);

const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore(); // ์ŠคํŒŒ์ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์›๋ž˜ ๋ฉ”์„œ๋“œ๋กœ ๋ณต์›