You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And register it in your `.storybook/main.js` file:
411
414
412
-
```js
413
-
// .storybook/main.js
414
-
module.exports = {
415
+
```ts
416
+
// .storybook/main.ts
417
+
const config = {
415
418
// ...rest of your code here
416
419
addons: ['@storybook/addon-coverage'],
417
420
};
421
+
export default config;
418
422
```
419
423
420
424
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
555
559
556
560
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.
557
561
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 = {
561
567
async setup() {
562
568
// execute whatever you like, in Node, once before all tests run
563
569
},
564
570
};
571
+
export default config;
565
572
```
566
573
567
574
#### preRender (deprecated)
@@ -574,13 +581,16 @@ module.exports = {
574
581
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`.
575
582
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.
576
583
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 = {
580
589
async preVisit(page, context) {
581
590
// execute whatever you like, before the story renders
582
591
},
583
592
};
593
+
export default config;
584
594
```
585
595
586
596
#### postRender (deprecated)
@@ -593,13 +603,16 @@ module.exports = {
593
603
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`.
594
604
Executes within a test after a story is rendered. Useful for asserting things after the story is rendered, such as DOM and image snapshotting.
595
605
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 = {
599
611
async postVisit(page, context) {
600
612
// execute whatever you like, after the story renders
601
613
},
602
614
};
615
+
export default config;
603
616
```
604
617
605
618
> **Note**
@@ -609,7 +622,7 @@ module.exports = {
609
622
610
623
To visualize the test lifecycle with these hooks, consider a simplified version of the test code automatically generated for each story in your Storybook:
// render the story and run its play function (if applicable)
639
+
// render the story and watch its play function (if applicable)
627
640
await page.execute('render', context);
628
641
629
-
// post-render hook
642
+
// post-visit hook
630
643
if (postVisit) await postVisit(page, context);
631
644
});
632
645
```
@@ -654,31 +667,37 @@ For reference, please use the [default `prepare`](https://github.com/storybookjs
654
667
655
668
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.
656
669
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';
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
728
748
729
749
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.
// use the test-runner utility to wait for fonts to load, etc.
738
758
awaitwaitForPageReady(page);
739
759
740
760
// by now, we know that the page is fully loaded
741
761
},
742
762
};
763
+
exportdefaultconfig;
743
764
```
744
765
745
766
#### StorybookTestRunner user agent
746
767
747
768
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.
748
769
749
-
```js
770
+
```ts
750
771
// At the render level, useful for dynamically rendering something based on the test-runner
751
772
exportconst MyStory = {
752
773
render: () => {
@@ -776,14 +797,14 @@ Below you will find recipes that use both the hooks and the utility functions to
776
797
777
798
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.
You can install `axe-playwright` and use it in tandem with the test-runner to test the accessibility of your components.
813
835
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.
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:
0 commit comments