Skip to content

Commit

Permalink
Merge branch 'next' into yann/use-test-package-in-template-stories
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf authored Oct 30, 2023
2 parents 11d1e03 + 010fc13 commit 2890ac2
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 26 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/code/addons/storyshots-core/ @ndelangen
/code/addons/storyshots-puppeteer/ @ndelangen
/code/addons/storysource/ @ndelangen
/code/addons/themes/ @JReinhold @Integrayshaun
/code/addons/toolbars/ @ndelangen @JReinhold
/code/addons/viewport/ @yannbf @ndelangen

Expand Down
8 changes: 7 additions & 1 deletion code/addons/a11y/src/a11yRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ const run = async (storyId: string) => {
}

const result = await axe.run(htmlElement, options);

// Axe result contains class instances, which telejson deserializes in a
// way that violates:
// Content Security Policy directive: "script-src 'self' 'unsafe-inline'".
const resultJson = JSON.parse(JSON.stringify(result));

// It's possible that we requested a new run on a different story.
// Unfortunately, axe doesn't support a cancel method to abort current run.
// We check if the story we run against is still the current one,
// if not, trigger a new run using the current story
if (activeStoryId === storyId) {
channel.emit(EVENTS.RESULT, result);
channel.emit(EVENTS.RESULT, resultJson);
} else {
active = false;
run(activeStoryId);
Expand Down
10 changes: 3 additions & 7 deletions code/addons/themes/src/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import type { Renderer, ProjectAnnotations } from '@storybook/types';
import { GLOBAL_KEY } from './constants';

const preview: ProjectAnnotations<Renderer> = {
globals: {
// Required to make sure SB picks this up from URL params
[GLOBAL_KEY]: '',
},
export const globals: ProjectAnnotations<Renderer>['globals'] = {
// Required to make sure SB picks this up from URL params
[GLOBAL_KEY]: '',
};

export default preview;
5 changes: 0 additions & 5 deletions code/builders/builder-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Build your stories with [vite](https://vitejs.dev/) for fast startup times and n
- [Getting started with Vite and Storybook (on a new project)](#getting-started-with-vite-and-storybook-on-a-new-project)
- [Migration from webpack / CRA](#migration-from-webpack--cra)
- [Customize Vite config](#customize-vite-config)
- [Svelte Options](#svelte-options)
- [TypeScript](#typescript)
- [React Docgen](#react-docgen)
- [Note about working directory](#note-about-working-directory)
Expand Down Expand Up @@ -113,10 +112,6 @@ The `configType` variable will be either `"DEVELOPMENT"` or `"PRODUCTION"`.

The function should return the updated Vite configuration.

### Svelte Options

When using this builder with Svelte, your `svelte.config.js` file will be used automatically.

### TypeScript

Configure your `.storybook/main.ts` to use TypeScript:
Expand Down
2 changes: 1 addition & 1 deletion docs/api/main-config-vite-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Parent: [main.js|ts configuration](./main-config.md)

Type: `(config: Vite.InlineConfig, options: Options) => Vite.InlineConfig | Promise<Vite.InlineConfig>`

Customize Storybook's Vite setup when using the [vite builder](../builders/vite.md).
Customize Storybook's Vite setup when using the [Vite builder](../builders/vite.md).

<!-- prettier-ignore-start -->

Expand Down
46 changes: 41 additions & 5 deletions docs/builders/vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Update your Storybook configuration (in `.storybook/main.js|ts`) to include the

## Configuration

Out of the box, Storybook's Vite builder includes a set of configuration defaults for the supported frameworks, which are merged alongside your existing configuration file. For an optimal experience when using the Vite builder, we recommend applying any configuration directly inside Vite's configuration file (i.e., [`vite.config.js`](https://vitejs.dev/config/)).
Out of the box, Storybook's Vite builder includes a set of configuration defaults for the supported frameworks, which are merged alongside your existing configuration file. For an optimal experience when using the Vite builder, we recommend applying any configuration directly inside Vite's configuration file (i.e., [`vite.config.js|ts`](https://vitejs.dev/config/)).

When Storybook loads, it automatically merges the configuration into its own. However, not all projects have the same requirements, and you may need to provide a custom configuration created specifically for Storybook. In that case, you can adjust your configuration file (.storybook/main.js|ts) and add the `viteFinal` configuration function as follows:
When Storybook loads, it automatically merges the configuration into its own. However, since different projects may have specific requirements, you may need to provide a custom configuration for Storybook. In such cases, you can modify your configuration file (`.storybook/main.js|ts`) and add the `viteFinal` configuration function as follows:

<!-- prettier-ignore-start -->

Expand All @@ -53,20 +53,42 @@ When Storybook loads, it automatically merges the configuration into its own. Ho

<!-- prettier-ignore-end -->

The asynchronous function `viteFinal` receives a `config` object with the default builder configuration and returns the updated configuration.
The asynchronous function [`viteFinal`](../api/main-config-vite-final.md) receives a `config` object with the default builder configuration and returns the updated configuration.

You can also override the builder's configuration based on the environment. For instance, if you need to provide a custom configuration for development purposes and another for production, you can extend the default configuration as follows:
### Environment-based configuration

If you need to customize the builder's configuration and apply specific options based on your environment, extend the `viteFinal` function as follows:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/main-config-vite-final.js.mdx',
'common/main-config-vite-final-env.js.mdx'
]}
/>

<!-- prettier-ignore-end -->

### Override the default configuration

By default, the Vite builder in Storybook searches for the Vite configuration file in the root directory of your Storybook project. However, you can customize it to look for the configuration file in a different location. For example:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/main-config-builder-custom-config.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

<div class="aside">

💡 If you do not want Storybook to load the Vite configuration file automatically, you can use the `viteConfigPath` option to point to a non-existent file.

</div>

### TypeScript

If you need, you can also configure Storybook's Vite builder using TypeScript. Rename your `.storybook/main.js` to `.storybook/main.ts` and adjust it as follows:
Expand Down Expand Up @@ -104,6 +126,20 @@ Currently, [automatic argType inference](../api/argtypes.md#automatic-argtype-in

<!-- prettier-ignore-end -->

### Interaction tests not working as expected

If you are migrating from a Webpack-based project, such as [CRA](https://create-react-app.dev/), to Vite, and you have enabled Interaction testing with the [`@storybook/addon-interactions`](https://storybook.js.org/addons/@storybook/addon-interactions) addon, you may run into a situation where your tests fail to execute notifying you that the `window` object is not defined. To resolve this issue, you can create a `preview-head.html` file in your Storybook configuration directory and include the following:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-vite-builder-jest-mock.html.mdx',
]}
/>

<!-- prettier-ignore-end -->

#### Learn more about builders

- Vite builder for bundling with Vite
Expand Down
15 changes: 15 additions & 0 deletions docs/snippets/common/main-config-builder-custom-config.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```js
// .storybook/main.js|ts

export default {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
core: {
builder: {
name: '@storybook/builder-vite',
options: {
viteConfigPath: '../customVite.config.js',
},
},
},
};
```
21 changes: 21 additions & 0 deletions docs/snippets/common/main-config-core-builder.ts-4-9.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```ts
// .storybook/main.ts

// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';

const config: StorybookConfig = {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
framework: '@storybook/your-framework',
core: {
builder: {
name: '@storybook/builder-vite',
options: {
viteConfigPath: '../../../vite.config.js',
},
},
},
};

export default config;
```
23 changes: 23 additions & 0 deletions docs/snippets/common/main-config-vite-final-env.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```js
// .storybook/main.js|ts

import { mergeConfig } from 'vite';

export default {
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
core: {
builder: '@storybook/builder-vite',
},
async viteFinal(config, { configType }) {
if (configType === 'DEVELOPMENT') {
// Your development configuration goes here
}
if (configType === 'PRODUCTION') {
// Your production configuration goes here.
}
return mergeConfig(config, {
// Your environment configuration here
});
},
};
```
27 changes: 27 additions & 0 deletions docs/snippets/common/main-config-vite-final.ts-4-9.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```ts
// .storybook/main.ts

// Replace your-framework with the framework you are using (e.g., react-vite, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';

import { mergeConfig } from 'vite';

const config: StorybookConfig = {
// Replace your-framework with the framework you are using (e.g., react-vite, vue3-vite)
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async viteFinal(config, { configType }) {
if (configType === 'DEVELOPMENT') {
// Your development configuration goes here
}
if (configType === 'PRODUCTION') {
// Your production configuration goes here.
}
return mergeConfig(config, {
// Your environment configuration here
});
},
};

export default config;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```html
<!-- .storybook/preview-head.html -->

<script>
window.global = window;
</script>
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```js
// .storybook/main.js
// .storybook/main.js|ts

export default {
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```ts
// .storybook/main.ts

// Replace your-framework with the framework you are using (e.g., react-vite, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';

const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async viteFinal(config, options) {
// Add your configuration here
return config;
},
};

export default config;
```
23 changes: 17 additions & 6 deletions docs/writing-tests/stories-in-unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,33 +172,44 @@ If you intend to test multiple stories in a single test, use the `composeStories

Storybook provides community-led addons for other frameworks like [Vue 2](https://storybook.js.org/addons/@storybook/testing-vue) and [Angular](https://storybook.js.org/addons/@storybook/testing-angular). However, these addons still lack support for the latest stable Storybook release. If you're interested in helping out, we recommend reaching out to the maintainers using the default communication channels (GitHub and [Discord server](https://discord.com/channels/486522875931656193/839297503446695956)).

### The args are not being passed to the test

<IfRenderer renderer='react'>

### The args are not being passed to the test

The components returned by `composeStories` or `composeStory` not only can be rendered as React components but also come with the combined properties from the story, meta, and global configuration. This means that if you want to access args or parameters, for instance, you can do so:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'react/reuse-args-test.js.mdx',
'react/reuse-args-test.ts.mdx',
]}
/>

<!-- prettier-ignore-end -->

</IfRenderer>

<IfRenderer renderer='vue'>

When using the `composeStories` or `composeStory` functions, the components being rendered will have a combination of properties from the story, meta, and global configuration. Therefore, if you need to access the args or parameters, you can do so as follows:
### The args are not being passed to the test

</IfRenderer>
When using the `composeStories` or `composeStory` functions, the components being rendered will have a combination of properties from the story, meta, and global configuration. Therefore, if you need to access the args or parameters, you can do so as follows:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'react/reuse-args-test.js.mdx',
'react/reuse-args-test.ts.mdx',
'vue/reuse-args-test.3.js.mdx',
'vue/reuse-args-test.3.ts.mdx',
]}
/>

<!-- prettier-ignore-end -->

</IfRenderer>

#### Learn about other UI tests

- [Test runner](./test-runner.md) to automate test execution
Expand Down

0 comments on commit 2890ac2

Please sign in to comment.