Skip to content

Commit 6db764f

Browse files
committed
update documentation to use Typescript
1 parent b2fb6a4 commit 6db764f

File tree

3 files changed

+88
-63
lines changed

3 files changed

+88
-63
lines changed

MIGRATION.test-runner.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- [Storyshots x Test Runner Comparison table](#storyshots-x-test-runner-comparison-table)
1010
- [Migration Steps](#migration-steps)
1111
- [Replacing `@storybook/addon-storyshots` with `@storybook/test-runner`:](#replacing-storybookaddon-storyshots-with-storybooktest-runner)
12-
- [Migrating storyshots features](#migrating-storyshots-features)
12+
- [Migrating Storyshots features](#migrating-storyshots-features)
1313
- [Smoke testing](#smoke-testing)
1414
- [Accessibility testing](#accessibility-testing)
1515
- [Image snapshot testing](#image-snapshot-testing)
@@ -135,7 +135,7 @@ import { getJestConfig } from '@storybook/test-runner';
135135
const defaultConfig = getJestConfig();
136136

137137
const config = {
138-
// The default configuration comes from @storybook/test-runner
138+
// The default Jest configuration comes from @storybook/test-runner
139139
...defaultConfig,
140140
snapshotResolver: './snapshot-resolver.js',
141141
};

README.md

Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ const testRunnerConfig = getJestConfig();
204204
* @type {import('@jest/types').Config.InitialOptions}
205205
*/
206206
module.exports = {
207-
// The default configuration comes from @storybook/test-runner
207+
// The default Jest configuration comes from @storybook/test-runner
208208
...testRunnerConfig,
209209
/** Add your own overrides below
210210
* @see https://jestjs.io/docs/configuration
@@ -299,10 +299,13 @@ If your Storybook does not have a `stories.json` file, you can generate one, pro
299299

300300
To enable `stories.json` in your Storybook, set the `buildStoriesJson` feature flag in `.storybook/main.js`:
301301

302-
```js
303-
module.exports = {
302+
```ts
303+
// .storybook/main.ts
304+
const config = {
305+
// ... rest of the config
304306
features: { buildStoriesJson: true },
305307
};
308+
export default config;
306309
```
307310

308311
Once you have a valid `stories.json` file, your Storybook will be compatible with the "index.json mode".
@@ -409,12 +412,13 @@ yarn add -D @storybook/addon-coverage
409412

410413
And register it in your `.storybook/main.js` file:
411414

412-
```js
413-
// .storybook/main.js
414-
module.exports = {
415+
```ts
416+
// .storybook/main.ts
417+
const config = {
415418
// ...rest of your code here
416419
addons: ['@storybook/addon-coverage'],
417420
};
421+
export default config;
418422
```
419423

420424
The addon has default options that might suffice for your project, and it accepts an [options object for project-specific configuration](https://github.com/storybookjs/addon-coverage#configuring-the-addon).
@@ -555,13 +559,16 @@ All three functions can be set up in the configuration file `.storybook/test-run
555559

556560
Async function that executes once before all the tests run. Useful for setting node-related configuration, such as extending Jest global `expect` for accessibility matchers.
557561

558-
```js
559-
// .storybook/test-runner.js
560-
module.exports = {
562+
```ts
563+
// .storybook/test-runner.ts
564+
import type { TestRunnerConfig } from '@storybook/test-runner';
565+
566+
const config: TestRunnerConfig = {
561567
async setup() {
562568
// execute whatever you like, in Node, once before all tests run
563569
},
564570
};
571+
export default config;
565572
```
566573

567574
#### preRender (deprecated)
@@ -574,13 +581,16 @@ module.exports = {
574581
Async function that receives a [Playwright Page](https://playwright.dev/docs/pages) and a context object with the current story's `id`, `title`, and `name`.
575582
Executes within a test before the story is rendered. Useful for configuring the Page before the story renders, such as setting up the viewport size.
576583

577-
```js
578-
// .storybook/test-runner.js
579-
module.exports = {
584+
```ts
585+
// .storybook/test-runner.ts
586+
import type { TestRunnerConfig } from '@storybook/test-runner';
587+
588+
const config: TestRunnerConfig = {
580589
async preVisit(page, context) {
581590
// execute whatever you like, before the story renders
582591
},
583592
};
593+
export default config;
584594
```
585595

586596
#### postRender (deprecated)
@@ -593,13 +603,16 @@ module.exports = {
593603
Async function that receives a [Playwright Page](https://playwright.dev/docs/pages) and a context object with the current story's `id`, `title`, and `name`.
594604
Executes within a test after a story is rendered. Useful for asserting things after the story is rendered, such as DOM and image snapshotting.
595605

596-
```js
597-
// .storybook/test-runner.js
598-
module.exports = {
606+
```ts
607+
// .storybook/test-runner.ts
608+
import type { TestRunnerConfig } from '@storybook/test-runner';
609+
610+
const config: TestRunnerConfig = {
599611
async postVisit(page, context) {
600612
// execute whatever you like, after the story renders
601613
},
602614
};
615+
export default config;
603616
```
604617

605618
> **Note**
@@ -609,7 +622,7 @@ module.exports = {
609622

610623
To visualize the test lifecycle with these hooks, consider a simplified version of the test code automatically generated for each story in your Storybook:
611624

612-
```js
625+
```ts
613626
// executed once, before the tests
614627
await setup();
615628
@@ -620,13 +633,13 @@ it('button--basic', async () => {
620633
// playwright page https://playwright.dev/docs/pages
621634
await page.goto(STORYBOOK_URL);
622635
623-
// pre-render hook
636+
// pre-visit hook
624637
if (preVisit) await preVisit(page, context);
625638
626-
// render the story and run its play function (if applicable)
639+
// render the story and watch its play function (if applicable)
627640
await page.execute('render', context);
628641
629-
// post-render hook
642+
// post-visit hook
630643
if (postVisit) await postVisit(page, context);
631644
});
632645
```
@@ -654,31 +667,37 @@ For reference, please use the [default `prepare`](https://github.com/storybookjs
654667

655668
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.
656669

657-
```js
658-
// .storybook/test-runner.js
659-
module.exports = {
670+
```ts
671+
// .storybook/test-runner.ts
672+
import type { TestRunnerConfig } from '@storybook/test-runner';
673+
674+
const config: TestRunnerConfig = {
660675
getHttpHeaders: async (url) => {
661676
const token = url.includes('prod') ? 'XYZ' : 'ABC';
662677
return {
663678
Authorization: `Bearer ${token}`,
664679
};
665680
},
666681
};
682+
export default config;
667683
```
668684

669685
#### tags
670686

671687
The `tags` property contains three options: `include | exclude | skip`, each accepting an array of strings:
672688

673-
```js
674-
// .storybook/test-runner.js
675-
module.exports = {
689+
```ts
690+
// .storybook/test-runner.ts
691+
import type { TestRunnerConfig } from '@storybook/test-runner';
692+
693+
const config: TestRunnerConfig = {
676694
tags: {
677695
include: [], // string array, e.g. ['test-only']
678696
exclude: [], // string array, e.g. ['design', 'docs-only']
679697
skip: [], // string array, e.g. ['design']
680698
},
681699
};
700+
export default config;
682701
```
683702

684703
`tags` are used for filtering your tests. Learn more [here](#filtering-tests-experimental).
@@ -693,7 +712,7 @@ While running tests using the hooks, you might want to get information from a st
693712

694713
Suppose your story looks like this:
695714

696-
```js
715+
```ts
697716
// ./Button.stories.ts
698717

699718
export const Primary = {
@@ -705,12 +724,12 @@ export const Primary = {
705724

706725
You can access its context in a test hook like so:
707726

708-
```js
709-
// .storybook/test-runner.js
710-
const { getStoryContext } = require('@storybook/test-runner');
727+
```ts
728+
// .storybook/test-runner.ts
729+
import { TestRunnerConfig, getStoryContext } from '@storybook/test-runner';
711730

712-
module.exports = {
713-
async postVisit(page, context) {
731+
const config: TestRunnerConfig = {
732+
async postRender(page, context) {
714733
// Get entire context of a story, including parameters, args, argTypes, etc.
715734
const storyContext = await getStoryContext(page, context);
716735
if (storyContext.parameters.theme === 'dark') {
@@ -720,6 +739,7 @@ module.exports = {
720739
}
721740
},
722741
};
742+
export default config;
723743
```
724744

725745
It's useful for skipping or enhancing use cases like [image snapshot testing](#image-snapshot), [accessibility testing](#accessibility-testing) and more.
@@ -728,25 +748,26 @@ It's useful for skipping or enhancing use cases like [image snapshot testing](#i
728748

729749
The `waitForPageReady` utility is useful when you're executing [image snapshot testing](#image-snapshot) with the test-runner. It encapsulates a few assertions to make sure the browser has finished downloading assets.
730750

731-
```js
732-
// .storybook/test-runner.js
733-
const { waitForPageReady } = require('@storybook/test-runner');
751+
```ts
752+
// .storybook/test-runner.ts
753+
import { TestRunnerConfig, waitForPageReady } from '@storybook/test-runner';
734754

735-
module.exports = {
736-
async postVisit(page, context) {
755+
const config: TestRunnerConfig = {
756+
async postRender(page, context) {
737757
// use the test-runner utility to wait for fonts to load, etc.
738758
await waitForPageReady(page);
739759

740760
// by now, we know that the page is fully loaded
741761
},
742762
};
763+
export default config;
743764
```
744765

745766
#### StorybookTestRunner user agent
746767

747768
The test-runner adds a `StorybookTestRunner` entry to the browser's user agent. You can use it to determine if a story is rendering in the context of the test runner. This might be useful if you want to disable certain features in your stories when running in the test runner, though it's likely an edge case.
748769

749-
```js
770+
```ts
750771
// At the render level, useful for dynamically rendering something based on the test-runner
751772
export const MyStory = {
752773
render: () => {
@@ -776,14 +797,14 @@ Below you will find recipes that use both the hooks and the utility functions to
776797
777798
You can use [Playwright's Page viewport utility](https://playwright.dev/docs/api/class-page#page-set-viewport-size) to programatically change the viewport size of your test. If you use [@storybook/addon-viewports](https://storybook.js.org/addons/@storybook/addon-viewport), you can reuse its parameters and make sure that the tests match in configuration.
778799
779-
```js
780-
// .storybook/test-runner.js
781-
const { getStoryContext } = require('@storybook/test-runner');
782-
const { MINIMAL_VIEWPORTS } = require('@storybook/addon-viewport');
800+
```ts
801+
// .storybook/test-runner.ts
802+
import { TestRunnerConfig, getStoryContext } from '@storybook/test-runner';
803+
import { MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';
783804

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

786-
module.exports = {
807+
const config: TestRunnerConfig = {
787808
async preVisit(page, story) {
788809
const context = await getStoryContext(page, story);
789810
const viewportName = context.parameters?.viewport?.defaultViewport;
@@ -805,19 +826,20 @@ module.exports = {
805826
}
806827
},
807828
};
829+
export default config;
808830
```
809831
810832
### Accessibility testing
811833
812834
You can install `axe-playwright` and use it in tandem with the test-runner to test the accessibility of your components.
813835
If you use [`@storybook/addon-a11y`](https://storybook.js.org/addons/@storybook/addon-a11y), you can reuse its parameters and make sure that the tests match in configuration, both in the accessibility addon panel and the test-runner.
814836
815-
```js
816-
// .storybook/test-runner.js
817-
const { getStoryContext } = require('@storybook/test-runner');
818-
const { injectAxe, checkA11y, configureAxe } = require('axe-playwright');
837+
```ts
838+
// .storybook/test-runner.ts
839+
import { TestRunnerConfig, getStoryContext } from '@storybook/test-runner';
840+
import { injectAxe, checkA11y, configureAxe } from 'axe-playwright';
819841

820-
module.exports = {
842+
const config: TestRunnerConfig = {
821843
async preVisit(page, context) {
822844
// Inject Axe utilities in the page before the story renders
823845
await injectAxe(page);
@@ -847,27 +869,31 @@ module.exports = {
847869
});
848870
},
849871
};
872+
export default config;
850873
```
851874
852875
### DOM snapshot (HTML)
853876
854877
You can use [Playwright's built in APIs](https://playwright.dev/docs/test-snapshots) for DOM snapshot testing:
855878
856-
```js
857-
// .storybook/test-runner.js
858-
module.exports = {
879+
```ts
880+
// .storybook/test-runner.ts
881+
import type { TestRunnerConfig } from '@storybook/test-runner';
882+
883+
const config: TestRunnerConfig = {
859884
async postVisit(page, context) {
860885
// the #storybook-root element wraps the story. In Storybook 6.x, the selector is #root
861886
const elementHandler = await page.$('#storybook-root');
862887
const innerHTML = await elementHandler.innerHTML();
863888
expect(innerHTML).toMatchSnapshot();
864889
},
865890
};
891+
export default config;
866892
```
867893
868894
When running with `--stories-json`, tests get generated in a temporary folder and snapshots get stored alongside. You will need to `--eject` and configure a custom [`snapshotResolver`](https://jestjs.io/docs/configuration#snapshotresolver-string) to store them elsewhere, e.g. in your working directory:
869895
870-
```js
896+
```ts
871897
// ./test-runner-jest.config.js
872898
const { getJestConfig } = require('@storybook/test-runner');
873899

@@ -877,13 +903,13 @@ const testRunnerConfig = getJestConfig();
877903
* @type {import('@jest/types').Config.InitialOptions}
878904
*/
879905
module.exports = {
880-
// The default configuration comes from @storybook/test-runner
906+
// The default Jest configuration comes from @storybook/test-runner
881907
...testRunnerConfig,
882908
snapshotResolver: './snapshot-resolver.js',
883909
};
884910
```
885911
886-
```js
912+
```ts
887913
// ./snapshot-resolver.js
888914
const path = require('path');
889915

@@ -904,14 +930,14 @@ module.exports = {
904930
905931
Here's a slightly different recipe for image snapshot testing:
906932
907-
```js
908-
// .storybook/test-runner.js
909-
const { waitForPageReady } = require('@storybook/test-runner');
910-
const { toMatchImageSnapshot } = require('jest-image-snapshot');
933+
```ts
934+
// .storybook/test-runner.ts
935+
import { TestRunnerConfig, waitForPageReady } from '@storybook/test-runner';
936+
import { toMatchImageSnapshot } from 'jest-image-snapshot';
911937

912938
const customSnapshotsDir = `${process.cwd()}/__snapshots__`;
913939

914-
module.exports = {
940+
const config: TestRunnerConfig = {
915941
setup() {
916942
expect.extend({ toMatchImageSnapshot });
917943
},
@@ -928,10 +954,9 @@ module.exports = {
928954
});
929955
},
930956
};
957+
export default config;
931958
```
932959
933-
There is also an exported `TestRunnerConfig` type available for TypeScript users.
934-
935960
## Troubleshooting
936961
937962
#### The error output in the CLI is too short

playwright/test-runner-jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { getJestConfig } = require('@storybook/test-runner');
22

3-
// The default configuration comes from @storybook/test-runner
3+
// The default Jest configuration comes from @storybook/test-runner
44
const testRunnerConfig = getJestConfig();
55

66
/**

0 commit comments

Comments
 (0)