Vue.js
Vue.js는 웹 사용자 인터페이스를 구축하기 위한 접근하기 쉽고, 성능이 좋으며 다재다능한 프레임워크입니다. WebdriverIO와 브라우저 러너를 사용하여 실제 브라우저에서 직접 Vue.js 컴포넌트를 테스트할 수 있습니다.
설정
Vue.js 프로젝트 내에서 WebdriverIO를 설정하려면, 컴포넌트 테스팅 문서의 지침을 따르세요. 러너 옵션 내에서 프리셋으로 vue를 선택해야 합니다. 예:
// wdio.conf.js
export const config = {
// ...
runner: ['browser', {
preset: 'vue'
}],
// ...
}
정보
Vue 프리셋은 @vitejs/plugin-vue가 설치되어 있어야 합니다. 또한 컴포넌트를 테스트 페이지에 렌더링하기 위해 Testing Library를 사용하는 것을 권장합니다. 따라서 다음과 같은 추가 의존성을 설치해야 합니다:
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @testing-library/vue @vitejs/plugin-vue
yarn add --dev @testing-library/vue @vitejs/plugin-vue
pnpm add --save-dev @testing-library/vue @vitejs/plugin-vue
bun add --dev @testing-library/vue @vitejs/plugin-vue
그런 다음 다음 명령을 실행하여 테스트를 시작할 수 있습니다:
npx wdio run ./wdio.conf.js
테스트 작성하기
다음과 같은 Vue.js 컴포넌트가 있다고 가정해 봅시다:
./components/Component.vue
<template>
<div>
<p>Times clicked: {{ count }}</p>
<button @click="increment">increment</button>
</div>
</template>
<script>
export default {
data: () => ({
count: 0,
}),
methods: {
increment() {
this.count++
},
},
}
</script>
테스트에서는 컴포넌트를 DOM으로 렌더링하고 그에 대한 단언(assertion)을 수행합니다. 컴포넌트를 테스트 페이지에 연결하기 위해 @vue/test-utils 또는 @testing-library/vue를 사용하는 것을 권장합니다. 컴포넌트와 상호작용하려면 실제 사용자 상호작용에 더 가깝게 동작하는 WebdriverIO 명령을 사용하세요. 예:
- @vue/test-utils
- @testing-library/vue
vue.test.js
import { $, expect } from '@wdio/globals'
import { mount } from '@vue/test-utils'
import Component from './components/Component.vue'
describe('Vue Component Testing', () => {
it('increments value on click', async () => {
// The render method returns a collection of utilities to query your component.
const wrapper = mount(Component, { attachTo: document.body })
expect(wrapper.text()).toContain('Times clicked: 0')
const button = await $('aria/increment')
// Dispatch a native click event to our button element.
await button.click()
await button.click()
expect(wrapper.text()).toContain('Times clicked: 2')
await expect($('p=Times clicked: 2')).toExist() // same assertion with WebdriverIO
})
})
vue.test.js
import { $, expect } from '@wdio/globals'
import { render } from '@testing-library/vue'
import Component from './components/Component.vue'
describe('Vue Component Testing', () => {
it('increments value on click', async () => {
// The render method returns a collection of utilities to query your component.
const { getByText } = render(Component)
// getByText returns the first matching node for the provided text, and
// throws an error if no elements match or if more than one match is found.
getByText('Times clicked: 0')
const button = await $(getByText('increment'))
// Dispatch a native click event to our button element.
await button.click()
await button.click()
getByText('Times clicked: 2') // assert with Testing Library
await expect($('p=Times clicked: 2')).toExist() // assert with WebdriverIO
})
})
Vue.js용 WebdriverIO 컴포넌트 테스트 스위트의 전체 예제는 예제 저장소에서 찾을 수 있습니다.