title |
---|
Test coverage |
Test coverage is the practice of measuring whether existing tests fully cover your code. That means surfacing areas which aren't currently being tested, such as: conditions, logic branches, functions and variables.
Coverage tests examine the instrumented code against a set of industry-accepted best practices. They act as the last line of QA to improve the quality of your test suite.
Storybook provides an official test coverage addon. Powered by Istanbul, which allows out-of-the-box code instrumentation for the most commonly used frameworks and builders in the JavaScript ecosystem.
Engineered to work alongside modern testing tools (e.g., Playwright), the coverage addon automatically instruments your code and generates code coverage data. For an optimal experience, we recommend using the test runner alongside the coverage addon to run your tests.
Run the following command to install the addon.
<CodeSnippets paths={[ 'common/storybook-coverage-addon-install.yarn.js.mdx', 'common/storybook-coverage-addon-install.npm.js.mdx', 'common/storybook-coverage-addon-install.pnpm.js.mdx', ]} />
Update your Storybook configuration (in .storybook/main.js|ts
) to include the coverage addon.
<CodeSnippets paths={[ 'common/storybook-coverage-addon-registration.js.mdx', 'common/storybook-coverage-addon-registration.ts.mdx', ]} />
Start your Storybook with:
<CodeSnippets paths={[ 'common/storybook-run-dev.yarn.js.mdx', 'common/storybook-run-dev.npm.js.mdx', 'common/storybook-run-dev.pnpm.js.mdx', ]} />
Finally, open a new terminal window and run the test-runner with:
<CodeSnippets paths={[ 'common/storybook-test-runner-coverage.yarn.js.mdx', 'common/storybook-test-runner-coverage.npm.js.mdx', 'common/storybook-test-runner-coverage.pnpm.js.mdx', ]} />
By default, the @storybook/addon-coverage
offers zero-config support for Storybook and instruments your code via babel-plugin-istanbul
for Babel, or vite-plugin-istanbul
for Vite. However, you can extend your Storybook configuration file (i.e., .storybook/main.js|ts
) and provide additional options to the addon. Listed below are the available options and examples of how to use them.
<CodeSnippets paths={[ 'common/storybook-coverage-addon-config-options.js.mdx', 'common/storybook-coverage-addon-config-options.ts.mdx', ]} />
Option | Description | Plugin |
---|---|---|
cwd |
Defines the current working directory options: { istanbul: { cwd: process.cwd(),}} |
Babel, Vite |
include |
Select the files to collect coverage options: { istanbul: { include: ['**/stories/**'],}} |
Babel, Vite |
exclude |
Select the files to exclude from coverage options: { istanbul: { exclude: ['**/stories/**'],}} |
Babel, Vite |
extension |
Sets additional file extensions for coverage options: { istanbul: { extension: ['.js', '.cjs', '.mjs'],}} |
Babel, Vite |
nycrcPath |
Defines the relative path for the existing nyc configuration file options: { istanbul: { nycrcPath: '../nyc.config.js',}} |
Babel, Vite |
excludeNodeModules |
Disables node_modules directory introspection options: { istanbul: { excludeNodeModules:false,}} |
Babel |
ignoreClassMethods |
Configures a set of method names to ignore from being collected options: { istanbul: { ignoreClassMethods: ['example', 'myMethod'],}} |
Babel |
useInlineSourceMaps |
Enables coverage collection on source maps options: { istanbul: { useInlineSourceMaps: false,}} |
Babel |
inputSourceMap |
Sets the value to store the source map. Useful for instrumenting code programmatically options: { istanbul: { inputSourceMap: sourceMap,}} |
Babel |
onCover |
Hook to monitor coverage collection for all tests options: { istanbul: { onCover: (fileName, fileCoverage) => {},}} |
Babel |
requireEnv |
Overrides the VITE_COVERAGE environment variable's value by granting access to the env variables options: { istanbul: { requireEnv: true,}} |
Vite |
cypress |
Replaces the VITE_COVERAGE environment variable with CYPRESS_COVERAGE . Requires Cypress options: { istanbul: { cypress: true,}} |
Vite |
checkProd |
Configures the plugin to skip instrumentation in production environments options: { istanbul: { checkProd: true,}} |
Vite |
forceBuildInstrument |
Configures the plugin to add instrumentation in build mode options: { istanbul: { forceBuildInstrument: true,}} |
Vite |
Out of the box, code coverage tests work seamlessly with Storybook's test-runner and the @storybook/addon-coverage
. However, that doesn't mean you can't use additional reporting tools (e.g., Codecov). For instance, if you're working with LCOV, you can use the generated output (in coverage/storybook/coverage-storybook.json
) and create your own report with:
<CodeSnippets paths={[ 'common/storybook-coverage-report-lcov.js.mdx', ]} />
If you intend on running coverage tests in frameworks with special files like Vue or Svelte, you'll need to adjust your configuration and enable the required file extensions. For example, if you're using Vue, you'll need to add the following to your nyc configuration file (i.e., .nycrc.json
or nyc.config.js
):
<CodeSnippets paths={[ 'common/storybook-coverage-report-vue.json.mdx', 'common/storybook-coverage-report-vue.js.mdx', ]} />
As the coverage addon is based on Babel and Vite plugins for code instrumentation, frameworks that don't rely upon these libraries (e.g., Angular configured with Webpack), will require additional configuration to enable code instrumentation. In that case, you can refer to the following repository for more information.
- Test runner to automate test execution
- Visual tests for appearance
- Accessibility tests for accessibility
- Interaction tests for user behavior simulation
- Coverage tests for measuring code coverage
- Snapshot tests for rendering errors and warnings
- Import stories in other tests for other tools