-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Conversation
Thanks for giving this a go @igor-dv ! This breaks existing common conventions in jest snapshot testing. How are edge cases handled?
|
It indeed breaks the convention, so in order to not conflict with jest I mark these snapshot files with a Snapshot update flag is supported. Files are not removed... My assumption is: Files will be located in the same places where the stories are - so it's a responsibility of the developers to remove them when they remove component/storyfile like they need to remove other unneeded assets (e.g. css files, images etc). But it can be handled in a few ways:
Are there any other edgecases that I am not familiar with ? |
@tmeasday Can you take a look at this? |
Generated by storyshots. Note that these are large files with snapshots for all the components that have stories. This is not ideal, but there is a pull request in progress for storyshots that will change this so that snapshots are stored in the component directory, which will make more sense. See: storybookjs/storybook#1584
Generated by storyshots. Note that these are large files with snapshots for all the components that have stories. This is not ideal, but there is a pull request in progress for storyshots that will change this so that snapshots are stored in the component directory, which will make more sense. See: storybookjs/storybook#1584
addons/storyshots/src/test-bodies.js
Outdated
} | ||
|
||
function getSnapshotFileName(context) { | ||
const fileName = context.storyFileName || __filename; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
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?
addons/storyshots/src/index.js
Outdated
@@ -21,6 +27,16 @@ const hasDependency = name => | |||
(pkg.devDependencies && pkg.devDependencies[name]) || | |||
(pkg.dependencies && pkg.dependencies[name]); | |||
|
|||
function patchStoriesOfFunction(storybookImpl) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I'm still interested in the approach where each story file is treated as a separate Jest test file, but that's probably much more complex than this approach. This seems great @igor-dv -- It would be ideal to solve your (2) above, but if it's not possible this seems an improvement on the status quo. We might want to do a run over some other storyshots related issues as I guess this will be a sort-of-breaking change? @ndelangen @shilman does that mean this would have to wait for storybook@4? |
Not necessarily a breaking change if this behavior is configurable =) But IMO this is a big change so it should be (If at all) a part of some release. |
Yeah! I mean we want people to notice it right ;) You're right, I guess if people have to use the new test body. We can make it the default for 4.0 then? |
Codecov Report
@@ Coverage Diff @@
## release/3.3 #1584 +/- ##
===============================================
+ Coverage 21.21% 23.12% +1.91%
===============================================
Files 252 253 +1
Lines 5695 5756 +61
Branches 672 692 +20
===============================================
+ Hits 1208 1331 +123
+ Misses 3984 3930 -54
+ Partials 503 495 -8
Continue to review full report at Codecov.
|
Seems great @igor-dv! Should we add the filename tracking to the vue layer as well (we really need to refactor this a bit I reckon) |
Yeah, I'll add it. Storyshots addon seems react dependent though. Do you have plans to refactor this somehow or to introduce another addon for Vue ? BTW, after adding the filename to react and react-native, seems like we have a lot of copy-pasting there.. And even |
Awesome work!... Created a quick PR that exports your PR Here: #1841 |
@igor-dv Is it just me or does Reproduce:
Expected: Storyshots in separate files but content remains exactly the same. Any thoughts? |
hm.. I need to check.. |
@digitalmaster , indeed there is a problem with the @tmeasday, @ndelangen what do you think, is this issue a blocker ? A quick fix could be providing the serializes separately for storyshots... |
Using the |
Hmm... I do think that this would be a bad bug to ship to users - at least not without some kind of warning letting them know that they can't use serializers if they opt in to our new multi file snapshot test function. Or maybe if there's a work around they can use in the mean time?.. if that's the case maybe we should include this in the readme? |
I'll try to add some kind of support until the 3.3 release |
Awesome.. thanks for looking into this @igor-dv 🙏🏽 |
I just tried the 3.3.0-alpha.2 releas and whenever I use
Doesn't matter if I pass the flag ==== Edit: Seems like it's because my Jest was out of date! Updating to latest Jest fixed it. |
Did something break this? what could be going on @igor-dv ? |
@ndelangen Oh sorry should've made the edit more obvious. Was due to my jest dep being outdated. Updating it to >20 Fixed it. Sorry about that. I'll leave my comment there so that if there are people hitting the same thing they know to update Jest. |
Hi, I'm having a problem picking up obsolete snapshots using 3.3.0-alpha.4 and
Any ideas? On my own project, I also ran into Storybook integrity errors when deleting entire story files |
I agree with @digitalmaster. We cannot underestimate the use of serializers in Jest and I foresee I lot of bug reports coming because of people who can't use e.g jest-styled-components in their tests. I made a PR to clarify it in |
Thank you guys for the great work on this PR. It makes such a big difference in my project that we would not be able to use snapshots without it. Still, I would like to suggest an improvement 🙃 The issueIn my setup, I always have a import React from 'react';
import { storiesOf } from '@storybook/react';
import Component from './component';
import cases from './cases.spec.js';
const stories = storiesOf('Category Icon Hover Menu', module)
for (const [caseName, caseData] of cases) {
stories.addCentered(caseName, () => (
<Component {...caseData} />
));
} I like organising the project like this because I get storybook "for free" while writing tests. However, the The alternativeI rewrote I personally like the results, and now the question is: should we make this the default behaviour and rewrite It would make the snapshot files smaller and easier and faster to read. I can make a PR and push my code to replace |
@zvictor , I don't know about the default behavior, but maybe you can make a PR that will allow customizing this behavior? |
@igor-dv customising in what way? do you mean having a parameter to toggle/select one style or another? |
You said that you've rewritten the |
Good idea. So I propose to change the signature of import _ from 'lodash'
import path from 'path'
import sanitize from 'sanitize-filename'
import initStoryshots, { multiSnapshotWithOptions } from '@storybook/addon-storyshots'
initStoryshots({
test: multiSnapshotWithOptions({}, ({ fileName, kind, story }) => {
const { dir } = path.parse(fileName)
const name = sanitize(`${_.kebabCase(kind)}--${_.kebabCase(story)}`)
return path.format({ dir: path.join(dir, '__snapshots__'), name, ext: '.storyshot' })
}),
}) Once you agree, I will start working on that. |
I am ok with this solution but would like to hear what others have to say |
FYI, I've added support for the custom serializers to the plugin. Still didn't publish it to npm though. |
Amazing! I have been working on that for 2 days. I tried many different things and I thought I was about to find a solution so many times, but none of my attempts worked. I am very curious to see your solution :) |
I've noticed that there was this PR - now it's possible to add serializer to the storyshots. I don't know if it covers all the needs, though... |
Would it be possible to move Some defensive code like that is needed because |
I think it's possible. Would you like to PR this with the proposal of the custom |
done 🙂 please check #2517. |
Issue: #887
What I did
This is an experimental feature, PR is for visibility and discussion (meanwhile).
I've created a custom jest matcher (review is welcomed as well) which patches a
toMatchSnapshot
matcher.How to test
Go to
storybook\addons\storyshots
and run
yarn example
TBD
#### (1) Handle react-native✅ Fixed as @tmeasday suggested in reviewFor react I've added this piece of code
I don't know if it will work with react-native and currently can't test it.
Any other suggestions how to extract story file name (without stack trace thingy) ?
#### (2) Integrity test to check if every .storyshot file has its story file✅(3) Add custom reporter for storyshots: ❌
Since with a
jest-specific-snapshot
we can't use the default.snap
extension, these tests are not included to "Snapshots" summeryBut I don't know if it's blocking..