Introduce new integration between Storybook and Backstop
Feature Description
With the update to Storybook to v6, we've started migrating a few of our stories to use the new Component Story Format in line with our documentation for how stories should be written going forward.
While Storybook is very useful by itself, we also use it as the basis for our visual regression tests using BackstopJS. Currently, all VRT scenarios are defined in a giant array in the .storybook/storybook-data.js module. This is cumbersome to update because it's completely separate from both stories and scenarios, leaving a lot to be desired.
In our legacy stories, backstop scenario configuration was set at the story level as a options object passed via story parameters. This gave the appearance that these options were actually used by Backstop although it still required adding into the storybook-data.js manually.
With the new CSF format, we have a lot more flexibility in making this work as expected because story definitions will no longer require running through storiesOf().
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Stories defined using CSF should be able to define their BackstopJS scenario definitions on the component itself, just like
argsorparameters, etc by defining ascenarioproperty- This property should be a boolean or a plain object (similar to the way
parameters.optionsis now)- If
truethe scenario for that story should be added using the default/common scenario definition (see below) falseshould yield the same as if there was noscenarioproperty defined (no added scenario) so there's not much reason to use it- If defined with an object, this configuration should extend the default/common scenario definition but it should not be able to override the
scenario.urlwhich must be based on the Story'sid- This format is intended for stories that require custom options for
delay, etc
- This format is intended for stories that require custom options for
- If
- This property should be a boolean or a plain object (similar to the way
- Backstop scenario sourcing should be updated to additionally include scenarios defined on story exports
- All stories should be collected according to the paths defined in the main Storybook config
- The named exports of each module should then be inspected, and any
namedExports.scenarioshould be added to the list of scenarios- Each
scenarioadded in this way should have default values populated for itslabelandurlbased on the story which are required by Backstop - The utilities for determining a story's
nameandid(needed for thelabelandurl) when not explicitly provided are sourced from this package:@componentdriven/csf - The story
kindis the same as the story'sdefaultExport.title
- Each
- Each of the 3 initial stories/kinds/components migrated to Storybook v6 should be updated to use this new definition for the scenarios
- The storiesβ respective entries in
storybook-data.jsshould be removed
- The storiesβ respective entries in
Implementation Brief
- Using
tests/backstop/scenarios.js, - Install the
globpackage to get the list of file names ending with.stories.jsgetting the paths of the story files from.storybook/main.js. - Install the
@babel/corepackage which will also install the@babel/parserand@babel/traversepackages. Those packages will be used to parse the stories to look for named exports and their properties. - Get the list of
*.stories.jsfiles using theglob.syncfunction and loop through them. - Read the file using the
readFileSyncfunction and use theparsefunction from@babel/parserto parse the code. - Using the
traversefunction from@babel/traverse, walk through the parsed code looking forExportDefaultDeclarationandAssignmentExpressionnode types, grabbing the necessary properties and adding them to an array (stories). The properties namely are:- the
titleandcomponentfrom the default export (ExportDefaultDeclaration) - any properties for a story via
AssignmentExpression
- the
- Once the code has been parsed and inspected as mentioned in the previous bullet point, loop through the array of stories.
- If the currently iterated story has the
scenarioproperty, then add the story details to the existingstorybookStoriesarray. The story details should be an object having the same keys as in.storybook/storybook-data.jsand as defined in the AC. - Remove stories which have the
scenarioproperty from.storybook/storybook-data.jsas per the AC.
Proof of concept: https://github.com/google/site-kit-wp/blob/ib/3182-storybook-backstop/tests/backstop/scenarios.js
Test Coverage
- None: no changes to unit or e2e tests
Visual Regression Changes
- There should be no changes. All the VRTs that are changed to the new format should pass the same!
QA Brief
- There are no user-facing changes here (possibly should be QA:Eng)
- Check all VRTs still pass
Changelog entry
- N/A
@felixarntz anything to add here?
@aaemnnosttv Nothing to add, lgtm. I've marked this as blocked by the other three issues for the Storybook v6 migration though, since they should be done first, to then be amended here for VRT.
@aaemnnosttv I was hoping we could use the new extract command which comes with Storybook 6, but when I tested, custom properties were not exported in the JSON file. So prior to to run the backstopjs command, I thought we would run sb extract to get the list of stories and then parse the JSON file. Unfortunately it's not possible due to the reason I mentioned earlier.
Let me know what you think of the suggested approach and there is a POC as well. And guess what, we can tweak the approach suggested in the IB to generate the same stories.json file that sb extract generates but in a much faster way and not having to build storybook.
- Install the
globpackage to get the list of file names ending with.stories.jsgetting the paths of the story files from.storybook/main.js. Get the list of *.stories.js files using the glob.sync function and loop through them.
I tried to see if Storybook has a utility we could import for this but it wasn't very obvious what they use for loading the stories so maybe this is available, but I couldn't find it π That said, main.js should be the source of truth as to where the stories are loaded from, we don't need to additionally glob/filter by *.stories.js even though they all should match that, the scenarios here doesn't need to have any awareness of what the specific path(s) are since that is entirely handled by the storybook config. Not sure if you meant doing that or not but just thought I'd clarify.
Install the @babel/core package which will also install the @babel/parser and @babel/traverse packages. Those packages will be used to parse the stories to look for named exports and their properties.
I really hoped Storybook provided a utility to do this so we wouldn't have to but it looks like they rely on webpack to load stories. Even the extract command uses puppeteer to load the iframe to get at the story data π
The story details should be an object having the same keys as in
.storybook/storybook-data.jsand as defined in the AC.
This is unnecessary, in addition to the scenario definition, we only need the story's id, kind, and name. The kind and name are only needed as the default value of the scenario's label as it is constructed today. However, a scenario.label should take precedence if provided.
Thanks for the POC branch, I think this will work but needs some adjustments per above but let's hold on updating that until the IB is solidified. Otherwise, I think this is close to ready for a PR and maybe 19 is a bit generous of an estimate relative to the effort remaining.
@aaemnnosttv About the following:
That said, main.js should be the source of truth as to where the stories are loaded from, we don't need to additionally glob/filter by *.stories.js even though they all should match that, the scenarios here doesn't need to have any awareness of what the specific path(s) are since that is entirely handled by the storybook config. Not sure if you meant doing that or not but just thought I'd clarify.
main.js only exports paths where stories are loaded but actually none of the stories are loaded. So glob is used to get all the *.stories.js files within the paths mentioned in main.js. And then with the paths, the files are read and parsed using babel's parser. Or you had something different in mind?
main.jsonly exports paths where stories are loaded but actually none of the stories are loaded. Soglobis used to get all the*.stories.jsfiles within the paths mentioned inmain.js. And then with the paths, the files are read and parsed using babel's parser. Or you had something different in mind?
@asvinb No, that's correct, it's just that in your original PR the glob was hardcoded rather than coming from the globs exported from main.js π
IB β
Just FYI https://github.com/google/site-kit-wp/issues/3508 is in CR and is implementing backstop tests the old fashioned way, so if that's still in CR when this is started, please take a look at that one too to make sure we don't miss it.
.storybook/main.js contains relative file paths relative to the location of .storybook/main.js
So running glob with this returns no results of course. (Presumably that's why @asvinb's snippet doesn't do this)
For now, I'll just ignore this step. I think the paths should be defined from the root :thinking:
I'm not sure what the scenario property should be:
"This property should be a plain object " "If defined with an object, this configuration should add a backstop scenario" "For stories that do not require any custom scenario configuration, an empty object should be set"
What is "defined with an object" vs "an empty object should be set"
I would have thought this would just be a boolean. If set to true, add a VRT, if set to false or null/undefined, then don't
I'm going to code this and see if I'm missing something with this approach
@danielgent the scenario property is intended to allow for the backstop scenario definition to be extended (e.g. onReadyScript, delay, etc). So, it could also be a boolean, that would be fine for probably the majority of stories that don't require additional configuration. Looks like this wasn't clear in the ACs, so I'll revise it π
.storybook/main.jscontains relative file paths relative to the location of.storybook/main.jsFor now, I'll just ignore this step. I think the paths should be defined from the root :thinking:
We can still do this, the globs just need to be resolved relative to the .storybook directory but the important thing is that we use the paths defined there for the collection of stories.
@danielgent I've revised the AC's to clarify and added the option to allow for a boolean scenario property which I agree is a bit cleaner for the common case where no custom options are needed π
Thanks @aaemnnosttv
Do we want the current VRTs to keep passing with no changes?
That's what I assumed (and that's what I'd think is better)
I used @asvinb's provided snippet which mostly worked (thanks @asvin!), only two things were
- optional chaining operator not supported on storybook babel config
- had to initalise empty stories objects
stories[ node.left.object.name ] = {}; - The Id wasn't correct. This needs to match up with the storybook Url, so I added a camelCase to kebab-case helper and used the component name to create the id all mentioned on linked PR
The other changes I made I'm not too sure about
Here is a PR where I've moved all the backstop definitions into DashboardCoreSiteAlerts.stories.js https://github.com/google/site-kit-wp/pull/3782
Please see my comments on that PR, and note that there are no new VRT snapshots nor are the VRT tests failing
But that's a bit different to what you commented above though.
Do we want the current VRTs to keep passing with no changes?
@danielgent Yes, there should be no changes to existing VRTs π
I used @asvinb's provided snippet which mostly worked (thanks @asvin!), only two things were
The points you mentioned sound okay to me.
The other changes I made I'm not too sure about Here is a PR where I've moved all the backstop definitions into DashboardCoreSiteAlerts.stories.js #3782 Please see my comments on that PR, and note that there are no new VRT snapshots nor are the VRT tests failing
I'll respond inline to your comments there, but this looks like it's off to a good start π Thanks!
@aaemnnosttv I've got this all working now except setting scenario to true
It works fine if set as an object (even an empty object) but when setting to true, the parsing code always returns it as an empty string
See comments on the PR. Happy to investigate more and learn about these libraries but who knows how long it'll take! :smile:
QA β Verified that VRTs are passing