Skip to content
This repository was archived by the owner on Nov 19, 2026. It is now read-only.

Extend hooks api with prepare and getHttpHeaders properties - #245

Merged
yannbf merged 4 commits into
nextfrom
feat/provide-prepare-api-and-browser-context
Mar 6, 2023
Merged

yannbf merged 4 commits into
nextfrom
feat/provide-prepare-api-and-browser-context

Conversation

@yannbf

@yannbf yannbf commented Jan 20, 2023 •

Copy link
Copy Markdown
Member

Replaces #222

There are now additional properties you can set in .storybook/test-runner.js:

prepare

The test-runner has a default prepare function which gets the browser in the right environment before testing the stories. You can override this behavior, in case you might want to hack the behavior of the browser. For example, you might want to set a cookie, or add query parameters to the visiting URL, or do some authentication before reaching the Storybook URL. You can do that by overriding the prepare function.

The prepare function receives an object containing:

getHttpHeaders

The test-runner makes a few fetch calls to check the status of a Storybook instance, and to get the index of the Storybook's stories. Additionally, it visits a page using Playwright. In all of these scenarios, it's possible, depending on where your Storybook is hosted, that you might need to set some HTTP headers. For example, if your Storybook is hosted behind a basic authentication, you might need to set the Authorization header. You can do so by passing a getHttpHeaders function to your test-runner config. That function receives the url of the fetch calls and page visits, and should return an object with the headers to be set.

// .storybook/test-runner.js
module.exports = {
  getHttpHeaders: async (url) => {
    const token = url.includes('prod') ? 'XYZ' : 'ABC';
    return {
      Authorization: `Bearer ${token}`,
    };
  },
};
📦 Published PR as canary version: 0.9.5--canary.245.714d3ca.0

✨ Test out this PR locally via:

npm install @storybook/test-runner@0.9.5--canary.245.714d3ca.0
# or 
yarn add @storybook/test-runner@0.9.5--canary.245.714d3ca.0

Version

Published prerelease version: v0.10.0-next.10

Changelog

🚀 Enhancement

🐛 Bug Fix

📝 Documentation

Authors: 8

@yannbf yannbf added the feature request New feature or request label Jan 20, 2023
@yannbf
yannbf force-pushed the feat/provide-prepare-api-and-browser-context branch from acc88f0 to 89d8b97 Compare January 20, 2023 14:49
@yannbf

yannbf commented Jan 20, 2023

Copy link
Copy Markdown
Member Author

@davidferguson the prepare function would allow you to achieve what you wanted in #242, and gives you much more power over it. The drawback is that you will be responsible for the behavior of preparing the browser, which might change in future releases.

@davidferguson

Copy link
Copy Markdown

Thanks @yannbf, this exactly fits our needs! Looking forward to being able to use this once it's merged.

@ndelangen

Copy link
Copy Markdown
Member

@yannbf merge it?!

@davidferguson

Copy link
Copy Markdown

Bump, would love to see this merged at some point!

@shilman shilman assigned yannbf and unassigned ndelangen Feb 10, 2023
@davidferguson

Copy link
Copy Markdown

Just wanted to give this another bump!

@yannbf

yannbf commented Mar 6, 2023

Copy link
Copy Markdown
Member Author

Hey @davidferguson sorry for the long time to merge this, got quite busy with the Storybook day conference!

@yannbf yannbf added the patch Increment the patch version when merged label Mar 6, 2023
@yannbf
yannbf merged commit f2191b7 into next Mar 6, 2023
This was referenced Mar 31, 2023
@github-actions

github-actions Bot commented Apr 3, 2023

Copy link
Copy Markdown

🚀 PR was released in v0.10.0 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature request New feature or request patch Increment the patch version when merged released stress test Triggers stress tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants