Examples
浏览器模式与框架无关,因此不提供任何渲染组件的方法。不过,你应该可以使用框架的测试工具包。
我们建议根据您的框架使用 testing-library
packages:
@testing-library/dom
if you don't use a framework@testing-library/vue
to render vue components@testing-library/svelte
to render svelte components@testing-library/react
to render react components@testing-library/preact
to render preact componentssolid-testing-library
to render solid components@marko/testing-library
to render marko components
testing-library
提供了一个包@testing-library/user-event
。我们不建议直接使用它,因为它会模拟事件而非实际触发事件--相反,请使用从 @vitest/browser/context
导入的 userEvent
,它使用 Chrome DevTools 协议或 Webdriver(取决于provider)。
ts
vue
ts
// based on @testing-library/vue example
// https://testing-library.com/docs/vue-testing-library/examples
import { userEvent } from '@vitest/browser/context'
import { render, screen } from '@testing-library/vue'
import Component from './Component.vue'
test('properly handles v-model', async () => {
render(Component)
// Asserts initial state.
expect(screen.getByText('Hi, my name is Alice')).toBeInTheDocument()
// Get the input DOM node by querying the associated label.
const usernameInput = await screen.findByLabelText(/username/i)
// Type the name into the input. This already validates that the input
// is filled correctly, no need to check the value manually.
await userEvent.fill(usernameInput, 'Bob')
expect(screen.getByText('Hi, my name is Alice')).toBeInTheDocument()
})