Component Testing
With WebdriverIOs Browser Runner you can run tests within an actual desktop or mobile browser while using WebdriverIO and the WebDriver protocol to automate and interact what gets rendered on the page. This approach has many advantages compared to other test frameworks that only allow testing against JSDOM.
How does it Work?
The Browser Runner uses Vite to render a test page and initialize a test framework to run your tests in the browser. Currently it only supports Mocha but Jasmine and Cucumber are on the roadmap. This allows to test any kind of components even for projects that don't use Vite.
The Vite server is started by the WebdriverIO testrunner and configured so that you can use all reporter and services as you used to for normal e2e tests. Furthermore it initializes a browser
instance that allows you to access a subset of the WebdriverIO API to interact with the any elements on the page. Similar as e2e tests you can access that instance through the browser
variable attached to the global scope or by importing it from @wdio/globals
depending on how injectGlobals
is set.
WebdriverIO has built-in support for the following frameworks:
- Nuxt: WebdriverIO's testrunner detects a Nuxt application and automatically sets up your project composables and helps mock out the Nuxt backend, read more in the Nuxt docs
- TailwindCSS: WebdriverIO's testrunner detects if you are using TailwindCSS and loads the environment properly into the test page
Setup
To set-up WebdriverIO for unit or component testing in the browser, initiate a new WebdriverIO project via:
npm init wdio@latest ./
# or
yarn create wdio ./
Once the configuration wizard starts, pick browser
for running unit and component testing and choose one of the presets if desired otherwise go with "Other" if you only want to run basic unit tests. You can also configure a custom Vite configuration if you use Vite already in your project. For more information check out all runner options.
Note: WebdriverIO by default will run browser tests in CI headlessly, e.g. a CI
environment variable is set to '1'
or 'true'
. You can manually configure this behavior using the headless
option for the runner.
At the end of this process you should find a wdio.conf.js
that contains various WebdriverIO configurations, including a runner
property, e.g.:
loading...
By defining different capabilities you can run your tests in different browser, in parallel if desired.
Test Harness
It is totally up to you what you want to run in your tests and how you like to render the components. However we recommend to use the Testing Library as utility framework as it provides plugins for various of component frameworks, such as React, Preact, Svelte and Vue. It is very useful for rendering components into the test page and it automatically cleans up these components after every test.
You can mix Testing Library primitives with WebdriverIO commands as you wish, e.g.:
loading...
Note: using render methods from Testing Library helps remove created components between the tests. If you don't use Testing Library ensure to attach your test components to a container that gets cleaned up between tests.