728x90
๐ต ๊ฐ๋ ๋ฐ ์ญํ
- ์คํ์ด(Spy): ์ค์ ํจ์์ ๋์์ ๊ทธ๋๋ก ์ ์งํ๋ฉด์, ํธ์ถ ์ฌ๋ถ, ํธ์ถ ํ์, ์ ๋ฌ๋ ์ธ์ ๋ฑ์ ๊ธฐ๋กํฉ๋๋ค.
- ๋ชฉ์ : ํจ์์ ๋ด๋ถ ๋ก์ง์ ๊ฑด๋๋ฆฌ์ง ์๊ณ , ํจ์๊ฐ ์ด๋ป๊ฒ ์ฌ์ฉ๋์๋์ง๋ฅผ ๊ฐ์ํ์ฌ ํ ์คํธ์ ์ ๋ขฐ์ฑ์ ๋์ด๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
๐ต ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
- ์คํ์ด ์์ฑํ๊ธฐ
const calculator = { add: (a, b) => a + b, }; // 'calculator' ๊ฐ์ฒด์ 'add' ๋ฉ์๋์ ์คํ์ด ๋ถ์ฐฉ const spyFn = jest.spyOn(calculator, 'add');
- ํจ์ ํธ์ถ ๋ฐ ๊ฒฐ๊ณผ ๊ฒ์ฆ
const result = calculator.add(2, 3); // ํธ์ถ ํ์ ํ์ธ expect(spyFn).toHaveBeenCalledTimes(1); // ์ ๋ฌ๋ ์ธ์ ํ์ธ expect(spyFn).toHaveBeenCalledWith(2, 3); // ํจ์์ ์ค์ ๊ฒฐ๊ณผ๊ฐ ํ์ธ expect(result).toBe(5);
- ์ค๋ช : ์ ์์ ๋ jest.spyOn()์ ์ฌ์ฉํ์ฌ add ํจ์๊ฐ ํธ์ถ๋ ํ์์ ์ธ์๋ฅผ ๊ฒ์ฆํ๋ฉด์๋, ํจ์์ ์๋ ๋์(๋ ์์ ํฉ)์ ๊ทธ๋๋ก ์ํํจ์ ๋ณด์ฌ์ค๋๋ค.
๐ต ํ์ฅ ๊ธฐ๋ฅ ๋ฐ ํ์ฉ๋ฒ
- ํจ์ ๋์ ๋์ฒด (mockImplementation)
- ๋ชฉ์ : ํ ์คํธ ์ํฉ์ ๋ฐ๋ผ ํจ์์ ๋์์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
- ์์ :
jest.spyOn(myObject, 'myMethod').mockImplementation(arg => `Mocked, ${arg}!`); console.log(myObject.myMethod('World')); // ์ถ๋ ฅ: "Mocked, World!"
- ์ค๋ช : ์ ์์ ๋ myMethod์ ์๋ ๋ก์ง ๋์ ์ง์ ํ ๋ก์ง์ผ๋ก ๋์ํ๋๋ก ์ค์ ํ์ฌ, ํน์ ํ ์คํธ ์๋๋ฆฌ์ค์ ๋ง๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๊ฒ ํฉ๋๋ค.
- ์๋ณธ ํจ์ ๋ณต๊ตฌ (mockRestore)
- ๋ชฉ์ : ํ ์คํธ ํ ์๋ ํจ์์ ์ํ๋ก ๋ณต๊ตฌํ์ฌ ๋ค๋ฅธ ํ ์คํธ์ ์ํฅ์ ์ฃผ์ง ์๋๋ก ํฉ๋๋ค.
- ์์ :
const spy = jest.spyOn(myObject, 'myMethod'); // ... ํ ์คํธ ์ฝ๋ ์คํ ... spy.mockRestore(); // ์๋ณธ ํจ์๋ก ๋ณต๊ตฌ
- ์ค๋ช : ๋ณต๊ตฌํ์ง ์์ผ๋ฉด ์คํ์ด๊ฐ ๋จ์ ๋ค๋ฅธ ํ ์คํธ์ ์ํฅ์ ์ค ์ ์์ผ๋ฏ๋ก, ํ ์คํธ ํ ๋ฐ๋์ ์๋ ์ํ๋ก ๋๋ ค๋์์ผ ํฉ๋๋ค.
๐ต ์ค์ ํ ์คํธ ํ๊ฒฝ์์์ ์ฌ์ฉ ์์
- ์ธ๋ถ API ํ
์คํธ
- ์๋ฅผ ๋ค์ด, axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ด๋ถ axios.get ํจ์์ ์คํ์ด๋ฅผ ๋ถ์ฐฉํ์ฌ API ํธ์ถ์ ๊ฐ์ํ ์ ์์ต๋๋ค.
const axios = require("axios"); const userService = require("./userService"); test("findOne fetches data from the API endpoint", async () => { const spyGet = jest.spyOn(axios, "get"); await userService.findOne(1); expect(spyGet).toHaveBeenCalledTimes(1); expect(spyGet).toHaveBeenCalledWith(`https://jsonplaceholder.typicode.com/users/1`); });
- ์ฃผ์์ฌํญ: ์ธ๋ถ API์ ์์กดํ์ง ์๋๋ก, ํ์ํ ๊ฒฝ์ฐ jest.fn()์ ํ์ฉํด ๊ฐ์ง ํจ์๋ก ๋์ฒดํ์ฌ ํ ์คํธ ํ๊ฒฝ์ ๋ ๋ฆฝ์ ์ผ๋ก ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
- ํ
์คํธ์ ๊ฒฐ์ ์ฑ(Deterministic) ํ๋ณด
- ๋ชฉ์ : ๋คํธ์ํฌ ์ํ๋ API ์๋ฒ์ ์๋ต์ ์ํฅ์ ๋ฐ์ง ์๊ณ ํญ์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋๋ก ํ ์คํธ๋ฅผ ์์ฑํฉ๋๋ค.
- ๋ฐฉ๋ฒ: axios.get๋ฅผ jest.fn().mockResolvedValue()๋ก ๋์ฒดํ์ฌ ๊ณ ์ ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๊ฒ ํฉ๋๋ค.
axios.get = jest.fn().mockResolvedValue({ data: { id: 1, name: "Dale Seo", }, });
๐ต ์์ฝ
- jest.spyOn()์ ํจ์์ ํธ์ถ ์ ๋ณด๋ฅผ ๊ฐ์ํ๋ฉด์๋ ์๋ณธ ๋ก์ง์ ๊ทธ๋๋ก ์ ์งํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ์ ๋๋ค.
- ํ์์ ๋ฐ๋ผ ํจ์์ ๋์์ ๋ณ๊ฒฝํ๊ฑฐ๋, ํ ์คํธ ํ ์๋ ์ํ๋ก ๋ณต๊ตฌํ๋ ๋ฑ์ ๋ค์ํ ํ์ฉ๋ฒ์ด ์์ต๋๋ค.
- ์ด๋ฅผ ํตํด ํ ์คํธ์ ์ ๋ขฐ์ฑ์ ๋์ด๊ณ , ์ธ๋ถ ํ๊ฒฝ์ ์์กดํ์ง ์๋ ์์ ์ ์ธ ํ ์คํธ ํ๊ฒฝ์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
์ถ์ฒ
https://www.daleseo.com/jest-fn-spy-on/
'JavaScript > js ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
js ๋ฐฐ์ด ๋ง๋ค๊ธฐ : Array.from(), Array.fromAsync(), Array.isArray(), Array.of() (1) | 2025.04.11 |
---|---|
ํ ์คํธ์ฝ๋ ๋ง๋ค๊ธฐ - jest (0) | 2025.03.07 |
LocalTunnel์ ์ด์ฉํ ๋ก์ปฌ ์๋ฒ ์ธ๋ถ ๊ณต์ ๋ฐฉ๋ฒ (0) | 2025.02.24 |
์ฌ์ฉ์์ ์์น ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ - ํญ์ ํ์ฉ (0) | 2025.02.24 |
Nestjs์์ Valkey ์ฐ๊ธฐ - *.service.ts (0) | 2025.02.22 |