Add tests for broken stories
Feature Description
We use Storybook as the foundation of our visual regression tests but we also have many more stories than we have VRT scenarios. Currently VRT is the only test we have in place to prevent Storybook from being broken, but it only catches stories that have corresponding VRT scenarios which are almost entirely all legacy stories. It doesn't make sense to enable VRT for every story, but we should at least still be able to require that no stories are broken which is a problem that keeps coming up.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- A new
test:storybookcommand should be created, which when run will check every story to see if it's broken or not- The command should list all broken stories by default although an option to fail on the first one found would be nice
- If a broken story is found, the command should return with a non-zero exit code
- Any new test scripts/config/files should go in a new
tests/storybookdirectory - A broken story is simply one that results in a
sb-show-errordisplayclass on thebodyelement in the preview iframe โย this frame should always be loaded directly (like it is in VRT) rather than in the larger view we normally use
- The new command should be integrated into our current GitHub actions so that broken stories cause the Storybook workflow to fail
Implementation Brief
- Install the
@storybook/addon-storyshots-puppeteerpackage as dev-dependency along withpuppeteeras per the documentation. https://storybook.js.org/addons/@storybook/addon-storyshots-puppeteer - Create
.storybook/Stories.test.jswhich contains the code sample in the "Specifying the storybook URL" section of the documentation, using the local static build of storybook found in/distviafile://. - The
setupTimeoutandtestTimeoutvalues are15sby default and this can be reduced to5sas per the documentation. - Update the
parametersobject in.storybook/preview.jsto include the asynchronouspuppeteerTestfunction (refer to documentation), where the function checks whether thebodyof the rendered story contains thesb-show-errordisplayclass name. For e.g:expect( await page.$eval( 'body', ( el ) => el.classList.contains( 'sb-show-errordisplay' ) ) ).toBe( false ); - Update
package.jsonto include a new command,test:storybookwhich runs:npm run test:js -- .storybook/Stories.test.js - Using
.github/workflows/storybook.yml,- Add a new job
test-storybookwhich uses the code from the existingbuild-storybookjob with the following changes:- Instead of running
npm run build:storybook, run thenpm run test:storybookcommand. - Remove
Upload artifactssection.
- Instead of running
- Add a new job
Test Coverage
- No further tests to be added.
Visual Regression Changes
- N/A
QA Brief
This should maybe be a QA:Eng ticket as there are no user-facing changes
- Check that all checks are passing on GitHub
Changelog entry
- N/A
@felixarntz I think this one is probably overdue ๐ Anything to add/update before IB? cc: @tofumatt @eugene-manuilov
๐ Let's do it, lgtm!
A broken story is simply one that results in a sb-show-errordisplay class on the body element in the preview iframe โ this frame should always be loaded directly (like it is in VRT) rather than in the larger view we normally use
@aaemnnosttv sometimes widgets become broken in a different way. They don't throw an error, they display an incorrect state instead. Not sure how we can catch it without VRT :thinking:
@aaemnnosttv sometimes widgets become broken in a different way. They don't throw an error, they display an incorrect state instead. Not sure how we can catch it without VRT ๐ค
@eugene-manuilov Sure โย anything not covered by VRT will be possible to be incorrect which is also not good, but for the purposes of this issue, the intention is just to ensure we at least catch the glaringly obvious broken ones which have been missed many times before. We can start with this and see how we can best evolve the stability of our stories going forward.
Ok, sounds good to me. :+1:
IB looks good, I'm excited for this! ๐๐ป
IB โ
@felixarntz it would be great if we could get this into the next sprint ๐
Running "test:storybook": "npm run test:js -- .storybook/Stories.test.js", doesn't run any tests
It just gives Pattern: .storybook/Stories.test.js - 0 matches
See https://github.com/google/site-kit-wp/compare/infrastructure/3378-Add-tests-for-broken-stories
I've moved it to assets/js/modules/Stories.test.js for now, as this is picked up by the Jest config :+1:
I just had to add '<rootDir>/.storybook/?(*.)test.js', under testMatch in jest.config.js to pick up .storybook/Stories.test.js"
Thanks @danielgent โย one minor change to request (I see this in the IB now as well) โย let's keep the Stories lowercase since it's not a component; i.e. stories.test.js ๐ Thanks!
Amazing! this totally works

Now the problem is how to make a PR that will fail because we actually have broken stories @aaemnnosttv
I can comment them out or we can fix them first? :smile:
Example broken story https://google.github.io/site-kit-wp/storybook/develop/?path=/story/analytics-module-settings--no-tag-gtm-property-w-access
I'm sure I've created a ticket to fix these
I'll make a PR now anyway so we can see it failing
When running on a Github workflow though the steps in the IB aren't sufficient as we haven't built storybook and put it into a local dist folder
All the tests fail with
ERR! Error when connecting to file:///home/runner/work/site-kit-wp/site-kit-wp/dist/iframe.html?id=global--accordion, did you start or build the storybook first? A storybook instance should be running or a static version should be built when using puppeteer test feature.
I'm going to try copying a few steps now
- Downloading the built artifacts
- Building storybook
Downloading the artifacts didn't work (I get an error 'An Artifact with name "storybook-files" was not found.')
But building storybook first did work

Seems excessive to build twice though. We could just combine the Build and Test storybook jobs @aaemnnosttv
@danielgent it fails to download the artifact because the job needs to depend on the build job for that to be available. The job.needs is missing.
Regarding the broken stories, I mentioned on the others that it didn't make sense to fix them again and again until this issue was done so, it's somewhat out of scope but unless they're substantially too much effort let's just fix them as part of this PR.
Sure thing Evan
So next I'll do
- Add
job.needsso the storybook test step is dependent on the build step - Fix the broken stories if they aren't too difficult
Also I need to revisit my changes to jest.config.js as my changes to testMatch cause the JS Test step to fail :thinking: (as now it's running stories.test.js )
I'm not making any progress trying to fix these broken stories. I'm finding the code very cryptic. Helper functions are passed to helper functions and the registry isn't making it all the way through
On this line https://github.com/google/site-kit-wp/blob/develop/stories/module-analytics-settings.stories.js#L48
Component( { callback, registry } ) { - registry is null. That's as far as I've got :joy:
I don't see how generateGTMAnalyticsPropertyStory has ever passed in registry to the inner component
Honestly I would prefer to just get rid of this generateGTMAnalyticsPropertyStory. It's really hard to follow. This generateGTMAnalyticsPropertyStory just runs sets up the registry anyway, so I'll try extracting this and just running that code inline :sweat:
@danielgent I agree some of the generator utilities around the old stories are a bit hard to follow.
I'll try extracting this and just running that code inline
Please don't do this just yet if it requires substantial changes. If needed we can always handle the broken parts in a separate issue. I'll take a look and see if I can figure out what's going on there.
@danielgent I pushed a few commits which I think should fix things there. Let me know if you run into any other problems ๐