Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support code coverage #120

Merged
merged 6 commits into from
Jun 21, 2022
Merged

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jun 13, 2022

Issue: #116

Release Notes

Support code coverage (#120)

Features

Setting up code coverage

The test runner supports code coverage with the --coverage flag or STORYBOOK_COLLECT_COVERAGE environment variable. The pre-requisite is that your components are instrumented using istanbul.

Instrument the code

Given that your components' code runs in the context of a real browser, they have to be instrumented so that the test runner is able to collect coverage. In order to do so, you have to setup the instrumentation yourself.

Install the istanbul babel plugin:

yarn add -D babel-plugin-istanbul

Storybook allows code transpilation with babel out of the box by configuring the babel function in your main.js. Add the istanbul plugin:

// .storybook/main.js
module.exports = {
  // ...rest of your code here
  babel: async (options) => {
    options.plugins.push([
      'istanbul',
      {
        // provide include patterns if you like
        include: ['src/components/**'],
        // provide exclude patterns if you like
        exclude: [
          '**/*.d.ts',
          '**/*{.,-}{spec,stories,types}.{js,jsx,ts,tsx}',
        ],
      },
    ]);

    return options;
  },
};

The babel plugin has default options that might suffice to your project, however if you want to know which options are taken into account you can check them here.

Run tests with --coverage flag

After setting up instrumentation, run Storybook then run the test-runner with --coverage:

yarn test-storybook --coverage

The test runner will report the results in the CLI and generate a .nyc_output/coverage.json file which can be used by nyc.

📦 Published PR as canary version: 0.2.0--canary.120.86b6b43.0

✨ Test out this PR locally via:

npm install @storybook/test-runner@0.2.0--canary.120.86b6b43.0
# or 
yarn add @storybook/test-runner@0.2.0--canary.120.86b6b43.0

Version

Published prerelease version: v0.3.0-next.0

Changelog

Release Notes

Support code coverage (#120)

Support code coverage (#120)


🚀 Enhancement

Authors: 1

@yannbf yannbf added the patch Increment the patch version when merged label Jun 13, 2022
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

bin/test-storybook.js Outdated Show resolved Hide resolved
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also shouldn't this be a minor release?

@yannbf yannbf added minor Increment the minor version when merged and removed patch Increment the patch version when merged labels Jun 13, 2022
@yannbf yannbf requested a review from shilman June 13, 2022 17:22
@yannbf yannbf force-pushed the yann/sb-382-test-runner-test-coverage-support branch from 8ae5160 to 86b6b43 Compare June 21, 2022 08:45
@yannbf yannbf merged commit bbe74ba into next Jun 21, 2022
@yannbf yannbf mentioned this pull request Jun 24, 2022
@yannbf yannbf deleted the yann/sb-382-test-runner-test-coverage-support branch November 18, 2022 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants