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

887 Generate snapshot per story file #1584

Merged
merged 31 commits into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
917f314
Use experemental jest-specific-snapshot to generate snapshot per stor…
igor-dv Aug 2, 2017
1d54e27
Add multiSnapshotWithOptions + upgrade jest-specific-snapshot to 0.2.0
igor-dv Aug 3, 2017
810a63b
Fix the case there is no "?"
igor-dv Aug 3, 2017
5e60a9c
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 6, 2017
dc8bbd7
Use patchStoriesOfFunction for RN
igor-dv Aug 6, 2017
8f9926f
Merge branch 'master' into 887-generate-snapshot-per-story-file
ndelangen Aug 7, 2017
3a78efe
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 8, 2017
9e20cb7
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 9, 2017
0eb3093
Add fileName prop to the storybook client api
igor-dv Aug 9, 2017
9f6e573
Try storyshot example pass on CI
igor-dv Aug 9, 2017
1c1c9db
Save snapshots
igor-dv Aug 9, 2017
ca294e9
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 9, 2017
3eb8390
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 10, 2017
ab71413
Support fileName in Vue
igor-dv Aug 10, 2017
ba02cc8
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 11, 2017
6bf56cb
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 12, 2017
e83b2fd
Merge branch 'master' into 887-generate-snapshot-per-story-file
ndelangen Aug 13, 2017
54cb5d1
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 15, 2017
70c2ebe
Merge branch 'master' into 887-generate-snapshot-per-story-file
igor-dv Aug 18, 2017
9b815a0
Merge branch 'master' into 887-generate-snapshot-per-story-file
Aug 20, 2017
96a389b
Merge branch 'master' into 887-generate-snapshot-per-story-file
ndelangen Aug 22, 2017
571373d
Merge branch 'release/3.3' into 887-generate-snapshot-per-story-file
ndelangen Aug 24, 2017
7de3acc
Merge branch 'release/3.3' into 887-generate-snapshot-per-story-file
igor-dv Aug 24, 2017
be9ab35
Merge branch 'release/3.3' into 887-generate-snapshot-per-story-file
ndelangen Aug 25, 2017
8e87fb8
CHANGE path of the snapshots files into the regular `__snapshots__` f…
ndelangen Aug 25, 2017
af02e15
CHANGE storyshots on cra-kitchen-sink to use this new feature
ndelangen Aug 25, 2017
fd3d5e5
CLEANUP reduce noise in unit tests
ndelangen Aug 25, 2017
07146a8
Merge branch 'release/3.3' into 887-generate-snapshot-per-story-file
shilman Aug 26, 2017
66c0d09
Document new option in README
shilman Aug 26, 2017
91e0105
Merge branch 'release/3.3' into 887-generate-snapshot-per-story-file
ndelangen Aug 26, 2017
6496454
Add integrity test to check if every .storyshot file has its story file
igor-dv Aug 27, 2017
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
1 change: 1 addition & 0 deletions addons/storyshots/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.storyshot
9 changes: 7 additions & 2 deletions addons/storyshots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"scripts": {
"build-storybook": "build-storybook",
"prepublish": "babel ./src --out-dir ./dist",
"storybook": "start-storybook -p 6006"
"storybook": "start-storybook -p 6006",
"example": "jest storyshot.test"
},
"dependencies": {
"babel-runtime": "^6.23.0",
"global": "^4.3.2",
"jest-specific-snapshot": "^0.2.0",
"prop-types": "^15.5.10",
"read-pkg-up": "^2.0.0"
},
Expand All @@ -24,11 +26,14 @@
"@storybook/channels": "^3.2.0",
"@storybook/react": "^3.2.3",
"babel-cli": "^6.24.1",
"babel-jest": "^20.0.3",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^15.6.1",
"react-dom": "^15.6.1"
"react-dom": "^15.6.1",
"jest": "^20.0.4",
"jest-cli": "^20.0.4"
},
"peerDependencies": {
"@storybook/addons": "^3.2.0",
Expand Down
33 changes: 29 additions & 4 deletions addons/storyshots/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import runWithRequireContext from './require_context';
import createChannel from './storybook-channel-mock';
import { snapshot } from './test-bodies';

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

let storybook;
let configPath;
Expand All @@ -21,6 +27,16 @@ const hasDependency = name =>
(pkg.devDependencies && pkg.devDependencies[name]) ||
(pkg.dependencies && pkg.dependencies[name]);

function patchStoriesOfFunction(storybookImpl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just save filename alongside the story in core rather than doing this hack?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it's a better way.. I just didn't want to change the core only for the addon needs.. But maybe in this case it's a good solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a pretty transparent change that could be useful for other purposes.

const realStoriesOf = storybookImpl.storiesOf;
// eslint-disable-next-line
storybookImpl.storiesOf = (kind, module) => {
const storyFileName = module ? module.filename : '';
const newKindName = `${storyFileName}?${kind}`;
return realStoriesOf(newKindName, module);
};
}

export default function testStorySnapshots(options = {}) {
addons.setChannel(createChannel());

Expand All @@ -45,9 +61,14 @@ export default function testStorySnapshots(options = {}) {
dirname: configDirPath,
};

patchStoriesOfFunction(storybook);

runWithRequireContext(content, contextOpts);
} else if (isRNStorybook) {
storybook = require.requireActual('@storybook/react-native');

patchStoriesOfFunction(storybook);

configPath = path.resolve(options.configPath || 'storybook');
require.requireActual(configPath);
} else {
Expand All @@ -70,13 +91,17 @@ export default function testStorySnapshots(options = {}) {

// eslint-disable-next-line
for (const group of stories) {
if (options.storyKindRegex && !group.kind.match(options.storyKindRegex)) {
const groupKindParts = group.kind.split('?');
const [storyFileName, kind] =
groupKindParts.length === 1 ? ['', groupKindParts[0]] : groupKindParts;

if (options.storyKindRegex && !kind.match(options.storyKindRegex)) {
// eslint-disable-next-line
continue;
}

describe(suite, () => {
describe(group.kind, () => {
describe(kind, () => {
// eslint-disable-next-line
for (const story of group.stories) {
if (options.storyNameRegex && !story.name.match(options.storyNameRegex)) {
Expand All @@ -85,7 +110,7 @@ export default function testStorySnapshots(options = {}) {
}

it(story.name, () => {
const context = { kind: group.kind, story: story.name };
const context = { storyFileName, kind, story: story.name };
options.test({ story, context });
});
}
Expand Down
22 changes: 20 additions & 2 deletions addons/storyshots/src/test-bodies.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import path from 'path';
import renderer from 'react-test-renderer';
import shallow from 'react-test-renderer/shallow';
import 'jest-specific-snapshot';

export const snapshotWithOptions = options => ({ story, context }) => {
function getRenderedTree(story, context, options) {
const storyElement = story.render(context);
const tree = renderer.create(storyElement, options).toJSON();
return renderer.create(storyElement, options).toJSON();
}

function getSnapshotFileName(context) {
const fileName = context.storyFileName || __filename;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely __filename isn't right here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, actually I need the test path.. I thought it will be a nice fallback..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this will just be the file test-bodies.js right? I'm not sure what a good default is but perhaps it should be figured out by the jest plugin?

const { dir, name } = path.parse(fileName);
return path.format({ dir, name, ext: '.storyshot' });
}

export const snapshotWithOptions = options => ({ story, context }) => {
const tree = getRenderedTree(story, context, options);
expect(tree).toMatchSnapshot();
};

export const multiSnapshotWithOptions = options => ({ story, context }) => {
const tree = getRenderedTree(story, context, options);
const snapshotFileName = getSnapshotFileName(context);
expect(tree).toMatchSpecificSnapshot(snapshotFileName);
};

export const snapshot = snapshotWithOptions({});

export function shallowSnapshot({ story, context }) {
Expand Down
5 changes: 5 additions & 0 deletions addons/storyshots/stories/storyshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import initStoryshots, { multiSnapshotWithOptions } from '../src';

initStoryshots({
test: multiSnapshotWithOptions({}),
});