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

@nlabs/storybook-addon-jest

v3.4.3

Published

React storybook addon that show component jest report

Downloads

4

Readme

Storybook addon Jest

Brings Jest results in storybook.

Storybook Jest Addon Demo

Checkout the above Live Storybook.

Getting started

Install

npm install --save-dev @nlabs/storybook-addon-jest

or

yarn add --dev @nlabs/storybook-addon-jest

Jest Configuration

When running Jest, be sure to save the results in a json file:

package.json

"scripts": {
  "test:generate-output": "jest --json --outputFile=jest-test-results.json"
}

You may want to add it the result file to .gitignore, since it's a generated file:

jest-test-results.json

But much like lockfiles and snapshots checking-in generated files can have certain advantages as well. It's up to you. We recommend to do check in the test results file so starting storybook from an clean git clone doesn't require running all tests first, but this can mean you'll experience merge conflicts on this file in the future. (re-generating this file is super easy though, just like lockfiles and snapshots)

Generating the test results

You need to make sure the generated test-restuls file exists before you start storybook. During development you will likely start jest in watch-mode and so the json file will be re-generated every time code or tests change.

npm run test:generate-output -- --watch

This change will then be HMR (hot module reloaded) using webpack and displayed by this addon.

If you want to pre-run jest automaticly during development or a static build, you may need to consider that if your tests fail, the script receives a non-0 exit code and will exit. You could create a prebuild:storybook npm script, which will never fail by appending || true:

"scripts": {
  "test:generate-output": "jest --json --outputFile=.jest-test-results.json || true",
  "test": "jest",
  "prebuild:storybook": "npm run test:generate-output",
  "build:storybook": "build-storybook -c .storybook -o build/",
  "predeploy": "npm run build:storybook",
  "deploy": "gh-pages -d build/",
}

Register

Register addon at .storybook/addons.js

import '@nlabs/storybook-addon-jest/register';

Usage

Assuming that you have created a test files MyComponent.test.js and MyOtherComponent.test.js

In your story.js

import results from '../.jest-test-results.json';
import { withTests } from '@nlabs/storybook-addon-jest';

storiesOf('MyComponent', module)
  .addDecorator(withTests({ results })('MyComponent', 'MyOtherComponent'))
  .add('This story shows test results from MyComponent.test.js and MyOtherComponent.test.js', () => (
    <div>Jest results in storybook</div>
  ));

Or in order to avoid importing .jest-test-results.json in each story, you can create a simple file withTests.js:

import results from '../.jest-test-results.json';
import { withTests } from '@nlabs/storybook-addon-jest';

export default withTests({
  results,
});

Then in your story:

// import your file
import withTests from '.withTests';

storiesOf('MyComponent', module)
  .addDecorator(withTests('MyComponent', 'MyOtherComponent'))
  .add('This story shows test results from MyComponent.test.js and MyOtherComponent.test.js', () => (
    <div>Jest results in storybook</div>
  ));

withTests(options)

  • options.results: [OBJECT] jest output results. mandatory
  • filteExt: [STRING] test file extention. optionnal. This allow you to write "MyComponent" and not "MyComponent.test.js". It will be used as regex to find your file results. Default value is ((\\.specs?)|(\\.tests?))?(\\.js)?$. That mean it will match: MyComponent.js, MyComponent.test.js, MyComponent.tests.js, MyComponent.spec.js, MyComponent.specs.js...

TODO

  • [ ] Add coverage
  • [ ] Display nested test better (describe)
  • [ ] Display the date of the test
  • [ ] Add unit tests
  • [ ] Add linting
  • [ ] Split

Contributing

Every ideas and contributions are welcomed.

Licence

MIT © 2017-present Renaud Tertrais