UNPKG

5.66 kBMarkdownView Raw
1# Storybook addon Jest
2
3Brings Jest results in storybook.
4
5[Framework Support](https://github.com/storybookjs/storybook/blob/master/ADDONS_SUPPORT.md)
6
7[![Storybook Jest Addon Demo](https://raw.githubusercontent.com/storybookjs/storybook/next/addons/jest/docs/storybook-addon-jest.gif)](http://storybooks-official.netlify.com/?selectedKind=Addons%7Cjest&selectedStory=withTests&full=0&addons=1&stories=1&panelRight=0&addonPanel=storybook%2Ftests%2Fpanel)
8
9> Checkout the above [Live Storybook](http://storybooks-official.netlify.com/?selectedKind=Addons%7Cjest&selectedStory=withTests&full=0&addons=1&stories=1&panelRight=0&addonPanel=storybook%2Ftests%2Fpanel).
10
11## Getting started
12
13### Install
14
15`npm install --save-dev @storybook/addon-jest`
16
17or
18
19`yarn add --dev @storybook/addon-jest`
20
21### Jest Configuration
22
23When running **Jest**, be sure to save the results in a json file:
24
25`package.json`
26
27```js
28"scripts": {
29 "test:generate-output": "jest --json --outputFile=.jest-test-results.json"
30}
31```
32
33You may want to add it the result file to `.gitignore`, since it's a generated file:
34
35```
36.jest-test-results.json
37```
38
39But much like lockfiles and snapshots checking-in generated files can have certain advantages as well. It's up to you.
40We recommend to **do** check in the test results file so starting storybook from an clean git clone doesn't require running all tests first,
41but this can mean you'll experience merge conflicts on this file in the future. (_re-generating this file is very similar to re-generating lockfiles and snapshots_)
42
43## Generating the test results
44
45You need to make sure the generated test-results file exists before you start storybook.
46During development you will likely start jest in watch-mode
47and so the json file will be re-generated every time code or tests change.
48
49```sh
50npm run test:generate-output -- --watch
51```
52
53This change will then be HMR (hot module reloaded) using webpack and displayed by this addon.
54
55If you want to pre-run jest automatically during development or a static build,
56you may need to consider that if your tests fail, the script receives a non-0 exit code and will exit.
57You could create a `prebuild:storybook` npm script, which will never fail by appending `|| true`:
58
59```json
60"scripts": {
61 "test:generate-output": "jest --json --outputFile=.jest-test-results.json || true",
62 "test": "jest",
63 "prebuild:storybook": "npm run test:generate-output",
64 "build:storybook": "build-storybook -c .storybook -o build/",
65 "predeploy": "npm run build:storybook",
66 "deploy": "gh-pages -d build/",
67}
68```
69
70### Register
71
72within `.storybook/main.js`:
73
74```js
75module.exports = {
76 addons: ['@storybook/addon-jest'],
77};
78```
79
80## Usage
81
82Assuming that you have created test files `MyComponent.test.js` and `MyOtherComponent.test.js`
83
84In your `story.js`
85
86```js
87import results from '../.jest-test-results.json';
88import { withTests } from '@storybook/addon-jest';
89
90export default {
91 title: 'MyComponent',
92 decorators: [withTests({ results })],
93};
94
95export const defaultView = () => <div>Jest results in storybook</div>;
96defaultView.parameters = {
97 jest: ['MyComponent.test.js', 'MyOtherComponent.test.js'],
98};
99```
100
101Or in order to avoid importing `.jest-test-results.json` in each story, add the decorator in your `.storybook/preview.js` and results will display for stories that you have set the `jest` parameter on:
102
103```js
104import { addDecorator } from '@storybook/react'; // <- or your view layer
105import { withTests } from '@storybook/addon-jest';
106
107import results from '../.jest-test-results.json';
108
109addDecorator(
110 withTests({
111 results,
112 })
113);
114```
115
116Then in your story:
117
118```js
119import React from 'react';
120
121export default {
122 title: 'MyComponent',
123};
124
125export const defaultView = () => <div>Jest results in storybook</div>;
126defaultView.parameters = {
127 jest: ['MyComponent.test.js', 'MyOtherComponent.test.js'],
128};
129```
130
131### Disabling
132
133You can disable the addon for a single story by setting the `jest` parameter to `{disable: true}`:
134
135```js
136import React from 'react';
137
138export default {
139 title: 'MyComponent',
140};
141
142export const defaultView = () => <div>Jest results in storybook</div>;
143defaultView.parameters = {
144 jest: { disable: true },
145};
146```
147
148### withTests(options)
149
150- **options.results**: OBJECT jest output results. _mandatory_
151- **filesExt**: STRING test file extension. _optional_. This allows 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 means it will match: MyComponent.js, MyComponent.test.js, MyComponent.tests.js, MyComponent.spec.js, MyComponent.specs.js...
152
153## Usage with Angular
154
155Assuming that you have created test files `my.component.spec.ts` and `my-other.component.spec.ts`
156
157Configure Jest with [jest-preset-angular](https://www.npmjs.com/package/jest-preset-angular)
158
159In project's `typings.d.ts` add
160
161```ts
162declare module '*.json' {
163 const value: any;
164 export default value;
165}
166```
167
168In your `.storybook/preview.ts`:
169
170```ts
171import { addDecorator } from '@storybook/angular';
172import { withTests } from '@storybook/addon-jest';
173
174import results from '../.jest-test-results.json';
175
176addDecorator(
177 withTests({
178 results,
179 filesExt: '((\\.specs?)|(\\.tests?))?(\\.ts)?$',
180 })
181);
182```
183
184##### Example [here](https://github.com/storybookjs/storybook/tree/master/examples/angular-cli)
185
186## TODO
187
188- [ ] Add coverage
189- [ ] Display nested test better (describe)
190- [ ] Display the date of the test
191- [ ] Add unit tests
192- [ ] Add linting
193- [ ] Split <TestPanel />
194
195## Contributing
196
197All ideas and contributions are welcomed.
198
199## Licence
200
201MIT © 2017-present Renaud Tertrais