๐์์ํ๋ฉฐ
์ฐ๋ง์ ๊ฐ๋ณ๊ฒ ์งํํ ํ๋ก์ ํธ์์ Jest๋ฅผ ๊ฐ๋จํ ์ ์ฉํด๋ณด์๋ค. ์ญ์ ์ฒ์ ๋์
ํ๋ ๊ฒ์ด๋ค ๋ณด๋ ์๊ฐ๋ ๋ง์ด ์์๋๊ณ ์๊ทธ๋ ๊ฑฐ๋ฆฌ๊ธฐ๋ ํ์ง๋ง ๊ทธ๋์ ๊ทธ๋ฐ์ง, PASS๊ฐ ๋ฐ ๋๋ง๋ค ๋๋ฌด ๊ธฐ๋ป์ ๋ค๋ค ๋ด๋ฌ๋ผ๊ณ ํ๊ณ ์ถ์๋คโฆ ๐คฃ
์ด์จ๋ , ์ด๋ฒ ํฌ์คํ
์์๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ์๋กญ๊ฒ ๋ฐฐ์ด Jest์ ๋ด์ฉ์ ์ ๋ฆฌํด๋ณด๋ฉด์ ๋ณต์ตํด๋ณด๋ ค๊ณ ํ๋ค.
โ
๋ฐฐ์ด ๊ฒ์ฆ
- ๋ฐฐ์ด ์์ ํน์ ๊ฐ์ด ํฌํจ๋์ด์๋์ง ํ์ธํ ๋๋
toContain
- ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ํ์ธํ ๋๋
toHaveLength
1
2
3
4
5
| const friends = ["์ ์งฑ๊ตฌ", "๊น์ฒ ์", "ํ์ ๋ฆฌ"]
it("friends ๋ฐฐ์ด์ ๊ฒ์ฌํ๋ค.", () => {
expect(friends).toContain("์ ์งฑ๊ตฌ")
expect(friends).toHaveLength(3)
})
|
โ
๊ฐ์ฒด ๊ฒ์ฆ
- ๊ฐ์ฒด์ ์ํ๋ ๊ฐ์ฒด์ ์ผ๋ถ๊ฐ ์๋์ง ํ์ธํ ๋๋
toMatchObject
- ๊ฐ์ฒด์ ํด๋น ํค ๊ฐ์ด ์๋์ง ํ์ธํ ๋๋
toHaveProperty
1
2
3
4
5
6
7
| const jjanggu = { name: "์ ์งฑ๊ตฌ", class: "ํด๋ฐ๋ผ๊ธฐ" }
it("jjanggu ๊ฐ์ฒด๋ฅผ ๊ฒ์ฌํ๋ค.", () => {
expect(jjanggu).toMatchObject({ name: "์ ์งฑ๊ตฌ" })
expect(jjanggu).toMatchObject({ name: "์ ์งฑ๊ตฌ", class: "ํด๋ฐ๋ผ๊ธฐ" })
expect(jjanggu).toMatchObject({ name: "์ ์งฑ๊ตฌ", class: "์ฅ๋ฏธ" }) //Fail
expect(jjanggu).toHaveProperty("name")
})
|
์งฑ๊ตฌ๋ ํด๋ฐ๋ผ๊ธฐ ๋ฐ์ด๊ณ ์ฅ๋ฏธ ๋ฐ์ด ์๋๋ฏ๋ก, ์๋ ํ
์คํธ๋ ์คํจํ๋ค.
1
| expect(jjanggu).toMatchObject({ name: "์ ์งฑ๊ตฌ", class: "์ฅ๋ฏธ" }) //Fail
|
โ
mocking
ํจ์ (utils)๋ฅผ ๋ชจํนํ๋ fn
, mock
, spyOn
์ ์ฐ์์๋ฅผ ์์๋ณด์!
โก๏ธfn
- ๋
๋ฆฝ์ ์ธ ํจ์๋ฅผ ๋ชจํนํ๋ค.
- API์ ์ฑ๊ณต, ์คํจ ์ฌ๋ก๋ฅผ ํ
์คํธ ํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
- ์๋ณธ ํจ์๋ฅผ importํ ํ์๊ฐ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| it("sum ํจ์๋ฅผ jest.fn์ผ๋ก ๋ชจํนํด๋ณธ๋ค.", () => {
const mockSum = jest.fn((a: number, b: number) => {
if (a === 1 && b === 2) return 10
if (a === 3 && b === 4) return 20
return 0
})
expect(mockSum(1, 2)).toBe(10)
expect(mockSum).toHaveBeenCalledWith(1, 2) // ํจ์ ์ธ์๊ฐ 1, 2๋ก ํธ์ถ๋จ
expect(mockSum(3, 4)).toBe(20)
expect(mockSum(5, 6)).toBe(0)
expect(mockSum).toHaveBeenCalledTimes(3) // ํจ์๊ฐ ์ธ ๋ฒ ํธ์ถ๋จ
})
|
โก๏ธmock
- ํน์ ๋ชจ๋ ์ ์ฒด๋ฅผ ๋ชจํนํ๋ค.
next.js์์ ์์ฃผ ์ฌ์ฉํ๋ next/navigation
์ ๋ชจํนํด, pathname์ ํ๋ฉด์ ํ์ํ๋ ์ปดํฌ๋ํธ๋ฅผ ํ
์คํธํด๋ณด์.
1
2
3
4
5
6
7
8
9
10
| "use client"
import React from "react"
import { usePathname } from "next/navigation"
const Path = () => {
const pathname = usePathname()
return <div>{pathname}</div>
}
export default Path
|
jest.mock
๊ธฐ๋ฅ์ ์ด์ฉํด next/navigation
์ ๋ชจํนํ๋ค.- ๋ชจํนํ์ ๋์ ๊ฒฐ๊ณผ๊ฐ์
mockReturnValue
๋ฅผ ์ด์ฉํด ์ค์ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| import { render, screen, act } from "@testing-library/react";
import Path from "./Path";
import { usePathname } from "next/navigation";
//๋ชจ๋์ ๋ชจํนํ๋ค.
jest.mock("next/navigation", () => ({
usePathname: jest.fn(),
}));
it("ํ์ฌ path๊ฐ ํ๋ฉด์ ํ์๋๋ค.", () => {
// ์ด๊ธฐ ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๋ค.
(usePathname as jest.Mock).mockReturnValue("/origin-route");
const { rerender } = render(<Path />); //๋ฆฌ๋ ๋๋ง์ ์๋์ผ๋ก triggerํ ์ ์๊ฒ ํด์ค๋ค.
screen.debug();
// ํ๋ฉด์ '/origin-route' ๊ฒฝ๋ก๊ฐ ํ์๋๋์ง ํ์ธํ๋ค.
expect(screen.getByText("/origin-route")).toBeInTheDocument();
// ๊ฒฝ๋ก๋ฅผ ๋ณ๊ฒฝํ๋ค.
//๋ฆฌ๋ ๋๋ง์ ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด act๋ก ๊ฐ์ผ๋ค.
act(() => {
(usePathname as jest.Mock).mockReturnValue("/new-route");
});
// ๊ฒฝ๋ก๊ฐ ๋ณ๊ฒฝ๋์๋์ง ํ์ธํ๋ค.
// ์๋์ผ๋ก rerender triggerํด์ค๋ค.
rerender(<Path />);
screen.debug();
expect(screen.getByText("/new-route")).toBeInTheDocument();
});
|
โก๏ธspyOn
1
2
3
| export default function sum(a: number, b: number): number {
return a + b
}
|
์ด๋ฌํ sum
ํจ์๋ฅผ ๊ฐ์ํด๋ณด์.
1
2
3
4
| import sum from "./sum"
jest.spyOn(sum) //๊ฐ์ ๋ถ๊ฐ
jest.spyOn({ sum }, "sum") // ์ ์ ์๋
|
:star: ์์ ์์ฑํ ๊ฒ ์ฒ๋ผ, jest.spyOn
๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ํ๊ธฐ ๋๋ฌธ์, jest.spyOn(sum)
์ผ๋ก ์์ฑํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ๋ฐ๋ผ์, jest.spyOn({ sum }, "sum")
๋ค์๊ณผ ๊ฐ์ด ์์๋ก ๊ฐ์ฒด ๋ฅผ ๋ง๋ค์ด์ ๊ฐ์ํด์ผ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| it("sum ํจ์์ spyOn์ ์ ์ฉํด ๋์์ ๊ฒ์ฆํ๋ค.", () => {
// sum ํจ์์ spyOn์ ์ ์ฉ
const sumSpy = jest.spyOn({ sum }, "sum")
// sum ํจ์ ํธ์ถ
const result = sum(3, 7)
// ํจ์ ํธ์ถ ์ฌ๋ถ ํ์ธ
expect(sumSpy).toHaveBeenCalled()
expect(sumSpy).toHaveBeenCalledTimes(1)
// ํธ์ถ๋ ์ธ์ ํ์ธ
expect(sumSpy).toHaveBeenCalledWith(3, 7)
// ๊ฒฐ๊ณผ๊ฐ ํ์ธ
expect(result).toBe(10)
// Spy ํด์
// ํด์ ํจ์ผ๋ก์ ์๋ ํจ์์ ๋์์ ๋ณต์ํ์ฌ, ๋ค๋ฅธ ํ
์คํธ์ ์ํฅ์ ์ฃผ์ง ์๋๋ก ํ๋ค.
sumSpy.mockRestore()
})
|
๐๏ธ์ฐธ๊ณ ๋์
- ํ๋ฐํธ์๋ ๊ฐ๋ฐ์ ์ํ ํ
์คํธ ์
๋ฌธ