Skip to content

Commit dd8e861

Browse files
author
Tom Coleman
authored
Merge pull request #1035 from storybooks/configure-snapshot-test
Configure snapshot test
2 parents 4026bd6 + b00d4a4 commit dd8e861

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

addons/storyshots/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,19 @@ initStoryshots({
100100
### `framework`
101101

102102
If you are running tests from outside of your app's directory, storyshot's detection of which framework you are using may fail. Pass `"react"` or `"react-native"` to short-circuit this.
103+
104+
### `test`
105+
106+
Run a custom test function for each story, rather than the default (a vanilla snapshot test). See the exports section below for more details.
107+
108+
## Exports
109+
110+
Apart from the default export (`initStoryshots`), Storyshots also exports some named test functions (see the `test` option above):
111+
112+
### `snapshot`
113+
114+
The default, render the story as normal and take a Jest snapshot.
115+
116+
### `renderOnly`
117+
118+
Just render the story, don't check the output at all (useful if you just want to ensure it doesn't error).

addons/storyshots/src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import renderer from 'react-test-renderer';
21
import path from 'path';
32
import readPkgUp from 'read-pkg-up';
43
import addons from '@storybook/addons';
54
import runWithRequireContext from './require_context';
65
import createChannel from './storybook-channel-mock';
6+
import { snapshot } from './test-bodies';
77
const { describe, it, expect } = global;
88

9+
export { snapshot, renderOnly } from './test-bodies';
10+
911
let storybook;
1012
let configPath;
1113

@@ -59,6 +61,8 @@ export default function testStorySnapshots(options = {}) {
5961
// Added not to break existing storyshots configs (can be removed in a future major release)
6062
options.storyNameRegex = options.storyNameRegex || options.storyRegex;
6163

64+
options.test = options.test || snapshot;
65+
6266
for (const group of stories) {
6367
if (options.storyKindRegex && !group.kind.match(options.storyKindRegex)) {
6468
continue;
@@ -73,9 +77,7 @@ export default function testStorySnapshots(options = {}) {
7377

7478
it(story.name, () => {
7579
const context = { kind: group.kind, story: story.name };
76-
const renderedStory = story.render(context);
77-
const tree = renderer.create(renderedStory).toJSON();
78-
expect(tree).toMatchSnapshot();
80+
options.test({ story, context });
7981
});
8082
}
8183
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import renderer from 'react-test-renderer';
2+
import shallow from 'react-test-renderer/shallow';
3+
4+
export function snapshot({ story, context }) {
5+
const storyElement = story.render(context);
6+
const tree = renderer.create(storyElement).toJSON();
7+
expect(tree).toMatchSnapshot();
8+
}
9+
10+
export function renderOnly({ story, context }) {
11+
const storyElement = story.render(context);
12+
const tree = renderer.create(storyElement);
13+
}

0 commit comments

Comments
 (0)