Skip to content
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

feat: support DOM snapshot testing #117

Merged
merged 1 commit into from
Jun 6, 2022
Merged

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented May 31, 2022

This PR provides an extra argument to the postRender hook containing the story's inner HTML.
With this it will be possible to do DOM snapshot testing with:

// .storybook/test-runner.js
module.exports = {
  async postRender(page, context, element) {
    expect(element).toMatchSnapshot();
  },
};
πŸ“¦ Published PR as canary version: 0.1.2--canary.117.f7145f0.0

✨ Test out this PR locally via:

npm install @storybook/test-runner@0.1.2--canary.117.f7145f0.0
# or 
yarn add @storybook/test-runner@0.1.2--canary.117.f7145f0.0

Version

Published prerelease version: v0.1.2-next.0

Changelog

πŸ› Bug Fix

⚠️ Pushed to next

  • add fake failure for testing purposes (@yannbf)
  • chore: add reports when workflow fails (@yannbf)

πŸ“ Documentation

Authors: 2

@yannbf yannbf added the patch Increment the patch version when merged label May 31, 2022
@yannbf yannbf force-pushed the feat/dom-snapshot-testing branch from e52920f to c7c14d8 Compare May 31, 2022 14:36
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the HTML is available off the page, why pass it to the hook at all? Instead we could just have a recipe where the user does:

async postRender(page, context) {
  const elementHandler = await page.$("#root")
  const innerHTML = await elementHandler.innerHTML();
  expect(innerHTML).toMatchSnapshot();
}

The benefits are:

  1. Our API remains simple
  2. We only "pay" to execute that code when it's actually needed, instead of doing it for every render regardless of whether the hooks use it

WDYT?

@yannbf yannbf force-pushed the feat/dom-snapshot-testing branch from c7c14d8 to f7145f0 Compare June 1, 2022 09:19
@yannbf
Copy link
Member Author

yannbf commented Jun 1, 2022

If the HTML is available off the page, why pass it to the hook at all? Instead we could just have a recipe where the user does:

async postRender(page, context) {
  const elementHandler = await page.$("#root")
  const innerHTML = await elementHandler.innerHTML();
  expect(innerHTML).toMatchSnapshot();
}

The benefits are:

  1. Our API remains simple
  2. We only "pay" to execute that code when it's actually needed, instead of doing it for every render regardless of whether the hooks use it

WDYT?

Makes total sense. I updated the PR accordingly.
About the jest-serializer-html lib, WDYT about it? Should we have it as a dependency by default for a seamless experience or have that as a part of a recipe? It's 112.6kB minified.

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yannbf Normally I'd say make the serializer part of a recipe since not all users will actually use this. However, after reading your PR I think it makes more sense to include it for now, because it's zero config now. Great stuff! I think maybe we can revisit this decision later after the test runner is more mature.

@yannbf yannbf merged commit cc538a7 into next Jun 6, 2022
@yannbf yannbf mentioned this pull request Jun 21, 2022
@yannbf yannbf deleted the feat/dom-snapshot-testing branch November 18, 2022 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Increment the patch version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants