-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Add addon jest #2295
Merged
Merged
Add addon jest #2295
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
f6df222
initial commit
renaudtertrais 1db27cb
v0.0.1
renaudtertrais b1db164
Add new TODOS item + add know issue when deploying in README
renaudtertrais 1433160
RENAME package & SYNC version with monorepo
ndelangen 812c054
Merge branch 'master' into add-addon-jest
danielduan f1f869c
Remove eslint files and yarn.lock
renaudtertrais e66e375
Merge branch 'master' into add-addon-jest
ndelangen 3af0b87
REMOVE dev setup on package && Linting
ndelangen f38a027
Linting
ndelangen 37d3748
CHANGE jest config to exclude the example (failing) jest test for add…
ndelangen b4c0d31
lockfile
ndelangen 758c195
Merge branch 'master' into add-addon-jest
ndelangen d004bd7
Merge branch 'master' into add-addon-jest
ndelangen 3c8c783
FIX review comment
ndelangen 1073760
Merge branch 'master' into add-addon-jest
danielduan d185d1a
Add addon-jest to examples/cra-kitchen-sink
renaudtertrais cc1eef2
Merge branch 'master' into add-addon-jest
ndelangen 2d17ece
WIP
ndelangen b42538e
IMPROVE parsing of failureMessage
ndelangen e310ec2
IMPROVE styling & parsing of failureMessage
ndelangen 3320ee3
Merge branch 'master' into add-addon-jest
Hypnosphi 6648656
IMPROVE jest failureMessages
ndelangen 663d5ae
Merge branch 'add-addon-jest' of github.com:storybooks/storybook into…
ndelangen ab0d84d
IMRPOVE styling
ndelangen d0d0494
Merge branch 'master' into add-addon-jest
Hypnosphi 045bca1
REFACTOR to glamorous
ndelangen 24b1982
no message
ndelangen 1f2edf0
FIX links in readme
ndelangen 6dbbb34
IMPROVE README
ndelangen 793b76e
CLEANUP: remove unneeded component
ndelangen 80bc824
IMPROVE readme
ndelangen d3ceba4
Merge branch 'master' into add-addon-jest
ndelangen 59fff75
DELETE unneeded file
ndelangen 7d95fae
Merge branch 'master' into add-addon-jest
ndelangen eb33a3e
CHANGE @storybook/addons to peerDependency && update versions
ndelangen 7206c93
UPDATE snapshots
ndelangen 9d83982
UPDATE snapshots
ndelangen 70d3424
Merge branch 'master' into add-addon-jest
ndelangen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Storybook addon Jest | ||
|
||
Brings Jest results in storybook. | ||
|
||
[![Storybook Jest Addon Demo](@storybook/addon-jest.gif)](https://storybook-addon-jest-example.herokuapp.com/) | ||
|
||
> Checkout the above [Live Storybook](https://storybook-addon-jest-example.herokuapp.com/). | ||
|
||
## Getting started | ||
|
||
### Install | ||
|
||
`npm install --save-dev @storybook/addon-jest` | ||
|
||
or | ||
|
||
`yarn add --dev @storybook/addon-jest` | ||
|
||
### Jest Configuration | ||
|
||
When running **Jest**, be sure to save the results in a json file: | ||
|
||
`package.json` | ||
|
||
```js | ||
"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. | ||
|
||
```sh | ||
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`: | ||
```json | ||
"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` | ||
|
||
```js | ||
import '@storybook/addon-jest/register'; | ||
``` | ||
|
||
## Usage | ||
|
||
Assuming that you have created a test files `MyComponent.test.js` and `MyOtherComponent.test.js` | ||
|
||
In your `story.js` | ||
|
||
```js | ||
import jestTestResults from '../.jest-test-results.json'; | ||
import withTests from '@storybook/addon-jest'; | ||
|
||
storiesOf('MyComponent', module) | ||
.addDecorator(withTests(jestTestResults, { filesExt: '.test.js' })('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`: | ||
|
||
```js | ||
import jestTestResults from '../.jest-test-results.json'; | ||
import withTests from '@storybook/addon-jest'; | ||
|
||
export default withTests(jestTestResults, { | ||
filesExt: '.test.js', | ||
}); | ||
``` | ||
|
||
Then in your story: | ||
|
||
```js | ||
// 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> | ||
)); | ||
``` | ||
|
||
## TODO | ||
|
||
- [ ] Add coverage | ||
- [ ] Display nested test better (describe) | ||
- [ ] Display the date of the test | ||
- [ ] Add unit tests | ||
- [ ] Add linting | ||
- [ ] Split <TestPanel /> | ||
|
||
## Contributing | ||
|
||
Every ideas and contributions are welcomed. | ||
|
||
## Licence | ||
|
||
MIT © 2017-present Renaud Tertrais |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@storybook/addon-jest", | ||
"version": "3.2.16", | ||
"description": "React storybook addon that show component jest report", | ||
"keywords": [ | ||
"addon", | ||
"jest", | ||
"react", | ||
"report", | ||
"results", | ||
"storybook", | ||
"unit-testing" | ||
], | ||
"homepage": "https://storybook.js.org", | ||
"bugs": "https://github.com/storybooks/storybook", | ||
"license": "MIT", | ||
"author": "Renaud Tertrais <[email protected]> (https://github.com/renaudtertrais)", | ||
"main": "dist/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/storybooks/storybook" | ||
}, | ||
"scripts": { | ||
"prepare": "node ../../scripts/prepare.js" | ||
}, | ||
"dependencies": { | ||
"@storybook/components": "^3.2.16", | ||
"global": "^4.3.2", | ||
"prop-types": "^15.6.0", | ||
"glamor": "^2.20.40", | ||
"glamorous": "^4.11.0" | ||
}, | ||
"devDependencies": { | ||
"react": "^16.1.0", | ||
"react-dom": "^16.1.0" | ||
}, | ||
"peerDependencies": { | ||
"@storybook/addons": "^3.2.16", | ||
"react": "*", | ||
"react-dom": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./dist/register'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
success: 'LIGHTSEAGREEN', | ||
error: 'CRIMSON', | ||
warning: 'DARKORANGE', | ||
grey: 'LIGHTSLATEGRAY', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import PropTypes from 'prop-types'; | ||
|
||
import glamorous from 'glamorous'; | ||
|
||
const Indicator = glamorous.div( | ||
({ color, size }) => ({ | ||
boxSizing: 'border-box', | ||
padding: `0 ${size / 2}px`, | ||
minWidth: size, | ||
minHeight: size, | ||
fontSize: size / 1.4, | ||
lineHeight: `${size}px`, | ||
color: 'white', | ||
textTransform: 'uppercase', | ||
borderRadius: size / 2, | ||
backgroundColor: color, | ||
}), | ||
({ styles }) => ({ | ||
...styles, | ||
}) | ||
); | ||
|
||
Indicator.defaultProps = { | ||
right: false, | ||
children: '', | ||
}; | ||
|
||
Indicator.propTypes = { | ||
color: PropTypes.string.isRequired, | ||
size: PropTypes.number.isRequired, | ||
children: PropTypes.node, | ||
right: PropTypes.bool, | ||
}; | ||
|
||
export default Indicator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import glamorous from 'glamorous'; | ||
|
||
import { baseFonts } from '@storybook/components'; | ||
|
||
import Indicator from './Indicator'; | ||
import Result, { FailedResult } from './Result'; | ||
import provideJestResult from '../hoc/provideJestResult'; | ||
import colors from '../colors'; | ||
|
||
const List = glamorous.ul({ | ||
listStyle: 'none', | ||
fontSize: 14, | ||
padding: 0, | ||
margin: '10px 0', | ||
}); | ||
|
||
const Item = glamorous.li({ | ||
display: 'block', | ||
margin: '10px 0', | ||
padding: 0, | ||
}); | ||
|
||
const NoTests = glamorous.div({ | ||
padding: '10px 20px', | ||
flex: 1, | ||
...baseFonts, | ||
}); | ||
|
||
const FileTitle = glamorous.h2({ | ||
margin: 0, | ||
fontWeight: 500, | ||
fontSize: 18, | ||
}); | ||
|
||
const SuiteHead = glamorous.div({ | ||
display: 'flex', | ||
alignItems: 'baseline', | ||
justifyContent: 'space-between', | ||
position: 'relative', | ||
paddingTop: 10, | ||
}); | ||
|
||
const SuiteTotals = glamorous(({ successNumber, failedNumber, result, className }) => ( | ||
<div className={className}> | ||
{successNumber > 0 && <div style={{ color: colors.success }}>{successNumber} passed</div>} | ||
{failedNumber > 0 && <div style={{ color: colors.error }}>{failedNumber} failed</div>} | ||
<div>{result.assertionResults.length} total</div> | ||
<div> | ||
<strong>{result.endTime - result.startTime}ms</strong> | ||
</div> | ||
</div> | ||
))({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
color: colors.grey, | ||
fontSize: '10px', | ||
|
||
'& > *': { | ||
marginLeft: 10, | ||
}, | ||
}); | ||
|
||
const SuiteProgress = glamorous(({ successNumber, result, className }) => ( | ||
<div className={className} role="progressbar"> | ||
<span style={{ width: `${successNumber / result.assertionResults.length * 100}%` }}> | ||
{`${successNumber / result.assertionResults.length * 100}%`} | ||
</span> | ||
</div> | ||
))(() => ({ | ||
width: '100%', | ||
backgroundColor: colors.error, | ||
height: 4, | ||
top: 0, | ||
position: 'absolute', | ||
left: 0, | ||
borderRadius: 3, | ||
overflow: 'hidden', | ||
appearance: 'none', | ||
|
||
'& > span': { | ||
backgroundColor: colors.success, | ||
bottom: 0, | ||
position: 'absolute', | ||
left: 0, | ||
top: 0, | ||
boxShadow: '4px 0 0 white', | ||
}, | ||
})); | ||
|
||
const SuiteTitle = glamorous.div({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
}); | ||
|
||
const Content = glamorous(({ tests, className }) => ( | ||
<div className={className}> | ||
{tests.map(({ name, result }) => { | ||
if (!result) { | ||
return <NoTests>This story has tests configures, but no file not found</NoTests>; | ||
} | ||
|
||
const successNumber = result.assertionResults.filter(({ status }) => status === 'passed') | ||
.length; | ||
const failedNumber = result.assertionResults.length - successNumber; | ||
|
||
return ( | ||
<section key={name}> | ||
<SuiteHead> | ||
<SuiteTitle> | ||
<Indicator | ||
color={result.status === 'passed' ? colors.success : colors.error} | ||
size={16} | ||
styles={{ marginRight: 5 }} | ||
> | ||
{result.status} | ||
</Indicator> | ||
<FileTitle>{name}</FileTitle> | ||
</SuiteTitle> | ||
<SuiteTotals {...{ successNumber, failedNumber, result }} /> | ||
<SuiteProgress {...{ successNumber, failedNumber, result }} /> | ||
</SuiteHead> | ||
<List> | ||
{result.assertionResults.map(res => ( | ||
<Item key={res.fullName || res.title}> | ||
{res.failureMessages && res.failureMessages.length ? ( | ||
<FailedResult {...res} /> | ||
) : ( | ||
<Result {...res} /> | ||
)} | ||
</Item> | ||
))} | ||
</List> | ||
</section> | ||
); | ||
})} | ||
</div> | ||
))({ | ||
padding: '10px 20px', | ||
flex: '1 1 0%', | ||
...baseFonts, | ||
}); | ||
|
||
const Panel = ({ tests }) => | ||
tests ? <Content tests={tests} /> : <NoTests>This story has no tests configures</NoTests>; | ||
|
||
Panel.defaultProps = { | ||
tests: null, | ||
}; | ||
|
||
Panel.propTypes = { | ||
tests: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
result: PropTypes.object, | ||
}) | ||
), | ||
}; | ||
|
||
export default provideJestResult(Panel); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean that everybody has to run the tests locally before ever starting storybook?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, edited the readme to make this clear.
For demo purposes I've added the testresults-file to source control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly does it demonstrate? Users are not supposed to create those files manually, are they?
Or do you mean "to make building our demos possible"?