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

Core: Make sure beforeAll is properly typed #28864

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions code/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Fragment, useEffect } from 'react';
import * as React from 'react';
import { Fragment, useEffect } from 'react';

import type { Channel } from 'storybook/internal/channels';
import { DocsContext as DocsContextProps, useArgs } from 'storybook/internal/preview-api';
Expand Down Expand Up @@ -160,7 +161,7 @@ export const loaders = [
}
return { docsContext };
},
];
] as Loader[];

export const decorators = [
// This decorator adds the DocsContext created in the loader above
Expand Down
2 changes: 1 addition & 1 deletion code/.storybook/storybook.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ const annotations = setProjectAnnotations([
},
]);

beforeAll(annotations.beforeAll!);
beforeAll(annotations.beforeAll);
4 changes: 2 additions & 2 deletions code/core/src/preview-api/modules/store/csf/composeConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModuleExports, ProjectAnnotations } from '@storybook/core/types';
import type { ModuleExports, NormalizedProjectAnnotations } from '@storybook/core/types';
import type { Renderer } from '@storybook/core/types';
import { global } from '@storybook/global';

Expand Down Expand Up @@ -41,7 +41,7 @@ export function getSingletonField<TFieldType = any>(

export function composeConfigs<TRenderer extends Renderer>(
moduleExportList: ModuleExports[]
): ProjectAnnotations<TRenderer> {
): NormalizedProjectAnnotations<TRenderer> {
const allArgTypeEnhancers = getArrayField(moduleExportList, 'argTypesEnhancers');
const stepRunners = getField(moduleExportList, 'runStep');
const beforeAllHooks = getArrayField(moduleExportList, 'beforeAll');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ComposedStoryFn,
LegacyStoryAnnotationsOrFn,
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
Parameters,
PreparedStory,
ProjectAnnotations,
Expand All @@ -35,13 +36,11 @@ import { prepareContext, prepareStory } from './prepareStory';
// TODO we should get to the bottom of the singleton issues caused by dual ESM/CJS modules
declare global {
// eslint-disable-next-line no-var
var globalProjectAnnotations: ProjectAnnotations<any>;
var globalProjectAnnotations: NormalizedProjectAnnotations<any>;
// eslint-disable-next-line no-var
var defaultProjectAnnotations: ProjectAnnotations<any>;
}

globalThis.globalProjectAnnotations ??= {};

export function setDefaultProjectAnnotations<TRenderer extends Renderer = Renderer>(
_defaultProjectAnnotations: ProjectAnnotations<TRenderer>
) {
Expand All @@ -67,7 +66,7 @@ export function setProjectAnnotations<TRenderer extends Renderer = Renderer>(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<TRenderer>
| NamedOrDefaultProjectAnnotations<TRenderer>[]
): ProjectAnnotations<TRenderer> {
): NormalizedProjectAnnotations<TRenderer> {
const annotations = Array.isArray(projectAnnotations) ? projectAnnotations : [projectAnnotations];
globalThis.globalProjectAnnotations = composeConfigs(annotations.map(extractAnnotation));

Expand Down Expand Up @@ -112,7 +111,7 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
defaultConfig && Object.keys(defaultConfig).length > 0
? defaultConfig
: globalThis.defaultProjectAnnotations ?? {},
globalThis.globalProjectAnnotations,
globalThis.globalProjectAnnotations ?? {},
projectAnnotations ?? {},
])
);
Expand Down
11 changes: 6 additions & 5 deletions code/frameworks/nextjs/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
Args,
ComposedStoryFn,
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
ProjectAnnotations,
Store_CSFExports,
StoriesWithPartialProps,
Expand Down Expand Up @@ -38,12 +39,12 @@ import * as nextJsAnnotations from './preview';
* @param projectAnnotations - e.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<ReactRenderer>
| NamedOrDefaultProjectAnnotations<ReactRenderer>[]
): ProjectAnnotations<ReactRenderer> {
projectAnnotations: NamedOrDefaultProjectAnnotations | NamedOrDefaultProjectAnnotations[]
): NormalizedProjectAnnotations<ReactRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations<ReactRenderer>(projectAnnotations);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<ReactRenderer>;
}

// This will not be necessary once we have auto preset loading
Expand Down
11 changes: 6 additions & 5 deletions code/renderers/react/src/portable-stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
Args,
ComposedStoryFn,
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
ProjectAnnotations,
Store_CSFExports,
StoriesWithPartialProps,
Expand All @@ -36,12 +37,12 @@ import type { ReactRenderer } from './types';
* @param projectAnnotations - e.g. (import * as projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<ReactRenderer>
| NamedOrDefaultProjectAnnotations<ReactRenderer>[]
): ProjectAnnotations<ReactRenderer> {
projectAnnotations: NamedOrDefaultProjectAnnotations | NamedOrDefaultProjectAnnotations[]
): NormalizedProjectAnnotations<ReactRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations<ReactRenderer>(projectAnnotations);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<ReactRenderer>;
}

// This will not be necessary once we have auto preset loading
Expand Down
11 changes: 6 additions & 5 deletions code/renderers/svelte/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Args,
ComposedStoryFn,
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
ProjectAnnotations,
Store_CSFExports,
StoriesWithPartialProps,
Expand Down Expand Up @@ -54,12 +55,12 @@ type MapToComposed<TModule> = {
* @param projectAnnotations - e.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<SvelteRenderer>
| NamedOrDefaultProjectAnnotations<SvelteRenderer>[]
): ProjectAnnotations<SvelteRenderer> {
projectAnnotations: NamedOrDefaultProjectAnnotations | NamedOrDefaultProjectAnnotations[]
): NormalizedProjectAnnotations<SvelteRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations<SvelteRenderer>(projectAnnotations);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<SvelteRenderer>;
}

// This will not be necessary once we have auto preset loading
Expand Down
11 changes: 6 additions & 5 deletions code/renderers/vue3/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Args,
ComposedStoryFn,
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
ProjectAnnotations,
Store_CSFExports,
StoriesWithPartialProps,
Expand Down Expand Up @@ -44,12 +45,12 @@ type MapToJSXAble<T> = {
* @param projectAnnotations - e.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<VueRenderer>
| NamedOrDefaultProjectAnnotations<VueRenderer>[]
): ProjectAnnotations<VueRenderer> {
projectAnnotations: NamedOrDefaultProjectAnnotations | NamedOrDefaultProjectAnnotations[]
): NormalizedProjectAnnotations<VueRenderer> {
setDefaultProjectAnnotations(vueProjectAnnotations);
return originalSetProjectAnnotations<VueRenderer>(projectAnnotations);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<VueRenderer>;
}

// This will not be necessary once we have auto preset loading
Expand Down