vitest-browser-vue
Render Vue components in Vitest Browser Mode. This library follows testing-library
principles and exposes only locators and utilities that encourage you to write tests that closely resemble how your Vue components are used.
Requires vitest
and @vitest/browser
2.1.0 or higher.
import { render } from 'vitest-browser-vue'
import { expect, test } from 'vitest'
test('counter button increments the count', async () => {
const screen = render(Component, {
props: {
initialCount: 1,
}
})
await screen.getByRole('button', { name: 'Increment' }).click()
await expect.element(screen.getByText('Count is 2')).toBeVisible()
})
vitest-browser-vue
also automatically injects render
and cleanup
methods on the page
. Example:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
setupFiles: ['vitest-browser-vue'],
},
})
import { page } from '@vitest/browser/context'
test('counter button increments the count', async () => {
const screen = page.render(Component, {
props: {
initialCount: 1,
}
})
screen.cleanup()
})
Unlike @testing-library/vue
, vitest-browser-vue
cleans up the component before the test starts instead of after, so you can see the rendered result in your UI. To avoid auto-cleanup, import the render
function from vitest-browser-vue/pure
.
Special thanks