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

Docs: Test runner minor fixes #24645

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/snippets/common/my-component-exclude-tags.story.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```js
// MyComponent.stories.js|jsx

import { MyComponent } from './MyComponent';

export default {
component: MyComponent,
tags: ['no-tests'], // 👈 Excludes all stories from being tested with the test-runner
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved
};

export const ExcludeStory = {
tags: ['no-tests'], // 👈 Configures the story to be excluded from testing via the `no-tests` tag
};
```
20 changes: 20 additions & 0 deletions docs/snippets/common/my-component-exclude-tags.story.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```ts
// MyComponent.stories.ts|tsx

// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';

import { MyComponent } from './MyComponent';

const meta: Meta<typeof MyComponent> = {
component: MyComponent,
tags: ['no-tests'], // 👈 Excludes all stories from being tested with the test-runner
};

export default meta;
type Story = StoryObj<typeof MyComponent>;

export const ExcludeStory: Story = {
tags: ['no-tests'], // 👈 Configures the story to be excluded from testing via the `no-tests` tag
};
```
14 changes: 14 additions & 0 deletions docs/snippets/common/my-component-include-tags.story.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```js
// MyComponent.stories.js|jsx

import { MyComponent } from './MyComponent';

export default {
component: MyComponent,
tags: ['test-only'], // 👈 Runs tests only for stories with this tag
};

export const IncludeStory = {
tags: ['test-only'], // 👈 Configures the story to be added to the test suite with the test-runner
};
```
20 changes: 20 additions & 0 deletions docs/snippets/common/my-component-include-tags.story.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```ts
// MyComponent.stories.ts|tsx

// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';

import { MyComponent } from './MyComponent';

const meta: Meta<typeof MyComponent> = {
component: MyComponent,
tags: ['test-only'], // 👈 Runs tests only for stories with this tag
};

export default meta;
type Story = StoryObj<typeof MyComponent>;

export const IncludeStory: Story = {
tags: ['test-only'], // 👈 Configures the story to be added to the test suite with the test-runner
};
```
14 changes: 14 additions & 0 deletions docs/snippets/common/my-component-skip-tags.story.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```js
// MyComponent.stories.js|jsx

import { MyComponent } from './MyComponent';

export default {
component: MyComponent,
tags: ['skip-test'], // 👈 Skips running tests on this story
};

export const SkipStory = {
tags: ['skip-test'], // 👈 Configures the story to be skipped when running tests with the test-runner
};
```
20 changes: 20 additions & 0 deletions docs/snippets/common/my-component-skip-tags.story.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```ts
// MyComponent.stories.ts|tsx

// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';

import { MyComponent } from './MyComponent';

const meta: Meta<typeof MyComponent> = {
component: MyComponent,
tags: ['skip-test'], // 👈 Skips running tests on this story
};

export default meta;
type Story = StoryObj<typeof MyComponent>;

export const SkipStory: Story = {
tags: ['skip-test'], // 👈 Configures the story to be skipped when running tests with the test-runner
};
```
2 changes: 1 addition & 1 deletion docs/snippets/common/test-runner-a11y-config.js.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { injectAxe, checkA11y } = require('axe-playwright');

