Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom test function for storyshots #1035

Merged
merged 2 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions addons/storyshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ initStoryshots({
### `framework`

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.

### `test`

Run a custom test function for each story, rather than the default (a vanilla snapshot test). See the exports section below for more details.

## Exports

Apart from the default export (`initStoryshots`), Storyshots also exports some named test functions (see the `test` option above):

### `snapshot`

The default, render the story as normal and take a Jest snapshot.

### `renderOnly`

Just render the story, don't check the output at all (useful if you just want to ensure it doesn't error).
10 changes: 6 additions & 4 deletions addons/storyshots/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import renderer from 'react-test-renderer';
import path from 'path';
import readPkgUp from 'read-pkg-up';
import addons from '@storybook/addons';
import runWithRequireContext from './require_context';
import createChannel from './storybook-channel-mock';
import { snapshot } from './test-bodies';
const { describe, it, expect } = global;

export { snapshot, renderOnly } from './test-bodies';

let storybook;
let configPath;

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

options.test = options.test || snapshot;

for (const group of stories) {
if (options.storyKindRegex && !group.kind.match(options.storyKindRegex)) {
continue;
Expand All @@ -73,9 +77,7 @@ export default function testStorySnapshots(options = {}) {

it(story.name, () => {
const context = { kind: group.kind, story: story.name };
const renderedStory = story.render(context);
const tree = renderer.create(renderedStory).toJSON();
expect(tree).toMatchSnapshot();
options.test({ story, context });
});
}
});
Expand Down
13 changes: 13 additions & 0 deletions addons/storyshots/src/test-bodies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import renderer from 'react-test-renderer';
import shallow from 'react-test-renderer/shallow';

export function snapshot({ story, context }) {
const storyElement = story.render(context);
const tree = renderer.create(storyElement).toJSON();
expect(tree).toMatchSnapshot();
}

export function renderOnly({ story, context }) {
const storyElement = story.render(context);
const tree = renderer.create(storyElement);
}