-
Notifications
You must be signed in to change notification settings - Fork 72
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
[Bug] Tests always run in the default viewport #85
Comments
Thanks for using this library and opening this issue @schneefux! There's currently a limitation that won't allow you to get info from the viewports addon, hopefully we can add support for that soon. @shilman WDYT of adding parameters to the context? It will make stories-json mode feel more limited but it is what it is I guess |
Is that because parameters are not available in stories-json mode? This could be another good use for |
I just want to share that this is possible now with the getStoryContext/page.evaluate. I'm using it to get |
Thanks @VickyKoblinski! Your project must be super cool, given all the stuff you've been doing with the test runner! |
TODO: add a recipe for that ☝️ |
We got inspired by @VickyKoblinski's answer with little changes, our stories are configured with viewports imported or customized with
|
@ibrahimgabsi I want to note that when the viewPort is resized, it will not return to its original state for the following stories. So we have to call something like setViewportSize(globalDefaultSize) if no custom window is specified. |
I slightly improved solution of @ibrahimgabsi to use built-in viewport sizes or reset to default on pages without Create const { getStoryContext } = require('@storybook/test-runner');
const { MINIMAL_VIEWPORTS } = require('@storybook/addon-viewport');
const DEFAULT_VP_SIZE = { width: 1280, height: 720 };
module.exports = {
async preRender(page, story) {
const context = await getStoryContext(page, story);
const vpName = context.parameters?.viewport?.defaultViewport;
const vpParams = MINIMAL_VIEWPORTS[vpName];
if (vpParams) {
const vpSize = Object.entries(vpParams.styles).reduce(
(acc, [screen, size]) => ({
...acc,
[screen]: parseInt(size),
}),
{}
);
page.setViewportSize(vpSize);
} else {
page.setViewportSize(DEFAULT_VP_SIZE);
}
},
}; |
Hey peeps! I will close this issue as the solution is now available and its recipe has been documented in the README here. Thank you! |
Describe the bug
The test runner runs tests full screen inside the default playwright Chrome viewport. It does not resize the viewport according to the
viewport
parameter of the story.Steps to reproduce the behavior
I have a component where a dropdown is hidden on large screens using a media query. I configured the story to use a mobile2 viewport by default and wrote a play function to test the visibility of the element:
When I run storybook locally and use the interactions addon, the test runs fine, but when I run the test runner, it fails.
I debugged the test runner using this custom
test-runner-jest.config.js
,--maxWorkers 1
and by adding sleep timeouts after every line. I can see that the playwright Chrome window has the wrong size and that it loads the story iframe in fullscreen.Environment
@storybook/test-runner
0.0.4@storybook/testing-library
0.0.9Additional context
As a workaround, I wrote a prerender hook in
.storybook/test-runner.js
that resizes the playwright page if the test hasMobile
in its name. A solution that does not rely on magic strings would be better though.The text was updated successfully, but these errors were encountered: