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

Extend hooks api with prepare and getHttpHeaders properties #245

Merged
merged 4 commits into from Mar 6, 2023

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jan 20, 2023

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
Copy link
Member Author

yannbf commented Jan 20, 2023

@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

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

@ndelangen
Copy link
Member

@yannbf merge it?!

@davidferguson
Copy link

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

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

Just wanted to give this another bump!

@yannbf
Copy link
Member Author

yannbf commented Mar 6, 2023

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
7 checks passed
This was referenced Mar 31, 2023
@github-actions
Copy link

github-actions bot commented Apr 3, 2023

πŸš€ PR was released in v0.10.0 πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.

None yet

3 participants