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: provide global getStoryContext utility #125

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jun 20, 2022

Issue #97

Release notes

Features

getStoryContext utility #125

While running tests using the hooks, you might want to get information from a story, such as the parameters passed to it, or its args. The test runner now provides a getStoryContext utility function that fetches the story context for the current story:

await getStoryContext(page, context);

You can use it for multiple use cases, and here's an example that combines the story context and accessibility testing:

// .storybook/test-runner.js
const { getStoryContext } = require('@storybook/test-runner');
const { injectAxe, checkA11y } = require('axe-playwright');
 
module.exports = {
 async preRender(page, context) {
   await injectAxe(page);
 },
 async postRender(page, context) {
  // Get entire context of a story, including parameters, args, argTypes, etc.
  const storyContext = await getStoryContext(page, context);
  // Do not test a11y for stories that disable a11y
  if (storyContext.parameters?.a11y?.disable) {
    return;
  }
  
   await checkA11y(page, '#root', {
     detailedReport: true,
     detailedReportOptions: {
       html: true,
     },
     // pass axe options defined in @storybook/addon-a11y
     axeOptions: storyContext.parameters?.a11y?.options
   })
 },
};
<!-- GITHUB_RELEASE PR BODY: canary-version -->
<details>
  <summary>📦 Published PR as canary version: <code>0.2.0--canary.125.f7e708e.0</code></summary>
  <br />

  :sparkles: Test out this PR locally via:
  
  ```bash
  npm install @storybook/[email protected]
  # or 
  yarn add @storybook/[email protected]

Version

Published prerelease version: v0.2.0-next.0

Changelog

Release Notes

feat: provide global getStoryContext utility (#125)


🚀 Enhancement

  • feat: provide global getStoryContext utility #125 (@yannbf)

🐛 Bug Fix

⚠️ Pushed to next

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

📝 Documentation

Authors: 3

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.

LOVE LOVE LOVE ❤️

@yannbf yannbf force-pushed the yann/sb-308-test-runner97-provide-a-method-to branch 2 times, most recently from 4c38a87 to 7fdef2d Compare June 20, 2022 21:31
@yannbf
Copy link
Member Author

yannbf commented Jun 20, 2022

@shilman I reworked on the API to make it more legible, please re-check! 🙏

@yannbf yannbf added the minor Increment the minor version when merged label Jun 21, 2022
@yannbf yannbf force-pushed the yann/sb-308-test-runner97-provide-a-method-to branch from 7fdef2d to f7c0433 Compare June 21, 2022 08:37
@yannbf yannbf force-pushed the yann/sb-308-test-runner97-provide-a-method-to branch from f7c0433 to f7e708e Compare June 21, 2022 08:42
@yannbf yannbf merged commit cb53eb4 into next Jun 21, 2022
@yannbf yannbf deleted the yann/sb-308-test-runner97-provide-a-method-to 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
minor Increment the minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants