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

Add addon jest #2295

Merged
merged 38 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f6df222
initial commit
renaudtertrais Nov 9, 2017
1db27cb
v0.0.1
renaudtertrais Nov 9, 2017
b1db164
Add new TODOS item + add know issue when deploying in README
renaudtertrais Nov 10, 2017
1433160
RENAME package & SYNC version with monorepo
ndelangen Nov 11, 2017
812c054
Merge branch 'master' into add-addon-jest
danielduan Nov 12, 2017
f1f869c
Remove eslint files and yarn.lock
renaudtertrais Nov 12, 2017
e66e375
Merge branch 'master' into add-addon-jest
ndelangen Nov 13, 2017
3af0b87
REMOVE dev setup on package && Linting
ndelangen Nov 13, 2017
f38a027
Linting
ndelangen Nov 13, 2017
37d3748
CHANGE jest config to exclude the example (failing) jest test for add…
ndelangen Nov 13, 2017
b4c0d31
lockfile
ndelangen Nov 14, 2017
758c195
Merge branch 'master' into add-addon-jest
ndelangen Nov 14, 2017
d004bd7
Merge branch 'master' into add-addon-jest
ndelangen Nov 14, 2017
3c8c783
FIX review comment
ndelangen Nov 14, 2017
1073760
Merge branch 'master' into add-addon-jest
danielduan Nov 14, 2017
d185d1a
Add addon-jest to examples/cra-kitchen-sink
renaudtertrais Nov 14, 2017
cc1eef2
Merge branch 'master' into add-addon-jest
ndelangen Nov 16, 2017
2d17ece
WIP
ndelangen Nov 16, 2017
b42538e
IMPROVE parsing of failureMessage
ndelangen Nov 17, 2017
e310ec2
IMPROVE styling & parsing of failureMessage
ndelangen Nov 18, 2017
3320ee3
Merge branch 'master' into add-addon-jest
Hypnosphi Nov 18, 2017
6648656
IMPROVE jest failureMessages
ndelangen Nov 18, 2017
663d5ae
Merge branch 'add-addon-jest' of github.com:storybooks/storybook into…
ndelangen Nov 18, 2017
ab0d84d
IMRPOVE styling
ndelangen Nov 18, 2017
d0d0494
Merge branch 'master' into add-addon-jest
Hypnosphi Nov 18, 2017
045bca1
REFACTOR to glamorous
ndelangen Nov 18, 2017
24b1982
no message
ndelangen Nov 18, 2017
1f2edf0
FIX links in readme
ndelangen Nov 19, 2017
6dbbb34
IMPROVE README
ndelangen Nov 19, 2017
793b76e
CLEANUP: remove unneeded component
ndelangen Nov 19, 2017
80bc824
IMPROVE readme
ndelangen Nov 21, 2017
d3ceba4
Merge branch 'master' into add-addon-jest
ndelangen Nov 21, 2017
59fff75
DELETE unneeded file
ndelangen Nov 21, 2017
7d95fae
Merge branch 'master' into add-addon-jest
ndelangen Nov 23, 2017
eb33a3e
CHANGE @storybook/addons to peerDependency && update versions
ndelangen Nov 23, 2017
7206c93
UPDATE snapshots
ndelangen Nov 23, 2017
9d83982
UPDATE snapshots
ndelangen Nov 23, 2017
70d3424
Merge branch 'master' into add-addon-jest
ndelangen Nov 23, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions addons/jest/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
["env", {
"targets": {
"browsers": ["last 3 versions", "IE >= 11"]
}
}],
"react"
]
}
2 changes: 2 additions & 0 deletions addons/jest/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Copy link
Member

Choose a reason for hiding this comment

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

Those should be covered by root .eslintignore file

37 changes: 37 additions & 0 deletions addons/jest/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
"extends": ["airbnb", "prettier"],
plugins: ["react"],
rules: {
"arrow-parens": ['error', 'as-needed'],
"comma-dangle": [1, {
"arrays": 'always-multiline',
"objects": 'always-multiline',
"imports": 'always-multiline',
"exports": 'always-multiline',
"functions": "ignore",
}],
"import/prefer-default-export": 0,
"react/forbid-prop-types": 0,
"react/no-danger": 0,
"react/jsx-no-target-blank": 0,
"react/require-default-props": 0,
"import/no-extraneous-dependencies": 2,
"react/jsx-filename-extension": 0,
"jsx-a11y/href-no-hash": 0,
},
"env": {
"browser": true,
"es6": true,
"commonjs": true,
"node": true,
"jest": true,
},
"settings": {
"import/resolver": {
"webpack": {
config: 'webpack-development.config.js'
},
},
},
};

