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