-
-
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
Add addon jest #2295
Changes from 14 commits
f6df222
1db27cb
b1db164
1433160
812c054
f1f869c
e66e375
3af0b87
f38a027
37d3748
b4c0d31
758c195
d004bd7
3c8c783
1073760
d185d1a
cc1eef2
2d17ece
b42538e
e310ec2
3320ee3
6648656
663d5ae
ab0d84d
d0d0494
045bca1
24b1982
1f2edf0
6dbbb34
793b76e
80bc824
d3ceba4
59fff75
7d95fae
eb33a3e
7206c93
9d83982
70d3424
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# 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": "jest --json --outputFile=.jest-test-results.json" | ||
} | ||
``` | ||
|
||
Add it the result file to `.gitignore`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @renaudtertrais @ndelangen why do we even have this recommendation? Checking this file into VCS makes a lot of sense to me, given that it's used in an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it will likely have tons of merge-conflicts over time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just like lockfiles and test snapshots. It's easy to regenerate them, so this shouldn't be a problem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true I will change the text and make it so users are aware of the trade-offs. |
||
|
||
``` | ||
.jest-test-results.json | ||
``` | ||
|
||
**Known issue**: if you use a deploy script using for example `gh-pages`, be sure to not put | ||
the `test` script that write the result in part of the script process (in `predeploy` for example). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
Instead use a different script: | ||
|
||
```json | ||
"scripts": { | ||
"test:output": "jest --json --outputFile=.jest-test-results.json", | ||
"test": "jest", | ||
"prebuild:storybook": "npm run test", | ||
"build:storybook": "build-storybook -c .storybook -o build/", | ||
"predeploy": "npm run build:storybook", | ||
"deploy": "gh-pages -d build/", | ||
} | ||
``` | ||
|
||
Then in dev use: | ||
|
||
```sh | ||
npm run test:output -- --watch | ||
``` | ||
|
||
When deploying: | ||
|
||
```sh | ||
npm run deploy | ||
``` | ||
|
||
### 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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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"? |
||
import withTests from '@storybook/addon-jest'; | ||
|
||
storiesOf('MyComponent', module) | ||
.addDecorator(withTests(jestTestResults, { filesExt: '.test.js' })('MyComponent', 'MyOtherComponent')); | ||
``` | ||
|
||
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')); | ||
``` | ||
|
||
### Styling | ||
|
||
The panel comes with a basic design. If you want to make it look a bit nicer, you add github markdown style by importing it in `.storybook/addons.js` | ||
|
||
```js | ||
import '@storybook/addon-jest/register'; | ||
import '@storybook/addon-jest/styles'; | ||
``` | ||
|
||
If you already use `storybook-readme` addon, you do not need to import it. | ||
|
||
## 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import '@storybook/addon-options/register'; | ||
// --> import 'storybook-addon-jest/register' | ||
import '../../register'; | ||
// --> import 'storybook-addon-jest/styles' | ||
import '../../dist/styles'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { configure, addDecorator } from '@storybook/react'; | ||
import { setOptions } from '@storybook/addon-options'; | ||
// --> import { setupJestAddon } from 'storybook-addon-jest'; | ||
|
||
|
||
setOptions({ | ||
name: 'JEST ADDON', | ||
url: 'https://github.com/storybooks/storybook', | ||
downPanelInRight: true, | ||
showLeftPanel: true, | ||
}); | ||
|
||
function loadStories() { | ||
require('../List.story'); | ||
} | ||
|
||
configure(loadStories, module); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move the stories to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am on it. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
|
||
const List = ({ items }) => <ul>{items.map(item => <li key={item}>{item}</li>)}</ul>; | ||
|
||
List.defaultProps = { | ||
items: [], | ||
}; | ||
|
||
export default List; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import withTests from './withTests'; | ||
import List from './List'; | ||
|
||
storiesOf('List', module) | ||
.addDecorator(withTests('List')) | ||
.add('3 items', () => ( | ||
<List items={['foo', 'bar']} /> | ||
)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
describe('Description: ', () => { | ||
it('should contain 3 items', () => { | ||
expect(3).toBe(3); | ||
}); | ||
|
||
it('should work fine', () => { | ||
expect(true).toBe(true); | ||
}); | ||
}); | ||
|
||
test('Failing test',() => { | ||
expect(['foo', 'bar', 'baz']).toEqual(['foo', 'bar']); | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// ---> import { withTests } from 'storybook-addon-jest'; | ||
import withTests from '../dist'; | ||
|
||
import jestTestResuls from './.jest-test-results.json'; | ||
|
||
|
||
export default withTests(jestTestResuls, { | ||
filesExt: '.test.js', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@storybook/addon-jest", | ||
"version": "3.2.15", | ||
"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/addons": "^3.2.15", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make it a peerDep from day one |
||
"global": "^4.3.2", | ||
"prop-types": "^15.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-react": "^6.24.1", | ||
"cross-env": "^5.1.1", | ||
"css-loader": "^0.28.7", | ||
"enzyme": "^3.1.1", | ||
"eslint": "^4.10.0", | ||
"eslint-config-airbnb": "^16.1.0", | ||
"eslint-config-prettier": "^2.7.0", | ||
"eslint-import-resolver-webpack": "^0.8.3", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.2", | ||
"eslint-plugin-react": "^7.4.0", | ||
"jest": "^21.2.1", | ||
"react": "^16.1.0", | ||
"react-dom": "^16.1.0", | ||
"style-loader": "^0.19.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "*", | ||
"react-dom": "*" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./dist/register'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
success: 'LIGHTSEAGREEN', | ||
error: 'CRIMSON', | ||
warning: 'DARKORANGE', | ||
grey: 'LIGHTSLATEGRAY', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const Indicator = ({ color, size, children = '', right }) => ( | ||
<div | ||
style={{ | ||
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, | ||
marginLeft: right ? size / 2 : 0, | ||
marginRight: right ? 0 : size / 2, | ||
}} | ||
> | ||
{children} | ||
</div> | ||
); | ||
Indicator.propTypes = { | ||
color: PropTypes.string.isRequired, | ||
size: PropTypes.number.isRequired, | ||
children: PropTypes.node.isRequired, | ||
right: PropTypes.bool.isRequired, | ||
}; | ||
|
||
export default Indicator; |
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.
The link is broken