You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore(docs): Update Storybook guide to v6 (#31653)
* Added Storybook v6 section
- Updated Storybook v6 section as per this [guide](https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324) to use Webpack v5 version
- Since [Webpack v5 bundler experimental support](https://storybook.js.org/blog/storybook-for-webpack-5/) will work without much fuss in the upcoming v6.3.0 (currently in beta), this guide may require slight modification in the coming days
* updated docs for storybook v6.3 and webpack 5
* remove stale whitespaces
* Update visual-testing-with-storybook.md
* chore: format
* Update visual-testing-with-storybook.md
* Update visual-testing-with-storybook.md
Co-authored-by: Lennart <[email protected]>
Co-authored-by: gatsbybot <[email protected]>
Copy file name to clipboardExpand all lines: docs/docs/how-to/testing/visual-testing-with-storybook.md
+44-197
Original file line number
Diff line number
Diff line change
@@ -11,260 +11,109 @@ As your project grows over time having this information available will be invalu
11
11
12
12
## Setting up your environment
13
13
14
-
> Note that the following instructions are using [npx](https://www.npmjs.com/package/npx). `npx` is a part of npm and in this case it allows you to automatically generate a file/folder structure complete with the default configuration. If you're running an older version of `npm` (`<5.2.0`) you should run the following command instead: `npm install -g @storybook/cli`. You can then run `sb init` from your Gatsby root directory to initialize Storybook.
14
+
> **Note:** The following instructions are using [npx](https://www.npmjs.com/package/npx). `npx` is a part of npm and in this case it allows you to automatically generate a file/folder structure complete with the default configuration. If you're running an older version of `npm` (`<5.2.0`) you should run the following command instead: `npm install -g @storybook/cli`. You can then run `sb init` from your Gatsby root directory to initialize Storybook.
15
15
16
16
To set up Storybook you need to install dependencies and do some custom configuration. You can get started quickly by using the automated command line tool from your Gatsby root directory:
17
17
18
18
```shell
19
19
npx -p @storybook/cli sb init
20
20
```
21
21
22
-
This command adds a set of boilerplate files for Storybook in your project. However, since this is for a Gatsby project, you need to update the default Storybook configuration a bit so you don't get errors when trying to use Gatsby specific components inside of the stories.
22
+
This command adds a set of boilerplate files for Storybook in your project. However, since this is for a Gatsby project, you need to update the default Storybook configuration a bit so you don't get errors when trying to use Gatsby specific components inside of the stories. You should have a configuration file at `.storybook/main.js` now.
23
23
24
-
### Storybook version 5
24
+
##Configuration
25
25
26
-
Storybook version 5.3 [brought a major change to how Storybook is configured](https://medium.com/storybookjs/declarative-storybook-configuration-49912f77b78).
26
+
> **Note:** Make sure that you are using a Storybook version `>=6.3.0` before following the instructions below. For older versions of Storybook you can visit the [Gatsby v2 documentation](https://v2.gatsbyjs.com/docs/how-to/testing/visual-testing-with-storybook/).
27
27
28
-
When you first install Storybook the only configuration file that will exist is `.storybook/main.js`, which will have the default stories location and default addons.
28
+
Storybook v6 uses webpack v4 by default, while Gatsby uses webpack v5. Hence, the webpack version for Storybook should be changed to match that of Gatsby to prevent conflicts. Storybook has [official webpack v5 support](https://storybook.js.org/blog/storybook-for-webpack-5/) and can be enabled in your Storybook config.
29
+
30
+
Install the necessary npm packages:
31
+
32
+
```shell
33
+
npm i -D @storybook/builder-webpack5 @storybook/manager-webpack5
34
+
```
35
+
36
+
Update your `.storybook/main.js` file to use webpack v5:
Adjustments to Storybook's default `webpack` configuration are required so that you can transpile Gatsby source files and to ensure you have the necessary Babel plugins to transpile Gatsby components. Add the following section to the `module.exports` object in `.storybook/main.js`.
46
+
Adjustments to Storybook's default webpack configuration are required so that you can transpile Gatsby source files and to ensure you have the necessary Babel plugins to transpile Gatsby components. Add the following section to your `.storybook/main.js`:
38
47
39
48
```js:title=.storybook/main.js
40
-
webpackFinal:asyncconfig=> {
41
-
// Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
49
+
module.exports= {
50
+
webpackFinal:asyncconfig=> {
51
+
// transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
Next create a new file under `.storybook` called `preview.js`. This configuration file preview.js is not responsible for loading any stories. Its main purpose is to add global parameters and [decorators](https://storybook.js.org/docs/addons/introduction/#1-decorators).
90
+
Create a new file under `.storybook` called `preview.js`. This configuration file `preview.js` is not responsible for loading any stories but for [configuring story rendering](https://storybook.js.org/docs/react/configure/overview#configure-story-rendering). Its main purpose is to add [global parameters](https://storybook.js.org/docs/react/writing-stories/parameters#global-parameters) and [decorators](https://storybook.js.org/docs/react/writing-stories/decorators).
105
91
106
92
```js:title=.storybook/preview.js
107
93
import { action } from"@storybook/addon-actions"
108
94
109
95
// Gatsby's Link overrides:
110
96
// Gatsby Link calls the `enqueue` & `hovering` methods on the global variable ___loader.
111
97
// This global object isn't set in storybook context, requiring you to override it to empty functions (no-op),
112
-
// so Gatsby Link doesn't throw any errors.
98
+
// so Gatsby Link doesn't throw errors.
113
99
global.___loader= {
114
100
enqueue: () => {},
115
101
hovering: () => {},
116
102
}
117
-
// This global variable is prevents the "__BASE_PATH__ is not defined" error inside Storybook.
103
+
// This global variable prevents the "__BASE_PATH__ is not defined" error inside Storybook.
118
104
global.__BASE_PATH__="/"
119
105
120
106
// Navigating through a gatsby app using gatsby-link or any other gatsby component will use the `___navigate` method.
121
-
// In Storybook it makes more sense to log an action than doing an actual navigate. Checkout the actions addon docs for more info: https://github.com/storybookjs/storybook/tree/master/addons/actions.
107
+
// In Storybook it makes more sense to log an action than doing an actual navigate. Checkout the actions addon docs for more info: https://github.com/storybookjs/storybook/tree/master/addons/actions
122
108
123
109
window.___navigate=pathname=> {
124
110
action("NavigateTo:")(pathname)
125
111
}
126
112
```
127
113
128
-
#### Add TypeScript Support
129
-
130
-
To configure TypeScript with Storybook and Gatsby, add the following configuration to `.storybook/main.js`.
114
+
## TypeScript Support
131
115
132
-
Add `tsx` as a file type to look for in the `stories` array. This assumes the default Storybook path, but the array configuration can be modified to where your stories live.
Add the following code after the line containing `config.resolve.mainFields = ["browser", "module", "main"];` (line 25) and before the `return config;` in the same function body (line 27):
Once you have this configured you should run Storybook to ensure it can start up properly and you can see the default stories installed by the CLI. To run storybook:
251
-
252
-
```shell
253
-
npm run storybook
254
-
```
255
-
256
-
Storybook CLI adds this command to your `package.json` for you so you shouldn't have to anything other than run the command. If Storybook builds successfully you should be able to navigate to `http://localhost:6006` and see the default stories supplied by the Storybook CLI.
257
-
258
-
However, if you use `StaticQuery` or `useStaticQuery` in your project Storybook needs to be run with the `NODE_ENV` set to `production` (as Storybook sets this by default to `development`). Otherwise `babel-plugin-remove-graphql-queries` won't be run. Moreover Storybook needs to know about [static files](https://storybook.js.org/docs/configurations/serving-static-files/#2-via-a-directory) generated by Gatsby's `StaticQuery`. Your scripts should look like:
The Storybook v6 has [out-of-the-box support for TypeScript](https://storybook.js.org/docs/react/configure/typescript). The stories and components can be authored with `.tsx` extension
268
117
269
118
## Writing stories
270
119
@@ -292,6 +141,4 @@ This is a very simple story without much going on, but honestly, nothing else re
292
141
293
142
## Other resources
294
143
295
-
- For more information on Storybook, visit
296
-
[the Storybook site](https://storybook.js.org/).
297
-
- Get started with a [Jest and Storybook starter](https://github.com/Mathspy/gatsby-storybook-jest-starter)
144
+
- For more information on Storybook, visit [the Storybook site](https://storybook.js.org/)
0 commit comments