site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Introduce new integration between Storybook and Backstop

Open aaemnnosttv opened this issue 5 years ago β€’ 16 comments

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 args or parameters, etc by defining a scenario property
    • This property should be a boolean or a plain object (similar to the way parameters.options is now)
      • If true the scenario for that story should be added using the default/common scenario definition (see below)
      • false should yield the same as if there was no scenario property 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.url which must be based on the Story's id
        • This format is intended for stories that require custom options for delay, etc
  • 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.scenario should be added to the list of scenarios
      • Each scenario added in this way should have default values populated for its label and url based on the story which are required by Backstop
      • The utilities for determining a story's name and id (needed for the label and url) when not explicitly provided are sourced from this package: @componentdriven/csf
      • The story kind is the same as the story's defaultExport.title
  • 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.js should be removed

Implementation Brief

  • Using tests/backstop/scenarios.js,
  • Install the glob package to get the list of file names ending with .stories.js getting the paths of the story files from .storybook/main.js.
  • 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.
  • Get the list of *.stories.js files using the glob.sync function and loop through them.
  • Read the file using the readFileSync function and use the parse function from @babel/parser to parse the code.
  • Using the traverse function from @babel/traverse, walk through the parsed code looking for ExportDefaultDeclaration and AssignmentExpression node types, grabbing the necessary properties and adding them to an array (stories). The properties namely are:
    • the title and component from the default export (ExportDefaultDeclaration)
    • any properties for a story via AssignmentExpression
  • 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 scenario property, then add the story details to the existing storybookStories array. The story details should be an object having the same keys as in .storybook/storybook-data.js and as defined in the AC.
  • Remove stories which have the scenario property from .storybook/storybook-data.js as 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

aaemnnosttv avatar Apr 19 '21 09:04 aaemnnosttv

@felixarntz anything to add here?

aaemnnosttv avatar Apr 19 '21 09:04 aaemnnosttv

@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.

felixarntz avatar Apr 19 '21 17:04 felixarntz

@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.

asvinb avatar Apr 30 '21 10:04 asvinb

  • Install the glob package to get the list of file names ending with .stories.js getting 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.js and 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 avatar May 07 '21 08:05 aaemnnosttv

@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?

asvinb avatar May 12 '21 14:05 asvinb

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?

@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 πŸ™‚

aaemnnosttv avatar May 12 '21 17:05 aaemnnosttv

IB βœ…

aaemnnosttv avatar Jun 11 '21 08:06 aaemnnosttv

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.

ivankruchkoff avatar Jun 21 '21 16:06 ivankruchkoff

.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:

danielgent avatar Jul 30 '21 12:07 danielgent

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 avatar Jul 30 '21 14:07 danielgent

@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.js contains relative file paths relative to the location of .storybook/main.js For 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.

aaemnnosttv avatar Jul 30 '21 15:07 aaemnnosttv

@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 πŸ‘

aaemnnosttv avatar Jul 30 '21 15:07 aaemnnosttv

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

  1. optional chaining operator not supported on storybook babel config
  2. had to initalise empty stories objects stories[ node.left.object.name ] = {};
  3. 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.

danielgent avatar Jul 30 '21 16:07 danielgent

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 avatar Aug 02 '21 09:08 aaemnnosttv

@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:

danielgent avatar Aug 03 '21 09:08 danielgent

QA βœ… Verified that VRTs are passing

johnPhillips avatar Aug 09 '21 16:08 johnPhillips