Skip to content

Commit 60c0e12

Browse files
[docs] add docs for testing Lit components with WebdriverIO (#1287)
1 parent bc5c21a commit 60c0e12

File tree

1 file changed

+48
-2
lines changed
  • packages/lit-dev-content/site/docs/v3/tools

1 file changed

+48
-2
lines changed

packages/lit-dev-content/site/docs/v3/tools/testing.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the [Starter Kits](/docs/v3/tools/starter-kits/) documentation for an easy t
1515

1616
## Selecting a test framework
1717

18-
Lit is a standard modern Javascript library, and you can use virtually any Javascript testing framework to test your Lit code. There are many popular options, including [Jest](https://jestjs.io/), [Karma](https://karma-runner.github.io/), [Mocha](https://mochajs.org/), [Jasmine](https://jasmine.github.io/), and [Web Test Runner](https://modern-web.dev/docs/test-runner/overview/).
18+
Lit is a standard modern Javascript library, and you can use virtually any Javascript testing framework to test your Lit code. There are many popular options, including [Jest](https://jestjs.io/), [Karma](https://karma-runner.github.io/), [Mocha](https://mochajs.org/), [Jasmine](https://jasmine.github.io/), [WebdriverIO](https://webdriver.io) and [Web Test Runner](https://modern-web.dev/docs/test-runner/overview/).
1919

2020
There are a few things you'll want to make sure your testing environment supports to effectively test your Lit code.
2121

@@ -37,7 +37,7 @@ To test on older browsers, your test environment will need to load some polyfill
3737

3838
## Using Web Test Runner { #web-test-runner }
3939

40-
We recommend using [Web Test Runner](https://modern-web.dev/docs/test-runner/overview/) since it is specifically designed to test modern web libraries like Lit using modern web features like custom elements and shadow DOM. See the [Getting Started](https://modern-web.dev/guides/test-runner/getting-started) documentation for Web Test Runner.
40+
[Web Test Runner](https://modern-web.dev/docs/test-runner/overview/) is specifically designed to test modern web libraries like Lit using modern web features like custom elements and shadow DOM. See the [Getting Started](https://modern-web.dev/guides/test-runner/getting-started) documentation for Web Test Runner.
4141

4242
In order to support older browsers, you need to configure Web Test Runner as follows:
4343

@@ -75,3 +75,49 @@ export default {
7575
};
7676
```
7777

78+
## Using WebdriverIO
79+
80+
[WebdriverIO](https://webdriver.io) is a good option for your component or end-to-end tests. It has very compelling advantages like support for [mocking](https://webdriver.io/docs/component-testing/mocking) and [code coverage](https://webdriver.io/docs/component-testing/coverage) reporting.
81+
82+
You can set up WebdriverIO in your project via:
83+
84+
```bash
85+
npm init wdio@latest ./
86+
```
87+
88+
It will start a configuration wizard that will guide you through some questions. Make sure select the following:
89+
90+
- __What type of testing would you like to do?__<br>Component or Unit Testing - in the browser
91+
- __Which framework do you use for building components?__<br>Lit
92+
93+
The remaining questions can be answered as desired.
94+
95+
In order test the component you have to render it into the test page before the test starts and ensure it gets cleaned up afterwards:
96+
97+
```ts
98+
import { expect, $ } from '@wdio/globals'
99+
100+
// Component.ts contains the <simple-greeting> component implemented the same as:
101+
// https://lit.dev/docs/components/overview/
102+
import './components/Component.ts'
103+
104+
describe('Lit Component testing', () => {
105+
let elem: HTMLElement
106+
107+
beforeEach(() => {
108+
elem = document.createElement('simple-greeting')
109+
})
110+
111+
it('should render component', async () => {
112+
elem.setAttribute('name', 'WebdriverIO')
113+
document.body.appendChild(elem)
114+
await expect($(elem)).toHaveText('Hello, WebdriverIO!')
115+
})
116+
117+
afterEach(() => {
118+
elem.remove()
119+
})
120+
})
121+
```
122+
123+
Find more information on [element assertions](https://webdriver.io/docs/api/expect-webdriverio), [finding elements](https://webdriver.io/docs/selectors#deep-selectors) within the Shadow DOM and [more](https://webdriver.io/docs/component-testing) in the WebdriverIO documentation.

0 commit comments

Comments
 (0)