Skip to content

Commit 5659a0e

Browse files
EskiMojo14hi-ogawa
andauthored
feat: Added vitest-browser-lit to vitest init browser and docs (#7705)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent 2702cf4 commit 5659a0e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

docs/guide/browser/index.md

+19
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ However, Vitest also provides packages to render components for several popular
399399
- [`vitest-browser-svelte`](https://github.com/vitest-dev/vitest-browser-svelte) to render [svelte](https://svelte.dev) components
400400
- [`vitest-browser-react`](https://github.com/vitest-dev/vitest-browser-react) to render [react](https://react.dev) components
401401

402+
Community packages are available for other frameworks:
403+
404+
- [`vitest-browser-lit`](https://github.com/EskiMojo14/vitest-browser-lit) to render [lit](https://lit.dev) components
405+
402406
If your framework is not represented, feel free to create your own package - it is a simple wrapper around the framework renderer and `page.elementLocator` API. We will add a link to it on this page. Make sure it has a name starting with `vitest-browser-`.
403407

404408
Besides rendering components and locating elements, you will also need to make assertions. Vitest forks the [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) library to provide a wide range of DOM assertions out of the box. Read more at the [Assertions API](/guide/browser/assertion-api).
@@ -473,6 +477,21 @@ test('loads and displays greeting', async () => {
473477
await expect.element(screen.getByRole('button')).toBeDisabled()
474478
})
475479
```
480+
```ts [lit]
481+
import { render } from 'vitest-browser-lit'
482+
import { html } from 'lit'
483+
import './greeter-button'
484+
485+
test('greeting appears on click', async () => {
486+
const screen = render(html`<greeter-button name="World"></greeter-button>`)
487+
488+
const button = screen.getByRole('button')
489+
await button.click()
490+
const greeting = screen.getByText(/hello world/iu)
491+
492+
await expect.element(greeting).toBeInTheDocument()
493+
})
494+
```
476495
:::
477496

478497
Vitest doesn't support all frameworks out of the box, but you can use external tools to run tests with these frameworks. We also encourage the community to create their own `vitest-browser` wrappers - if you have one, feel free to add it to the examples above.

packages/vitest/src/create/browser/creator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ function getFramework(): prompt.Choice[] {
8484
value: 'react',
8585
description: '"The library for web and native user interfaces"',
8686
},
87+
{
88+
title: 'lit',
89+
value: 'lit',
90+
description: '"A simple library for building fast, lightweight web components."',
91+
},
8792
{
8893
title: 'preact',
8994
value: 'preact',
@@ -112,6 +117,8 @@ function getFrameworkTestPackage(framework: string) {
112117
return 'vitest-browser-svelte'
113118
case 'react':
114119
return 'vitest-browser-react'
120+
case 'lit':
121+
return 'vitest-browser-lit'
115122
case 'preact':
116123
return '@testing-library/preact'
117124
case 'solid':
@@ -205,6 +212,9 @@ function getPossibleFramework(dependencies: Record<string, string>) {
205212
if (dependencies.svelte || dependencies['@sveltejs/kit']) {
206213
return 'svelte'
207214
}
215+
if (dependencies.lit || dependencies['lit-html']) {
216+
return 'lit'
217+
}
208218
if (dependencies.preact) {
209219
return 'preact'
210220
}

packages/vitest/src/create/browser/examples.ts

+58
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,62 @@ test('renders name', async () => {
135135
`,
136136
}
137137

138+
const litExample = {
139+
name: 'HelloWorld.js',
140+
js: `
141+
import { html, LitElement } from 'lit'
142+
143+
export class HelloWorld extends LitElement {
144+
static properties = {
145+
name: { type: String },
146+
}
147+
148+
constructor() {
149+
super()
150+
this.name = 'World'
151+
}
152+
153+
render() {
154+
return html\`<h1>Hello \${this.name}!</h1>\`
155+
}
156+
}
157+
158+
customElements.define('hello-world', HelloWorld)
159+
`,
160+
ts: `
161+
import { html, LitElement } from 'lit'
162+
import { customElement, property } from 'lit/decorators.js'
163+
164+
@customElement('hello-world')
165+
export class HelloWorld extends LitElement {
166+
@property({ type: String })
167+
name = 'World'
168+
169+
render() {
170+
return html\`<h1>Hello \${this.name}!</h1>\`
171+
}
172+
}
173+
174+
declare global {
175+
interface HTMLElementTagNameMap {
176+
'hello-world': HelloWorld
177+
}
178+
}
179+
`,
180+
test: `
181+
import { expect, test } from 'vitest'
182+
import { render } from 'vitest-browser-lit'
183+
import { html } from 'lit'
184+
import './HelloWorld.js'
185+
186+
test('renders name', async () => {
187+
const screen = render(html\`<hello-world name="Vitest"></hello-world>\`)
188+
const element = screen.getByText('Hello Vitest!')
189+
await expect.element(element).toBeInTheDocument()
190+
})
191+
`,
192+
}
193+
138194
const vanillaExample = {
139195
name: 'HelloWorld.js',
140196
js: `
@@ -191,6 +247,8 @@ function getExampleTest(framework: string) {
191247
return vueExample
192248
case 'svelte':
193249
return svelteExample
250+
case 'lit':
251+
return litExample
194252
case 'marko':
195253
return markoExample
196254
default:

0 commit comments

Comments
 (0)