/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
module.exports = {
Expand Down
7 changes: 3 additions & 4 deletions docs/snippets/common/test-runner-a11y-config.ts.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
```ts
// .storybook/test-runner.ts

import { injectAxe, checkA11y } from 'axe-playwright';

import type { TestRunnerConfig } from '@storybook/test-runner';
import { injectAxe, checkA11y } from 'axe-playwright';

/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
const a11yConfig: TestRunnerConfig = {
Expand All @@ -23,5 +22,5 @@ const a11yConfig: TestRunnerConfig = {
},
};

module.exports = a11yConfig;
export default a11yConfig;
```
2 changes: 1 addition & 1 deletion docs/snippets/common/test-runner-a11y-configure.js.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { injectAxe, checkA11y, configureAxe } = require('axe-playwright');
const { getStoryContext } = require('@storybook/test-runner');

/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
module.exports = {
Expand Down
9 changes: 4 additions & 5 deletions docs/snippets/common/test-runner-a11y-configure.ts.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
```ts
// .storybook/test-runner.ts

import { injectAxe, checkA11y, configureAxe } from 'axe-playwright';

import type { TestRunnerConfig } from '@storybook/test-runner';
import { getStoryContext } from '@storybook/test-runner';

import type { TestRunnerConfig } from '@storybook/test-runner';
import { injectAxe, checkA11y, configureAxe } from 'axe-playwright';

/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
const a11yConfig: TestRunnerConfig = {
Expand All @@ -33,5 +32,5 @@ const a11yConfig: TestRunnerConfig = {
},
};

module.exports = a11yConfig;
export default a11yConfig;
```
5 changes: 2 additions & 3 deletions docs/snippets/common/test-runner-a11y-disable.js.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
```js
// .storybook/test-runner.js

const { injectAxe, checkA11y } = require('axe-playwright');

const { getStoryContext } = require('@storybook/test-runner');

const { injectAxe, checkA11y } = require('axe-playwright');
/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
module.exports = {
Expand Down
9 changes: 4 additions & 5 deletions docs/snippets/common/test-runner-a11y-disable.ts.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
```ts
// .storybook/test-runner.ts

import { injectAxe, checkA11y } from 'axe-playwright';

import type { TestRunnerConfig } from '@storybook/test-runner';
import { getStoryContext } from '@storybook/test-runner';

import type { TestRunnerConfig } from '@storybook/test-runner';
import { injectAxe, checkA11y } from 'axe-playwright';

/*
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
const a11yConfig: TestRunnerConfig = {
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -32,5 +31,5 @@ const a11yConfig: TestRunnerConfig = {
},
};

module.exports = a11yConfig;
export default a11yConfig;
```
12 changes: 12 additions & 0 deletions docs/snippets/common/test-runner-auth.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```js
// .storybook/test-runner.js

module.exports = {
getHttpHeaders: async (url) => {
const token = url.includes('prod') ? 'XYZ' : 'ABC';
return {
Authorization: `Bearer ${token}`,
};
},
};
```
16 changes: 16 additions & 0 deletions docs/snippets/common/test-runner-auth.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```ts
// .storybook/test-runner.ts

import type { TestRunnerConfig } from '@storybook/test-runner';

const config: TestRunnerConfig = {
getHttpHeaders: async (url) => {
const token = url.includes('prod') ? 'prod-token' : 'dev-token';
return {
Authorization: `Bearer ${token}`,
};
},
};

export default config;
```
32 changes: 32 additions & 0 deletions docs/snippets/common/test-runner-custom-page-viewport.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
```js
// .storybook/test-runner.js

const { getStoryContext } = require('@storybook/test-runner');
const { MINIMAL_VIEWPORTS } = require('@storybook/addon-viewport');

const DEFAULT_VIEWPORT_SIZE = { width: 1280, height: 720 };

module.exports = {
async preRender(page, story) {
// Accesses the story's parameters and retrieves the viewport used to render it
const context = await getStoryContext(page, story);
const viewportName = context.parameters?.viewport?.defaultViewport;
const viewportParameter = MINIMAL_VIEWPORTS[viewportName];

if (viewportParameter) {
const viewportSize = Object.entries(viewportParameter.styles).reduce(
(acc, [screen, size]) => ({
...acc,
// Converts the viewport size from percentages to numbers
[screen]: parseInt(size),
}),
{}
);
// Configures the Playwright page to use the viewport size
page.setViewportSize(viewportSize);
} else {
page.setViewportSize(DEFAULT_VIEWPORT_SIZE);
}
},
};
```
36 changes: 36 additions & 0 deletions docs/snippets/common/test-runner-custom-page-viewport.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
```ts
// .storybook/test-runner.js

import type { TestRunnerConfig } from '@storybook/test-runner';
import { getStoryContext } from '@storybook/test-runner';

const { MINIMAL_VIEWPORTS } = require('@storybook/addon-viewport');

const DEFAULT_VIEWPORT_SIZE = { width: 1280, height: 720 };

const config: TestRunnerConfig = {
async preRender(page, story) {
// Accesses the story's parameters and retrieves the viewport used to render it
const context = await getStoryContext(page, story);
const viewportName = context.parameters?.viewport?.defaultViewport;
const viewportParameter = MINIMAL_VIEWPORTS[viewportName];

if (viewportParameter) {
const viewportSize = Object.entries(viewportParameter.styles).reduce(
(acc, [screen, size]) => ({
...acc,
// Converts the viewport size from percentages to numbers
[screen]: parseInt(size),
}),
{}
);
// Configures the Playwright page to use the viewport size
page.setViewportSize(viewportSize);
} else {
page.setViewportSize(DEFAULT_VIEWPORT_SIZE);
}
},
};

export default config;
```
5 changes: 4 additions & 1 deletion docs/snippets/common/test-runner-helper-function.js.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```js
// .storybook/test-runner.js

const { getStoryContext } = require('@storybook/test-runner');
const { getStoryContext, waitForPageReady } = require('@storybook/test-runner');

module.exports = {
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved
// Hook that is executed before the test runner starts running tests
Expand All @@ -23,6 +23,9 @@ module.exports = {
// Get the entire context of a story, including parameters, args, argTypes, etc.
const storyContext = await getStoryContext(page, context);

// This utility function is designed for image snapshot testing. It will wait for the page to be fully loaded, including all the async items (e.g., images, fonts, etc.).
await waitForPageReady(page);

// Add your configuration here.
},
};
Expand Down
8 changes: 5 additions & 3 deletions docs/snippets/common/test-runner-helper-function.ts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// .storybook/test-runner.ts

import type { TestRunnerConfig } from '@storybook/test-runner';

import { getStoryContext } from '@storybook/test-runner';
import { getStoryContext, waitForPageReady } from '@storybook/test-runner';

const config: TestRunnerConfig = {
// Hook that is executed before the test runner starts running tests
Expand All @@ -25,9 +24,12 @@ const config: TestRunnerConfig = {
// Get the entire context of a story, including parameters, args, argTypes, etc.
const storyContext = await getStoryContext(page, context);

// This utility function is designed for image snapshot testing. It will wait for the page to be fully loaded, including all the async items (e.g., images, fonts, etc.).
await waitForPageReady(page);
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved

// Add your configuration here.
},
};

module.exports = config;
export default config;
```
2 changes: 1 addition & 1 deletion docs/snippets/common/test-runner-hooks-example.ts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ const config: TestRunnerConfig = {
},
};

module.exports = config;
export default config;
```
11 changes: 11 additions & 0 deletions docs/snippets/common/test-runner-tags-config.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```js
// .storybook/test-runner.js

module.exports = {
tags: {
include: ['test-only', 'pages'],
exclude: ['no-tests', 'tokens'],
skip: ['skip-test', 'layout'],
},
};
```
15 changes: 15 additions & 0 deletions docs/snippets/common/test-runner-tags-config.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```ts
// .storybook/test-runner.ts

import type { TestRunnerConfig } from '@storybook/test-runner';

const config: TestRunnerConfig = {
tags: {
include: ['test-only', 'pages'],
exclude: ['no-tests', 'tokens'],
skip: ['skip-test', 'layout'],
},
};

export default config;
```
Loading