![Meet Socket at BlackHat and DEF CON in Las Vegas](https://cdn.sanity.io/images/cgdhsj6q/production/4a3876139ffd3878bb3e7800a14cf4044245dca7-1080x834.jpg?w=400&fit=max&auto=format)
Security News
Meet Socket at BlackHat and DEF CON in Las Vegas
Come meet the Socket team at BlackHat and DEF CON! We're sponsoring some fun networking events and we would love to see you there.
@storybook/addon-jest
Advanced tools
Readme
Brings Jest results in storybook.
Checkout the above Live Storybook.
npm install --save-dev @storybook/addon-jest
or
yarn add --dev @storybook/addon-jest
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)
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 addon at .storybook/addons.js
import '@storybook/addon-jest/register';
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 '@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 '@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>
));
((\\.specs?)|(\\.tests?))?(\\.js)?$
. That mean it will match: MyComponent.js, MyComponent.test.js, MyComponent.tests.js, MyComponent.spec.js, MyComponent.specs.js...Every ideas and contributions are welcomed.
MIT © 2017-present Renaud Tertrais
FAQs
React storybook addon that show component jest report
The npm package @storybook/addon-jest receives a total of 57,633 weekly downloads. As such, @storybook/addon-jest popularity was classified as popular.
We found that @storybook/addon-jest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Come meet the Socket team at BlackHat and DEF CON! We're sponsoring some fun networking events and we would love to see you there.
Security News
Learn how Socket's 'Non-Existent Author' alert helps safeguard your dependencies by identifying npm packages published by deleted accounts. This is one of the fastest ways to determine if a package may be abandoned.
Security News
In July, the Python Software Foundation mounted a quick response to address a leaked GitHub token, elected new board members, and added more members to the team supporting PSF and PyPI infrastructure.