Skip to content

Commit

Permalink
Merge pull request #94 from czibbelvin/fix-preview-extract
Browse files Browse the repository at this point in the history
Ensure preview is ready before moving on to extract
  • Loading branch information
ahuth committed May 13, 2024
2 parents 83dcceb + 76c726b commit 2a92e05
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions src/browser/StorybookPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,44 @@ function fetchStoriesFromWindow(): Promise<StorybookStory[]> {
);
}

return storybookPreview.extract().then(() => {
// Pick only the properties we need from Storybook's representation of a story.
//
// This is necessary because Playwright's `page.evaluate` requires return values to be JSON
// serializable, so we need to make sure there are no non-serializable things in this object.
// There's no telling what Storybook addons people are using, and whether their parameters are
// serializable or not.
//
// See https://github.com/chanzuckerberg/axe-storybook-testing/issues/44 for a bug caused by this.
function pickOnlyNecessaryAndSerializableStoryProperties(
story: Story,
): StorybookStory {
return {
id: story.id,
name: story.name,
title: story.title,
parameters: {
axe: story.parameters.axe,
},
};
}

const storyStore = storybookPreview.storyStore as StoryStore<Renderer>;

return storyStore
.raw()
.map(pickOnlyNecessaryAndSerializableStoryProperties);
});
let readyPromise: Promise<void> = Promise.resolve();
if (storybookPreview.ready) {
readyPromise = storybookPreview.ready();
} else {
// @ts-expect-error initializationPromise doesn't exist in v8, but it does in v7.
readyPromise = storybookPreview.storyStore.initializationPromise;
}

return readyPromise.then(() =>
storybookPreview.extract().then(() => {
// Pick only the properties we need from Storybook's representation of a story.
//
// This is necessary because Playwright's `page.evaluate` requires return values to be JSON
// serializable, so we need to make sure there are no non-serializable things in this object.
// There's no telling what Storybook addons people are using, and whether their parameters are
// serializable or not.
//
// See https://github.com/chanzuckerberg/axe-storybook-testing/issues/44 for a bug caused by this.
function pickOnlyNecessaryAndSerializableStoryProperties(
story: Story,
): StorybookStory {
return {
id: story.id,
name: story.name,
title: story.title,
parameters: {
axe: story.parameters.axe,
},
};
}

const storyStore = storybookPreview.storyStore as StoryStore<Renderer>;

return storyStore
.raw()
.map(pickOnlyNecessaryAndSerializableStoryProperties);
}),
);
}

/**
Expand Down

0 comments on commit 2a92e05

Please sign in to comment.