Copy link
Member

Choose a reason for hiding this comment

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

Let's try to use our root eslint config for this package, and override only the things that really need it, if any

5 changes: 5 additions & 0 deletions addons/jest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.DS_Store
dist
.jest-test-results.json
build
133 changes: 133 additions & 0 deletions addons/jest/README.md
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/)
Copy link
Member

Choose a reason for hiding this comment

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

The link is broken


> 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`:
Copy link
Member

@Hypnosphi Hypnosphi Nov 19, 2017

Choose a reason for hiding this comment

The 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 import. What's the downside?

Copy link
Member

Choose a reason for hiding this comment

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

Well, it will likely have tons of merge-conflicts over time?

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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).
Copy link
Member

Choose a reason for hiding this comment

The 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';
Copy link
Member

@Hypnosphi Hypnosphi Nov 18, 2017

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?

Copy link
Member

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.

Copy link
Member

@Hypnosphi Hypnosphi Nov 19, 2017

Choose a reason for hiding this comment

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

For demo purposes

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
Binary file added addons/jest/docs/storybook-addon-jest.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions addons/jest/example/.storybook/addons.js
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';

17 changes: 17 additions & 0 deletions addons/jest/example/.storybook/config.js
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);
Copy link
Member

Choose a reason for hiding this comment

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

Let's move the stories to example/cra-kitchen-sink

Copy link
Member

Choose a reason for hiding this comment

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

agreed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am on it.

9 changes: 9 additions & 0 deletions addons/jest/example/List.js
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;
12 changes: 12 additions & 0 deletions addons/jest/example/List.story.js
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']} />
));
13 changes: 13 additions & 0 deletions addons/jest/example/List.test.js
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']);
})
9 changes: 9 additions & 0 deletions addons/jest/example/withTests.js
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',
});
71 changes: 71 additions & 0 deletions addons/jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@storybook/addon-jest",
"version": "3.2.14",
"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 <renaud.tertrais@gmail.com> (https://github.com/renaudtertrais)",
"files": [
"dist",
"styles.js",
"register.js"
],
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/storybooks/storybook"
},
"scripts": {
"prebuild": "npm run clear",
"build": "cross-env NODE_ENV=production babel -d ./dist ./src",
Copy link
Member

Choose a reason for hiding this comment

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

We need a prepare script instead (see other addons). All the babel-related stuff can be removed from package as we use top-level babel dependencies and setup for transpiling everything

"prebuild:storybook": "npm run clean:storybook",
"build:storybook": "build-storybook -c example/.storybook -o build/",
"clean:storybook": "rm -rf build",
"clear": "rm -rf ./dist",
"predeploy": "npm run build:storybook",
"deploy": "gh-pages -d build",
"prepublish": "npm run build",
"storybook": "cross-env NODE_ENV=development start-storybook -p 3001 -c example/.storybook",
"test:example": "jest example --json --outputFile=example/.jest-test-results.json"
},
"dependencies": {},
"devDependencies": {
"@storybook/addon-options": "^3.2.14",
"@storybook/react": "^3.2.14",
"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",
"gh-pages": "^1.0.0",
"jest": "^21.2.1",
"react": "^16.1.0",
"react-dom": "^16.1.0",
"style-loader": "^0.19.0"
},
"peerDependencies": {
"@storybook/addons": "^3.2.14",
"prop-types": "^15.6.0",
Copy link
Member

Choose a reason for hiding this comment

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

It should be a simple dependency, not a peer one

Copy link
Member

Choose a reason for hiding this comment

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

I actually meant the prop-types package. It's OK for an end user not to have it

"react": "^16.0.0",
"react-dom": "^16.0.0"
}
}
1 change: 1 addition & 0 deletions addons/jest/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./dist/register');
6 changes: 6 additions & 0 deletions addons/jest/src/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
success: 'LIGHTSEAGREEN',
error: 'CRIMSON',
warning: 'DARKORANGE',
grey: 'LIGHTSLATEGRAY',
}
24 changes: 24 additions & 0 deletions addons/jest/src/components/Indicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';

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>
);

export default Indicator;
Loading