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

Add tests for broken stories

Open aaemnnosttv opened this issue 5 years ago โ€ข 22 comments

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:storybook command 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/storybook directory
    • 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
  • 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-puppeteer package as dev-dependency along with puppeteer as per the documentation. https://storybook.js.org/addons/@storybook/addon-storyshots-puppeteer
  • Create .storybook/Stories.test.js which contains the code sample in the "Specifying the storybook URL" section of the documentation, using the local static build of storybook found in /dist via file://.
  • The setupTimeout and testTimeout values are 15s by default and this can be reduced to 5s as per the documentation.
  • Update the parameters object in .storybook/preview.js to include the asynchronous puppeteerTest function (refer to documentation), where the function checks whether the body of the rendered story contains the sb-show-errordisplay class name. For e.g:
    expect( await page.$eval( 'body', ( el ) => el.classList.contains( 'sb-show-errordisplay' ) ) ).toBe( false );
    
  • Update package.json to include a new command, test:storybook which runs:
    npm run test:js -- .storybook/Stories.test.js
    
  • Using .github/workflows/storybook.yml,
    • Add a new job test-storybook which uses the code from the existing build-storybook job with the following changes:
      • Instead of running npm run build:storybook, run the npm run test:storybook command.
      • Remove Upload artifacts section.

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

aaemnnosttv avatar May 19 '21 09:05 aaemnnosttv

@felixarntz I think this one is probably overdue ๐Ÿ˜„ Anything to add/update before IB? cc: @tofumatt @eugene-manuilov

aaemnnosttv avatar May 19 '21 09:05 aaemnnosttv

๐Ÿ‘ Let's do it, lgtm!

felixarntz avatar May 19 '21 16:05 felixarntz

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:

eugene-manuilov avatar May 19 '21 16:05 eugene-manuilov

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

aaemnnosttv avatar May 19 '21 20:05 aaemnnosttv

Ok, sounds good to me. :+1:

eugene-manuilov avatar May 20 '21 17:05 eugene-manuilov

IB looks good, I'm excited for this! ๐Ÿ‘๐Ÿป

IB โœ…

tofumatt avatar Jun 16 '21 15:06 tofumatt

@felixarntz it would be great if we could get this into the next sprint ๐Ÿ™‚

aaemnnosttv avatar Jul 07 '21 10:07 aaemnnosttv

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

danielgent avatar Aug 03 '21 11:08 danielgent

I've moved it to assets/js/modules/Stories.test.js for now, as this is picked up by the Jest config :+1:

danielgent avatar Aug 03 '21 11:08 danielgent

I just had to add '<rootDir>/.storybook/?(*.)test.js', under testMatch in jest.config.js to pick up .storybook/Stories.test.js"

danielgent avatar Aug 03 '21 11:08 danielgent

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!

aaemnnosttv avatar Aug 03 '21 12:08 aaemnnosttv

Amazing! this totally works

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:

danielgent avatar Aug 03 '21 12:08 danielgent

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

danielgent avatar Aug 03 '21 12:08 danielgent

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

  1. Downloading the built artifacts
  2. Building storybook

danielgent avatar Aug 03 '21 12:08 danielgent

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

build first

Seems excessive to build twice though. We could just combine the Build and Test storybook jobs @aaemnnosttv

danielgent avatar Aug 03 '21 12:08 danielgent

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

aaemnnosttv avatar Aug 03 '21 13:08 aaemnnosttv

Sure thing Evan

So next I'll do

  1. Add job.needs so the storybook test step is dependent on the build step
  2. 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 )

danielgent avatar Aug 03 '21 13:08 danielgent

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 avatar Aug 03 '21 16:08 danielgent

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

aaemnnosttv avatar Aug 04 '21 06:08 aaemnnosttv

@danielgent I pushed a few commits which I think should fix things there. Let me know if you run into any other problems ๐Ÿ‘

aaemnnosttv avatar Aug 04 '21 08:08 aaemnnosttv