Skip to content

Commit baa1f80

Browse files
authored
Merge pull request #11 from storybookjs/feat/prototype
MVP Prototype
2 parents 4b46f7b + 403f2b1 commit baa1f80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3700
-1066
lines changed

.babelrc.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module.exports = {
22
presets: [
3-
"@babel/preset-env",
4-
"@babel/preset-typescript",
5-
"@babel/preset-react",
3+
['@babel/preset-env', { targets: { node: 'current' } }],
4+
'@babel/preset-typescript',
5+
'@babel/preset-react',
66
],
77
env: {
88
esm: {
99
presets: [
1010
[
11-
"@babel/preset-env",
11+
'@babel/preset-env',
1212
{
1313
modules: false,
14+
targets: { node: 'current' },
1415
},
1516
],
1617
],

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Test
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
11+
- name: Use Node.js 14.x
12+
uses: actions/setup-node@v1
13+
with:
14+
node-version: 14.x
15+
16+
- name: Install dependencies
17+
uses: bahmutov/npm-install@v1
18+
19+
- name: Run tests
20+
run: |
21+
yarn test

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ node_modules/
33
storybook-static/
44
build-storybook.log
55
.DS_Store
6-
.env
6+
.env
7+
.cache
8+
stories/design-system
9+
yarn-error.log

.prettierrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
{}
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"bracketSpacing": true,
5+
"trailingComma": "es5",
6+
"singleQuote": true,
7+
"arrowParens": "always"
8+
}

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = {
33
"../stories/**/*.stories.mdx",
44
"../stories/**/*.stories.@(js|jsx|ts|tsx)",
55
],
6-
addons: ["../preset.js", "@storybook/addon-essentials"],
6+
addons: ["@storybook/addon-essentials"],
77
};

README.md

Lines changed: 162 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,197 @@
1-
# Storybook Addon Kit
1+
# Storybook Test Runner
22

3-
Simplify the creation of Storybook addons
3+
Storybook test runner turns all of your stories into executable tests.
44

5-
- 📝 Live-editing in development
6-
- ⚛️ React/JSX support
7-
- 📦 Transpiling and bundling with Babel
8-
- 🏷 Plugin metadata
9-
- 🚢 Release management with [Auto](https://github.com/intuit/auto)
10-
- 🧺 Boilerplate and sample code
11-
- 🛄 ESM support
12-
- 🛂 TypeScript by default with option to eject to JS
5+
## Table of Contents
136

14-
## Getting Started
7+
- [1. Features](#features)
8+
- [2. Getting Started](#getting-started)
9+
- [3. Configuration](#configuration)
10+
- [3. Running against a deployed Storybook](#running-against-a-deployed-storybook)
11+
- [4. Running in CI](#running-in-ci)
12+
- [5. Troubleshooting](#troubleshooting)
13+
- [6. Future work](#future-work)
1514

16-
Click the **Use this template** button to get started.
15+
## Features
1716

18-
![](https://user-images.githubusercontent.com/321738/125058439-8d9ef880-e0aa-11eb-9211-e6d7be812959.gif)
17+
- ⚡️ Zero config setup
18+
- 💨 Smoke test all stories
19+
- ▶️ Test stories with play functions
20+
- 🏃 Test your stories in parallel in a headless browser
21+
- 👷 Get feedback from error with a link directly to the story
22+
- 🐛 Debug them visually and interactively in a live browser with [addon-interactions](https://storybook.js.org/docs/react/essentials/interactions)
23+
- 🎭 Powered by [Jest](https://jestjs.io/) and [Playwright](https://playwright.dev/)
24+
- 👀 Watch mode, filters, and the conveniences you'd expect
1925

20-
Clone your repository and install dependencies.
26+
## Getting started
2127

22-
```sh
23-
yarn
24-
```
25-
26-
### Development scripts
27-
28-
- `yarn start` runs babel in watch mode and starts Storybook
29-
- `yarn build` build and package your addon code
30-
31-
### Switch from TypeScript to JavaScript
32-
33-
Don't want to use TypeScript? We offer a handy eject command: `yarn eject-ts`
34-
35-
This will convert all code to JS. It is a destructive process, so we recommended running this before you start writing any code.
28+
1. Install the test runner and the interactions addon in Storybook:
3629

37-
## What's included?
38-
39-
![Demo](https://user-images.githubusercontent.com/42671/107857205-e7044380-6dfa-11eb-8718-ad02e3ba1a3f.gif)
40-
41-
The addon code lives in `src`. It demonstrates all core addon related concepts. The three [UI paradigms](https://storybook.js.org/docs/react/addons/addon-types#ui-based-addons)
42-
43-
- `src/Tool.js`
44-
- `src/Panel.js`
45-
- `src/Tab.js`
46-
47-
Which, along with the addon itself, are registered in `src/preset/manager.js`.
30+
```jsx
31+
yarn add @storybook/test-runner -D
32+
```
4833

49-
Managing State and interacting with a story:
34+
<details>
35+
<summary>1.1 Optional instructions to install the Interactions addon for visual debugging of play functions</summary>
36+
37+
```jsx
38+
yarn add @storybook/addon-interactions @storybook/jest @storybook/testing-library -D
39+
```
40+
41+
Then add it to your `.storybook/main.js` config and enable debugging:
42+
43+
```jsx
44+
module.exports = {
45+
stories: ['@storybook/addon-interactions'],
46+
features: {
47+
interactionsDebugger: true,
48+
}
49+
};
50+
```
51+
</details>
52+
53+
2. Add a `test-storybook` script to your package.json
54+
55+
```json
56+
{
57+
"scripts": {
58+
"test-storybook": "test-storybook"
59+
}
60+
}
61+
```
5062

51-
- `src/withGlobals.js` & `src/Tool.js` demonstrates how to use `useGlobals` to manage global state and modify the contents of a Story.
52-
- `src/withRoundTrip.js` & `src/Panel.js` demonstrates two-way communication using channels.
53-
- `src/Tab.js` demonstrates how to use `useParameter` to access the current story's parameters.
63+
3. Run Storybook (the test runner runs against a running Storybook instance):
5464

55-
Your addon might use one or more of these patterns. Feel free to delete unused code. Update `src/preset/manager.js` and `src/preset/preview.js` accordingly.
65+
```jsx
66+
yarn storybook
67+
```
5668

57-
Lastly, configure you addon name in `src/constants.js`.
69+
4. Run the test runner:
5870

59-
### Metadata
71+
```jsx
72+
yarn test-storybook
73+
```
6074

61-
Storybook addons are listed in the [catalog](https://storybook.js.org/addons) and distributed via npm. The catalog is populated by querying npm's registry for Storybook-specific metadata in `package.json`. This project has been configured with sample data. Learn more about available options in the [Addon metadata docs](https://storybook.js.org/docs/react/addons/addon-catalog#addon-metadata).
75+
> **NOTE:** The runner assumes that your Storybook is running on port `6006`. If you're running Storybook in another port, set the TARGET_URL before running your command like:
76+
>```jsx
77+
>TARGET_URL=http://localhost:9009 yarn test-storybook
78+
>```
79+
80+
## Configuration
81+
82+
The test runner is based on [Jest](https://jestjs.io/) and will accept the [CLI options](https://jestjs.io/docs/cli) that Jest does, like `--watch`, `--marWorkers`, etc.
83+
84+
The test runner works out of the box, but you can override its Jest configuration by adding a `test-runner-jest.config.js` that sets up your environment in the root folder of your project.
85+
86+
```js
87+
// test-runner-jest.config.js
88+
module.exports = {
89+
cacheDirectory: 'node_modules/.cache/storybook/test-runner',
90+
testMatch: ['**/*.stories.[jt]s(x)?'],
91+
transform: {
92+
'^.+\\.stories\\.[jt]sx?$': '@storybook/test-runner/playwright/transform',
93+
'^.+\\.[jt]sx?$': 'babel-jest',
94+
},
95+
preset: 'jest-playwright-preset',
96+
testEnvironment: '@storybook/test-runner/playwright/custom-environment.js',
97+
testEnvironmentOptions: {
98+
'jest-playwright': {
99+
browsers: ['chromium', 'firefox', 'webkit'], // run tests against all browsers
100+
},
101+
},
102+
};
103+
```
62104
63-
## Release Management
105+
The runner uses [jest-playwright](https://github.com/playwright-community/jest-playwright) and you can pass [testEnvironmentOptions](https://github.com/playwright-community/jest-playwright#configuration) to further configure it, such as how it's done above to run tests against all browsers instead of just chromium.
64106

65-
### Setup
107+
## Running against a deployed Storybook
66108

67-
This project is configured to use [auto](https://github.com/intuit/auto) for release management. It generates a changelog and pushes it to both GitHub and npm. Therefore, you need to configure access to both:
109+
By default, the test runner assumes that you're running it against a locally served Storybook.
110+
If you want to define a target url so it runs against deployed Storybooks, you can do so by passing the `TARGET_URL` environment variable:
68111

69-
- [`NPM_TOKEN`](https://docs.npmjs.com/creating-and-viewing-access-tokens#creating-access-tokens) Create a token with both _Read and Publish_ permissions.
70-
- [`GH_TOKEN`](https://github.com/settings/tokens) Create a token with the `repo` scope.
112+
```bash
113+
TARGET_URL=https://the-storybook-url-here.com yarn test-storybook
114+
```
71115

72-
Then open your `package.json` and edit the following fields:
116+
## Running in CI
117+
118+
### Running against deployed Storybooks on Github Actions deployment
119+
120+
On Github actions, once services like Vercel, Netlify and others do deployment run, they follow a pattern of emitting a `deployment_status` event containing the newly generated URL under `deployment_status.target_url`. Here's an example of an action to run tests based on that:
121+
122+
```yml
123+
name: Storybook Tests
124+
on: deployment_status
125+
jobs:
126+
test:
127+
timeout-minutes: 60
128+
runs-on: ubuntu-latest
129+
if: github.event.deployment_status.state == 'success'
130+
steps:
131+
- uses: actions/checkout@v2
132+
- uses: actions/setup-node@v2
133+
with:
134+
node-version: '14.x'
135+
- name: Install dependencies
136+
run: yarn
137+
- name: Run Storybook tests
138+
run: yarn test-storybook
139+
env:
140+
TARGET_URL: '${{ github.event.deployment_status.target_url }}'
141+
```
73142
74-
- `name`
75-
- `author`
76-
- `repository`
143+
### Running againts locally built Storybooks in CI
77144
78-
#### Local
145+
In order to build and run tests against your Storybook in CI, you might need to use a combination of commands involving the [concurrently](https://www.npmjs.com/package/concurrently), [http-server](https://www.npmjs.com/package/http-server) and [wait-on](https://www.npmjs.com/package/wait-on) libraries. Here's a recipe that does the following: Storybook is built and served locally, and once it is ready, the test runner will run against it.
79146
80-
To use `auto` locally create a `.env` file at the root of your project and add your tokens to it:
147+
```json
148+
{
149+
"test-storybook:ci": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"yarn build-storybook && npx http-server storybook-static --port 6006 --silent\" \"wait-on tcp:6006 && yarn test-storybook\""
150+
}
151+
```
81152

82-
```bash
83-
GH_TOKEN=<value you just got from GitHub>
84-
NPM_TOKEN=<value you just got from npm>
153+
And then you can essentially run `test-storybook:ci` in your CI:
154+
155+
```yml
156+
name: Storybook Tests
157+
on: push
158+
jobs:
159+
test:
160+
timeout-minutes: 60
161+
runs-on: ubuntu-latest
162+
steps:
163+
- uses: actions/checkout@v2
164+
- uses: actions/setup-node@v2
165+
with:
166+
node-version: '14.x'
167+
- name: Install dependencies
168+
run: yarn
169+
- name: Run Storybook tests
170+
run: yarn test-storybook:ci
85171
```
86172
87-
Lastly, **create labels on GitHub**. You’ll use these labels in the future when making changes to the package.
88173
89-
```bash
90-
npx auto create-labels
91-
```
174+
## Troubleshooting
92175
93-
If you check on GitHub, you’ll now see a set of labels that `auto` would like you to use. Use these to tag future pull requests.
176+
#### The test runner seems flaky and keeps timing out
94177
95-
#### GitHub Actions
178+
If your tests are timing out with `Timeout - Async callback was not invoked within the 15000 ms timeout specified by jest.setTimeout`, it might be that playwright couldn't handle to test the amount of stories you have in your project. Maybe you have a large amount of stories or your CI has a really low RAM configuration.
96179

97-
This template comes with GitHub actions already set up to publish your addon anytime someone pushes to your repository.
180+
In either way, to fix it you should limit the amount of workers that run in parallel by passing the [--maxWorkers](https://jestjs.io/docs/cli#--maxworkersnumstring) option to your command:
98181

99-
Go to `Settings > Secrets`, click `New repository secret`, and add your `NPM_TOKEN`.
182+
```json
183+
{
184+
"test-storybook:ci": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"yarn build-storybook && npx http-server storybook-static --port 6006 --silent\" \"wait-on tcp:6006 && yarn test-storybook --maxWorkers=2\""
185+
}
186+
```
100187

101-
### Creating a releasing
188+
#### Adding the test runner to other CI environments
102189

103-
To create a release locally you can run the following command, otherwise the GitHub action will make the release for you.
190+
As the test runner is based on playwright, depending on your CI setup you might need to use specific docker images or other configuration. In that case, you can refer to the [Playwright CI docs](https://playwright.dev/docs/ci) for more information.
104191

105-
```sh
106-
yarn release
107-
```
192+
## Future work
108193

109-
That will:
194+
Future plans involve adding support for the following features:
110195

111-
- Build and package the addon code
112-
- Bump the version
113-
- Push a release to GitHub and npm
114-
- Push a changelog to GitHub
196+
- 🧪 Custom test functions
197+
- 📄 Run addon reports

0 commit comments

Comments
 (0)