@testing-library/vue: 컴포넌트 마운트
@testing-library/vue로 Vue 컴포넌트를 마운트하고 props와 전역 플러그인을 제공하며, 사용자 이벤트를 시뮬레이션하고 렌더링된 템플릿을 검증합니다.
@testing-library/vue: 컴포넌트 마운트은(는) CoddyKit의 무료 Frontend Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Frontend Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Frontend Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
Vue용 Testing Library
@testing-library/vue는 Vue 3 컴포넌트에 동일한 사용자 중심 테스트 방식을 제공합니다. API는 React 버전과 유사합니다. render(), screen 조회, userEvent, jest-dom 매처를 사용할 수 있습니다.
설치
Vue용 테스트 라이브러리 패키지를 설치하십시오.
npm install -D @testing-library/vue @testing-library/user-event @testing-library/jest-dom
# Also install vitest for a Vite project:
npm install -D vitest jsdomVue용 render()
render(Component, options)는 Vue 컴포넌트를 마운트합니다. options 객체를 사용하여 props, 전역 플러그인, 설정 옵션을 전달하십시오.
import { render, screen } from '@testing-library/vue';
import Button from './Button.vue';
test('renders button label', () => {
render(Button, { props: { label: 'Submit' } });
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
});Props 전달
options 객체에서 props를 전달하십시오. defineProps<T>()를 사용하면 TypeScript가 prop 유형을 확인합니다.
render(UserCard, {
props: {
user: { id: 1, name: 'Alice', email: 'alice@example.com' }
}
});전역 플러그인 제공
전역 구성(플러그인, 컴포넌트, provide 값)을 global 옵션으로 전달하십시오.
import { createPinia } from 'pinia';
import router from './router';
render(App, {
global: {
plugins: [createPinia(), router],
provide: { theme: 'dark' }
}
});재사용 가능한 renderWithPlugins 도우미
테스트마다 설정을 반복하지 않도록 테스트에 필요한 모든 플러그인을 포함하는 사용자 지정 렌더링 래퍼를 만드십시오.
// test-utils.ts
import { render } from '@testing-library/vue';
import { createPinia } from 'pinia';
export function renderWithSetup(component: any, options = {}) {
return render(component, {
global: { plugins: [createPinia()] },
...options
});
}이벤트 테스트
userEvent로 컴포넌트와 상호작용한 다음 사용자가 보는 내용이나 발생한 이벤트를 단언하십시오.
import userEvent from '@testing-library/user-event';
test('emits submit on form submit', async () => {
const user = userEvent.setup();
const { emitted } = render(LoginForm);
await user.type(screen.getByLabelText('Email'), 'alice@example.com');
await user.click(screen.getByRole('button', { name: /login/i }));
expect(emitted().submit[0]).toEqual([{ email: 'alice@example.com' }]);
});슬롯 테스트
render 옵션에서 슬롯 콘텐츠를 문자열이나 컴포넌트로 전달하십시오.
render(Card, {
slots: {
default: '<p>Card body content</p>',
header: '<h2>My Card Title</h2>'
}
});
expect(screen.getByText('Card body content')).toBeInTheDocument();v-model 동작 테스트
userEvent로 입력 필드를 채우고, 발생한 이벤트나 표시된 출력이 예상한 업데이트된 상태와 일치하는지 확인하십시오.
Pinia 스토어 테스트
각 테스트마다 새로운 Pinia 인스턴스를 만드십시오. 설정을 위해 스토어 상태를 직접 수정한 다음 렌더링된 출력을 단언하십시오.
const pinia = createPinia();
render(Counter, { global: { plugins: [pinia] } });
const store = useCounterStore();
store.count = 5; // set initial state
await nextTick();
expect(screen.getByText('5')).toBeInTheDocument();비동기 업데이트 후 요소 찾기
Vue는 DOM을 비동기적으로 업데이트합니다. 이벤트를 발생시켜 비동기 상태 변경을 유도한 후 await nextTick() 또는 findBy* 조회를 사용하십시오.
import { nextTick } from 'vue';
await user.click(button);
await nextTick();
expect(screen.getByText('Updated!')).toBeInTheDocument();빠른 확인
@testing-library/vue로 테스트하는 컴포넌트에 Pinia나 Vue Router를 어떻게 전달합니까?
복습: @testing-library/vue
render(Component, options)는 Vue 컴포넌트를 마운트합니다. options.props로 props를 전달합니다. global.plugins로 플러그인을 제공합니다. 사용자 지정 이벤트를 확인하려면 emitted()를 사용합니다. options.slots로 슬롯을 전달합니다. Vue의 비동기 업데이트에는 await nextTick()을 사용합니다. 공유 플러그인 설정을 위해 사용자 지정 renderWithSetup() 래퍼를 만듭니다.
자주 묻는 질문
“@testing-library/vue: 컴포넌트 마운트” 강의는 무료인가요?
네 — “@testing-library/vue: 컴포넌트 마운트” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Frontend Academy 강의 전체를 잠금 해제할 수 있습니다. Frontend Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“@testing-library/vue: 컴포넌트 마운트”에서 뭘 배우나요?
@testing-library/vue로 Vue 컴포넌트를 마운트하고 props와 전역 플러그인을 제공하며, 사용자 이벤트를 시뮬레이션하고 렌더링된 템플릿을 검증합니다. 브라우저에서 직접 실행하는 실습 코드로 Frontend Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Frontend Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Frontend Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“@testing-library/vue: 컴포넌트 마운트” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Frontend Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Frontend Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Jest 설정과 기본 테스트
- @testing-library/react: render와 userEvent
- @testing-library/vue: 컴포넌트 마운트
- 모듈과 API 호출 모킹