Testing
๐ ๏ธ ์์ด์ ํธ ์คํฌ
npx -y skills add DaleStudy/skills --skill testingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
React Testing Library ๋ฐ Vitest ๊ธฐ๋ฐ ํ ์คํ ๋ชจ๋ฒ ๊ด๋ก. ๋ค์ ์ํฉ์์ ์ฌ์ฉ: (1) .test.tsx, .test.ts, .spec.tsx, .spec.ts ํ์ผ ์์ ์, (2) ์ปดํฌ๋ํธ ํ ์คํธ ์์ฑ ๋๋ ๋ฆฌํฉํ ๋ง ์, (3) 'test', 'testing', 'vitest', 'RTL', 'getByRole', 'userEvent', 'waitFor', 'expect' ํค์๋ ํฌํจ ์์ ์, (4) ๋น๋๊ธฐ ๋ก์ง ๋๋ ์ฌ์ฉ์ ์ํธ์์ฉ ํ ์คํธ ์์ฑ ์
The file declares its own license as MIT. That is the authorโs claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
6.2 KB, as published. Nobody here has run it
Testing Library
React Testing Library ๊ธฐ๋ฐ ํ ์คํธ ์์ฑ ๋ชจ๋ฒ ๊ด๋ก ๋ฐ ์ํฐํจํด ํํผ ๊ฐ์ด๋.
ํต์ฌ ์์น
Testing Library์ ์ฒ ํ: ์ฌ์ฉ์๊ฐ ์ฌ์ฉํ๋ ๋ฐฉ์๋๋ก ํ ์คํธํ๋ผ
- ์ ๊ทผ์ฑ ๊ธฐ๋ฐ ์ฟผ๋ฆฌ ์ฐ์ - ์ค์ ์ฌ์ฉ์๊ฐ ์์๋ฅผ ์ฐพ๋ ๋ฐฉ์ ์ฌ์ฉ
- ๊ตฌํ ์ธ๋ถ์ฌํญ ํ ์คํธ ๊ธ์ง - ์ปดํฌ๋ํธ ๋ด๋ถ ์ํ/๋ฉ์๋ ์ง์ ์ ๊ทผ ์ง์
- ์ค์ ์ฌ์ฉ์ ํ๋ ์๋ฎฌ๋ ์ด์ - userEvent ์ฌ์ฉ, fireEvent ์ง์
- ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ช ์์ ๋๊ธฐ - waitFor, findBy ํ์ฉ
์ฟผ๋ฆฌ ์ฐ์ ์์
Testing Library๋ ๋ค์ํ ์ฟผ๋ฆฌ๋ฅผ ์ ๊ณตํ์ง๋ง, ์ ๊ทผ์ฑ๊ณผ ์ฌ์ฉ์ ๊ฒฝํ์ ๋ฐ์ํ๋ ์์๋ก ์ฌ์ฉํด์ผ ํจ.
๊ถ์ฅ ์ฟผ๋ฆฌ ์์ (๋์ โ ๋ฎ์)
getByRole(์ต์ฐ์ ) - ์คํฌ๋ฆฐ ๋ฆฌ๋๊ฐ ์ธ์ํ๋ ๋ฐฉ์getByLabelText- ํผ ์์ (label๊ณผ ์ฐ๊ฒฐ๋ input)getByPlaceholderText- placeholder๊ฐ ๋ช ํํ ๊ฒฝ์ฐgetByText- ํ ์คํธ ์ฝํ ์ธ ๋ก ๊ฒ์getByDisplayValue- ํ์ฌ ์ ๋ ฅ๋ ๊ฐ์ผ๋ก ๊ฒ์ (ํผ ์์)getByAltText- ์ด๋ฏธ์ง alt ์์ฑgetByTitle- title ์์ฑ (tooltip ๋ฑ)getByTestId(์ตํ ์๋จ) - ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ๋ถ๊ฐ๋ฅํ ๋๋ง ์ฌ์ฉ
์์ธ ๊ฐ์ด๋: references/query-priority.md
์ฌ์ฉ์ ์ํธ์์ฉ ํ ์คํธ
userEvent ์ฌ์ฉ (๊ถ์ฅ)
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
test('์ฌ์ฉ์๊ฐ ํผ์ ์ ์ถํ ์ ์๋ค', async () => {
const user = userEvent.setup();
render(<LoginForm />);
await user.type(screen.getByRole('textbox', { name: /์ด๋ฉ์ผ/i }), '[email protected]');
await user.type(screen.getByLabelText(/๋น๋ฐ๋ฒํธ/i), 'password123');
await user.click(screen.getByRole('button', { name: /๋ก๊ทธ์ธ/i }));
expect(await screen.findByText(/ํ์ํฉ๋๋ค/i)).toBeInTheDocument();
});
ํต์ฌ:
userEvent.setup()ํธ์ถ ํ ์ฌ์ฉ- ๋ชจ๋ user ๋ฉ์๋๋
awaitํ์ - ์ค์ ๋ธ๋ผ์ฐ์ ์ด๋ฒคํธ ์์ ์ฌํ (focus, keydown, keyup ๋ฑ)
fireEvent ์ง์
// โ ๋์ ์ - fireEvent ์ฌ์ฉ
fireEvent.click(button);
fireEvent.change(input, { target: { value: "text" } });
// โ
์ข์ ์ - userEvent ์ฌ์ฉ
await user.click(button);
await user.type(input, "text");
์์ธ ๊ฐ์ด๋: references/user-events.md
๋น๋๊ธฐ ์ฒ๋ฆฌ
findBy ์ฟผ๋ฆฌ (๊ถ์ฅ)
// โ
์ข์ ์ - findBy ์ฌ์ฉ
const successMessage = await screen.findByText(/์ ์ฅ๋์์ต๋๋ค/i);
expect(successMessage).toBeInTheDocument();
findBy = getBy + waitFor ์กฐํฉ (์๋์ผ๋ก ์์ ๋ํ๋ ๋๊น์ง ๋๊ธฐ)
waitFor ์ฌ์ฉ
// ๋ณต์กํ ๋น๋๊ธฐ ๊ฒ์ฆ
await waitFor(() => {
expect(screen.getByRole("alert")).toHaveTextContent("์ฑ๊ณต");
});
// ์ฌ๋ฌ ์กฐ๊ฑด ๊ฒ์ฆ
await waitFor(() => {
expect(mockFn).toHaveBeenCalledTimes(1);
expect(screen.queryByText(/๋ก๋ฉ ์ค/i)).not.toBeInTheDocument();
});
์ํฐํจํด
// โ ๋์ ์ - ์์์ timeout
await new Promise((resolve) => setTimeout(resolve, 1000));
// โ ๋์ ์ - act() ์๋ ์ฌ์ฉ (๋ณดํต ๋ถํ์)
await act(async () => {
// ...
});
// โ
์ข์ ์ - findBy ๋๋ waitFor
await screen.findByText(/์๋ฃ/i);
์์ธ ๊ฐ์ด๋: references/async-patterns.md
์์ฃผ ํ๋ ์ค์
1. ๊ตฌํ ์ธ๋ถ์ฌํญ ํ ์คํธ
// โ ๋์ ์ - ๋ด๋ถ ์ํ ์ ๊ทผ
expect(component.state.isOpen).toBe(true);
wrapper.find(".internal-class").simulate("click");
// โ
์ข์ ์ - ์ฌ์ฉ์ ๊ด์ ๊ฒ์ฆ
expect(screen.getByRole("dialog")).toBeVisible();
await user.click(screen.getByRole("button", { name: /์ด๊ธฐ/i }));
2. container ์ฟผ๋ฆฌ ์ฌ์ฉ
// โ ๋์ ์ - container.querySelector
const { container } = render(<MyComponent />);
const button = container.querySelector('.my-button');
// โ
์ข์ ์ - screen ์ฟผ๋ฆฌ
const button = screen.getByRole('button', { name: /์ ์ถ/i });
3. ๋ถํ์ํ waitFor
// โ ๋์ ์ - ๋๊ธฐ ์์์ waitFor
await waitFor(() => {
expect(screen.getByText("Hello")).toBeInTheDocument();
});
// โ
์ข์ ์ - ๋๊ธฐ ์์๋ ์ฆ์ ๊ฒ์ฆ
expect(screen.getByText("Hello")).toBeInTheDocument();
4. getBy* + toBeInTheDocument() ์ ๊ฑฐ
getBy*๋ ์์๋ฅผ ๋ชป ์ฐพ์ผ๋ฉด throwํ๋ฏ๋ก toBeInTheDocument()๋ ๊ธฐ์ ์ ์ผ๋ก ์ค๋ณต์ด๋ค. ํ์ง๋ง ์ ๊ฑฐํ์ง ๋ง๋ผ โ ๋ฆฌํฉํ ๋ง ํ ๋จ์ ์ฟผ๋ฆฌ๊ฐ ์๋๋ผ ์๋์ ์ธ ์กด์ฌ ๊ฒ์ฆ์์ ์ฝ๋ ๋
์์๊ฒ ์ ๋ฌํ๋ ์ญํ ์ ํ๋ค.
// โ ๋์ ์ - assertion ์์ด ์ฟผ๋ฆฌ๋ง ๋จ๊น
screen.getByRole("button", { name: /์ ์ถ/i });
// โ
์ข์ ์ - ๋ช
์์ assertion์ผ๋ก ์๋ ์ ๋ฌ
expect(screen.getByRole("button", { name: /์ ์ถ/i })).toBeInTheDocument();
๋จ, ์กด์ฌ ๊ฒ์ฆ์ด ์๋ ๋ค๋ฅธ ์์ฑ์ ๊ฒ์ฆํ ๋๋ toBeInTheDocument()๋ฅผ ์ถ๊ฐํ ํ์ ์๋ค:
// โ
์ข์ ์ - ๋ค๋ฅธ matcher๋ก ์ถฉ๋ถ
expect(screen.getByRole("button", { name: /์ ์ถ/i })).toBeDisabled();
// โ ๋์ ์ - ์ค๋ณต assertion
expect(screen.getByRole("button", { name: /์ ์ถ/i })).toBeInTheDocument();
expect(screen.getByRole("button", { name: /์ ์ถ/i })).toBeDisabled();
์ ์ฒด ์ํฐํจํด ๋ชฉ๋ก: references/common-mistakes.md
Vitest + MSW ์ค์
ํ ์คํธ ํ๊ฒฝ ์ค์ ์ด ํ์ํ ๊ฒฝ์ฐ ๋ค์ ํ ํ๋ฆฟ ์ฐธ์กฐ:
assets/vitest.config.ts- Vitest ์ค์ (React ํ๋ฌ๊ทธ์ธ, ์ปค๋ฒ๋ฆฌ์ง ํฌํจ)assets/test-setup.ts- Vitest ๊ธ๋ก๋ฒ ์ค์ assets/msw-setup.ts- MSW ํธ๋ค๋ฌ ๋ฐ ์๋ฒ ์ค์