npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@storybook/addon-coverage

v1.0.4

Published

Tools to support code coverage in Storybook

Downloads

620,777

Readme

Storybook Addon Coverage

Tools to support code coverage in Storybook and the Storybook test runner. It supports Storybook projects that use Webpack5 or Vite.

Installation

Install this addon by adding the @storybook/addon-coverage dependency:

yarn add -D @storybook/addon-coverage

And by registering it in your .storybook/main.js:

export default {
  addons: ["@storybook/addon-coverage"],
};

Configuring the addon

This addon instruments your code by using a custom wrapper around istanbul-lib-instrument if your project uses Webpack5 or vite-plugin-istanbul if your project uses Vite. It provides some default configuration, but if you want to add yours, you can do so by setting the options in your .storybook/main.js:

export default {
  addons: [
    {
      name: "@storybook/addon-coverage",
      options: {
        istanbul: {
          include: ["**/stories/**"],
        },
      },
    },
  ],
};

The available options if your project uses Webpack5 are as follows:

| Option name | Description | Type | Default | | ---------------------- | -------------------------------------------------------------------------------------------------------------- | --------------- | -------------------------------------------------------------------------------------------- | | cwd | Set the working directory | String | process.cwd() | | nycrcPath | Path to specific nyc config to use instead of automatically searching for a nycconfig. | string | - | | include | Glob pattern to include files. It has precedence over the include definition from your nyc config | Array<String> | - | | exclude | Glob pattern to exclude files. It has precedence over the exclude definition from your nyc config | Array<String> | defaultExclude in https://github.com/storybookjs/addon-coverage/blob/main/src/constants.ts | | extension | List of supported extensions. It has precedence over the extension definition from your nyc config | Array<String> | ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte] | | coverageVariable | The global variable name that Istanbul will use to store coverage results. | string | - | | preserveComments | Indicates whether comments in the code should be preserved during the instrumentation process. | boolean | true | | compact | Controls whether the output of instrumented code is compacted. Useful for debugging when set to false. | boolean | false | | esModules | Determines whether the code to be instrumented uses ES Module syntax. | boolean | true | | autoWrap | When set to true, wraps program code in a function to enable top-level return statements. | boolean | true | | produceSourceMap | If true, instructs Istanbul to produce a source map for the instrumented code. | boolean | true | | sourceMapUrlCallback | A callback function that gets invoked with the filename and the source map URL when a source map is generated. | function | - | | debug | Enables the debug mode, providing additional logging information during the instrumentation process. | boolean | - |

Note: If you're using TypeScript, you can import the type for the options like so:

import type { AddonOptionsWebpack } from "@storybook/addon-coverage";

The available options if your project uses Vite are as follows:

| Option name | Description | Type | Default | | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- | -------------------------------------------------------------------------------- | | cwd | Set the working directory | String | process.cwd() | | include | See here for more info | Array<String> or string | ['**'] | | exclude | See here for more info | Array<String> or string | list | | extension | List of extensions that nyc should attempt to handle in addition to .js | Array<String> or string | ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte] | | requireEnv | Optional boolean to require the environment variable (defaults to VITE_COVERAGE) to equal true in order to instrument the code. Otherwise it will instrument even if env variable is not set. However if requireEnv is not set the instrumentation will stop if the environment variable is equal to false. | boolean | - | | cypress | Optional boolean to change the environment variable to CYPRESS_COVERAGE instead of VITE_COVERAGE. For ease of use with @cypress/code-coverage coverage | boolean | - | | checkProd | Optional boolean to enforce the plugin to skip instrumentation for production environments. Looks at Vite's isProduction key from the ResolvedConfig. | boolean | - | | forceBuildInstrument | Optional boolean to enforce the plugin to add instrumentation in build mode. | boolean | false | | nycrcPath | Path to specific nyc config to use instead of automatically searching for a nycconfig. This parameter is just passed down to @istanbuljs/load-nyc-config. | string | - |

Note: If you're using TypeScript, you can import the type for the options like so:

import type { AddonOptionsVite } from "@storybook/addon-coverage";

Troubleshooting

The coverage addon doesn't support optimized builds

The --test flag is designed to be as fast as possible, removing addons known to slow down the build and are not needed for functional testing. One of these addons is @storybook/addon-coverage, which is used in conjunction with the Storybook Test runner to collect coverage information for your stories.

If you are using addon-coverage AND running the test runner against your built Storybook, the --test flag will strip out the coverage information. To configure the --test build to keep coverage information (at the expense of a slightly slower build), update your Storybook configuration file (i.e.,.storybook/main.js|ts) and include the disabledAddons option.

// main.js

export default {
  // Your Storybook configuration goes here
  build: {
    test: {
      disabledAddons: [
        '@storybook/addon-docs',
        '@storybook/addon-essentials/docs',
      ],
    },
  },
}

Development scripts

  • yarn start runs babel in watch mode
  • yarn build build and package your addon code

To run the examples, choose one of the projects in the examples directory then run:

  • yarn to install dependencies and link the addon locally
  • yarn storybook to run Storybook
  • yarn test-storybook --coverage to test coverage report generation

License

MIT