Testing

Testing ensures your code functions as you intend and saves you from tedious debugging.

See the Starter Kits documentation for an easy to use setup with a fully pre-configured testing environment that works great for testing Lit components.

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, Karma, Mocha, Jasmine, WebdriverIO and Web Test Runner.

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

Lit components are designed to run in the browser so testing should be conducted in a browser environment. Tools specifically focusing on testing node code may not be a good fit.

While it's possible to test without a browser by shimming DOM calls, we don't recommend this approach since it won't test the code in the way your users experience it.

The test environment you use must have support for using modern Javascript, including using modules with bare module specifiers, or else down-leveling modern Javascript appropriately. See the Requirements for legacy browsers documentation for more details.

To test on older browsers, your test environment will need to load some polyfills, including the web components polyfills and Lit's polyfill-support module. See the Polyfills documentation for more details.

Web Test Runner is specifically designed to test modern web libraries like Lit using modern web features like custom elements and shadow DOM. See the Getting Started documentation for Web Test Runner.

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

Install @web/dev-server-legacy:

Setup web-test-runner.config.js:

WebdriverIO is a good option for your component or end-to-end tests. It has very compelling advantages like support for mocking and code coverage reporting.

You can set up WebdriverIO in your project via:

It will start a configuration wizard that will guide you through some questions. Make sure select the following:

  • What type of testing would you like to do?
    Component or Unit Testing - in the browser
  • Which framework do you use for building components?
    Lit

The remaining questions can be answered as desired.

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:

Find more information on element assertions, finding elements within the Shadow DOM and more in the WebdriverIO documentation.