Skip to content

Commit

Permalink
Merge pull request #125 from storybookjs/yann/sb-308-test-runner97-pr…
Browse files Browse the repository at this point in the history
…ovide-a-method-to

feat: provide global getStoryContext utility
  • Loading branch information
yannbf committed Jun 21, 2022
2 parents 1d6cf27 + f7e708e commit cb53eb4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .storybook/test-runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import { getStoryContext } from '../dist/cjs/playwright/hooks';
import type { TestRunnerConfig } from '../dist/ts';

const snapshotsDir = process.env.SNAPSHOTS_DIR || '__snapshots__';
Expand All @@ -9,6 +10,14 @@ const config: TestRunnerConfig = {
expect.extend({ toMatchImageSnapshot });
},
async postRender(page, context) {
// Get entire context of a story, including parameters, args, argTypes, etc.
const { parameters } = await getStoryContext(page, context);

if (parameters?.tests?.disableSnapshots) {
console.log('skipping story ', context.id);
return;
}

// Visual snapshot tests
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot({
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,46 @@ it('button--basic', async () => {
});
```

### Global utility functions

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:

```js
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:

```js
// .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
})
},
};
```
## Troubleshooting
#### Errors with Jest 28
Expand Down
8 changes: 8 additions & 0 deletions src/playwright/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import global from 'global';
import type { Page } from 'playwright';
import type { StoryContext } from '@storybook/csf';

export type TestContext = {
id: string;
Expand All @@ -22,3 +23,10 @@ export const setPreRender = (preRender: TestHook) => {
export const setPostRender = (postRender: TestHook) => {
global.__sbPostRender = postRender;
};

export const getStoryContext = async (page: Page, context: TestContext): Promise<StoryContext> => {
// @ts-ignore
return page.evaluate(({ storyId }) => globalThis.__getContext(storyId), {
storyId: context.id,
});
};
6 changes: 5 additions & 1 deletion src/setup-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export const setupPage = async (page) => {
});
}
async function __getContext(storyId) {
return globalThis.__STORYBOOK_PREVIEW__.storyStore.loadStory({ storyId });
}
async function __test(storyId) {
try {
await __waitForElement('#root');
Expand All @@ -104,7 +108,7 @@ export const setupPage = async (page) => {
throw new StorybookTestRunnerError(storyId, message);
}
const channel = window.__STORYBOOK_ADDONS_CHANNEL__;
const channel = globalThis.__STORYBOOK_ADDONS_CHANNEL__;
if(!channel) {
throw new StorybookTestRunnerError(
storyId,
Expand Down
2 changes: 1 addition & 1 deletion src/util/getStorybookMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as storybookMain from './getStorybookMain';

import { getStorybookMetadata } from './getStorybookMetadata';

describe.only('getStorybookMetadata', () => {
describe('getStorybookMetadata', () => {
afterAll(() => {
process.env.STORYBOOK_CONFIG_DIR = undefined;
});
Expand Down
11 changes: 11 additions & 0 deletions stories/atoms/Button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ Primary.args = {
label: 'Button',
};

export const Secondary = Template.bind({});
Secondary.args = {
...Primary.args,
primary: false,
};
Secondary.parameters = {
tests: {
disableSnapshots: true
}
}

export const Demo = (args) => (
<button type="button" onClick={() => args.onSubmit('clicked')}>
Click
Expand Down

0 comments on commit cb53eb4

Please sign in to comment.