UNPKG

4.36 kBMarkdownView Raw
1# StoryShots [![CircleCI](https://circleci.com/gh/kadirahq/storyshots.svg?style=shield)](https://circleci.com/gh/kadirahq/storyshots)
2
3Snapshot Testing for [React Storybook](https://github.com/kadirahq/react-storybook)
4
5![StoryShots in use](docs/screenshot.png)
6
7With StoryShots, you could use your existing Storybook stories, as the input for snapshot testing. We do it by integrating Jest's snapshot testing support, directly into Storybook.
8
9Read This: [Snapshot Testing in React Storybook](https://voice.kadira.io/snapshot-testing-in-react-storybook-43b3b71cec4f#.ndyuhcxhd)
10
11## Getting Started
12
13First of all, you need to use the latest version of React Storybook.
14So, do this:
15
16```sh
17npm update @kadira/storybook
18```
19
20Then add the following NPM module into your app.
21
22```sh
23npm i -D @kadira/storyshots
24```
25
26Then, add a NPM script as follows:
27
28```js
29"scripts": {
30 "test-storybook": "storyshots"
31}
32```
33
34Then, run the following command:
35
36```sh
37npm run test-storybook
38```
39
40After that, you can see an output like this:
41
42![First Run](docs/first-run.png)
43
44This will create a set of snapshots, inside your Storybook config directory. You could publish them into GIT.
45
46## UI Changes
47
48Once you did a UI change, you could run StoryShots again with:
49
50```sh
51npm run test-storybook
52```
53
54Then, you can see changes with a diff view, like the following:
55
56![UI Changes](docs/screenshot.png)
57
58If these changes are intentional, you could update snapshots with:
59
60```sh
61npm run test-storybook -- -u
62```
63
64If not, you could try to correct it and re-run the above command.
65
66## Key Features
67
68StoryShots comes with some few features which help you to be productive and customize it, to suit your project.
69
70### Interactive Mode
71
72When you have a lot of UI changes, it's a good idea to check and update them, one by one. That's where our interactive mode comes in.
73Run the following command:
74
75```sh
76npm run test-storybook -- -i
77```
78
79### Watch files
80
81It's pretty useful to watch files and re-run StoryShots again. You can do that with the `-w` flag.
82
83```sh
84npm run test-storybook -- -w
85```
86
87### Grep Stories
88
89You may don't want to storyshot each and every of your stories. If so, you could grep which stories you want to storyshot, by invoking the `-g` option:
90
91```sh
92npm run test-storybook -- -g "theme"
93```
94
95You can also use the `-x` option similarly to exclude some stories.
96
97### Provide Custom Loaders
98
99When we are running your stories, we don't use Webpack. So, we can't import files other than `.js` and `.json`.
100This means actually, that we can't import your `.css` and `.png` files.
101
102In order to fix this issue, we provide some mock loaders for few of the most common file types.
103Here are [they](https://github.com/kadirahq/storyshots/blob/master/src/default_config/loaders.js).
104
105But, we can't add all the loaders you might use. So, we allow you to customize it.
106Instead of using our loaders, you could use a set of loaders you want.
107
108For that, first create a file called `loaders.js` in your project root. Then add support to few loaders like this:
109
110```js
111var loaders = module.exports = {};
112
113// to support css modules
114loaders['.css'] = function(path) {
115 return {};
116};
117
118// to support jpeg files
119loaders['.jpeg'] = function(path) {
120 return path;
121}
122```
123
124Then, run StoryShots like this:
125
126```
127npm run test-storybook -- --loaders=loaders.js
128```
129
130> You could also update your original NPM script, according to the following instead.
131> ~~~
132> "test-storybook": "storyshots --loaders=loaders.js"
133> ~~~
134
135### Add Window and Global Polyfills
136
137StoryShot doesn't use an actual browser, to run your code. Since your UI components may use browser features, we try to create a minimal browser environment with JSDom and with some common polyfills.
138
139You can see them [here](https://github.com/kadirahq/storyshots/blob/master/src/default_config/polyfills.js).
140
141But, you may also use some other browser features. Then, we allow you to add custom polyfills, replacing our own config.
142Create a file like [this](https://github.com/kadirahq/storyshots/blob/master/src/default_config/polyfills.js) with your own polyfills.
143
144Then, run StoryShots like this:
145
146```sh
147npm run test-storybook -- --polyfills=loaders.js
148```
149
150## Other Features
151
152Beside these main features, StoryShots comes with few other minor features.
153You could see them by looking at the help:
154
155```sh
156npm run test-storybook -- -h
157```