Skip to content

Commit 4612467

Browse files
Merge pull request #31438 from storybookjs/valentin/stabilize-experimental-after-each
Hooks: Stabilize experimental afterEach hook
2 parents 9ae3155 + 47938f8 commit 4612467

File tree

13 files changed

+59
-44
lines changed

13 files changed

+59
-44
lines changed

MIGRATION.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Story Store API Changes](#story-store-api-changes)
2525
- [Global State Management](#global-state-management)
2626
- [Experimental Status API has turned into a Status Store](#experimental-status-api-has-turned-into-a-status-store)
27+
- [`experimental_afterEach` has been stabilized](#experimental_aftereach-has-been-stabilized)
2728
- [Testing Module Changes](#testing-module-changes)
2829
- [Consolidate `@storybook/blocks` into addon docs](#consolidate-storybookblocks-into-addon-docs)
2930
- [Configuration and Type Changes](#configuration-and-type-changes)
@@ -862,6 +863,21 @@ addons.register(MY_ADDON_ID, (api) => {
862863
+ }]);
863864
```
864865

866+
#### `experimental_afterEach` has been stabilized
867+
868+
The experimental_afterEach hook has been promoted to a stable API and renamed to afterEach.
869+
870+
To migrate, simply replace all instances of experimental_afterEach with afterEach in your stories, preview files, and configuration.
871+
872+
```diff
873+
export const MyStory = {
874+
- experimental_afterEach: async ({ canvasElement }) => {
875+
+ afterEach: async ({ canvasElement }) => {
876+
// cleanup logic
877+
},
878+
};
879+
```
880+
865881
#### Testing Module Changes
866882

867883
The `TESTING_MODULE_RUN_ALL_REQUEST` event has been removed:

code/addons/a11y/src/preview.test.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import type { StoryContext } from 'storybook/internal/csf';
55

66
import { run } from './a11yRunner';
7-
import { withLinkPaths } from './a11yRunnerUtils';
8-
import { experimental_afterEach } from './preview';
7+
import { afterEach } from './preview';
98
import { getIsVitestRunning, getIsVitestStandaloneRun } from './utils';
109

1110
const mocks = vi.hoisted(() => {
@@ -107,7 +106,7 @@ describe('afterEach', () => {
107106

108107
mockedRun.mockResolvedValue(result as any);
109108

110-
await expect(() => experimental_afterEach(context)).rejects.toThrow();
109+
await expect(() => afterEach(context)).rejects.toThrow();
111110

112111
expect(mockedRun).toHaveBeenCalledWith(context.parameters.a11y, context.id);
113112

@@ -128,7 +127,7 @@ describe('afterEach', () => {
128127
mockedRun.mockResolvedValue(result as any);
129128
mocks.getIsVitestStandaloneRun.mockReturnValue(false);
130129

131-
await experimental_afterEach(context);
130+
await afterEach(context);
132131

133132
expect(mockedRun).toHaveBeenCalledWith(context.parameters.a11y, context.id);
134133

@@ -155,7 +154,7 @@ describe('afterEach', () => {
155154
mockedRun.mockResolvedValue(result as any);
156155
mocks.getIsVitestStandaloneRun.mockReturnValue(false);
157156

158-
await experimental_afterEach(context);
157+
await afterEach(context);
159158

160159
expect(mockedRun).toHaveBeenCalledWith(context.parameters.a11y, context.id);
161160

@@ -174,7 +173,7 @@ describe('afterEach', () => {
174173
};
175174
mockedRun.mockResolvedValue(result as any);
176175

177-
await experimental_afterEach(context);
176+
await afterEach(context);
178177

179178
expect(mockedRun).toHaveBeenCalledWith(context.parameters.a11y, context.id);
180179
expect(context.reporting.addReport).toHaveBeenCalledWith({
@@ -194,7 +193,7 @@ describe('afterEach', () => {
194193
},
195194
});
196195

197-
await experimental_afterEach(context);
196+
await afterEach(context);
198197

199198
expect(mockedRun).not.toHaveBeenCalled();
200199
expect(context.reporting.addReport).not.toHaveBeenCalled();
@@ -209,7 +208,7 @@ describe('afterEach', () => {
209208
},
210209
});
211210

212-
await experimental_afterEach(context);
211+
await afterEach(context);
213212

214213
expect(mockedRun).not.toHaveBeenCalled();
215214
expect(context.reporting.addReport).not.toHaveBeenCalled();
@@ -224,7 +223,7 @@ describe('afterEach', () => {
224223
},
225224
});
226225

227-
await experimental_afterEach(context);
226+
await afterEach(context);
228227

229228
expect(mockedRun).not.toHaveBeenCalled();
230229
expect(context.reporting.addReport).not.toHaveBeenCalled();
@@ -235,7 +234,7 @@ describe('afterEach', () => {
235234
const error = new Error('Test error');
236235
mockedRun.mockRejectedValue(error);
237236

238-
await expect(() => experimental_afterEach(context)).rejects.toThrow();
237+
await expect(() => afterEach(context)).rejects.toThrow();
239238

240239
expect(mockedRun).toHaveBeenCalledWith(context.parameters.a11y, context.id);
241240
expect(context.reporting.addReport).toHaveBeenCalledWith({
@@ -253,7 +252,7 @@ describe('afterEach', () => {
253252
viewMode: 'docs',
254253
});
255254

256-
await experimental_afterEach(context);
255+
await afterEach(context);
257256

258257
expect(mockedRun).not.toHaveBeenCalled();
259258
expect(context.reporting.addReport).not.toHaveBeenCalled();

code/addons/a11y/src/preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getIsVitestStandaloneRun } from './utils';
88

99
let vitestMatchersExtended = false;
1010

11-
export const experimental_afterEach: AfterEach<any> = async ({
11+
export const afterEach: AfterEach<any> = async ({
1212
id: storyId,
1313
reporting,
1414
parameters,

code/core/src/components/components/Checkbox/Checkbox.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const All: Story = {
3131
<small>(native)</small>
3232
</div>
3333
),
34-
experimental_afterEach: async ({ canvasElement }) => {
34+
afterEach: async ({ canvasElement }) => {
3535
canvasElement.querySelectorAll<HTMLInputElement>('[data-indeterminate]').forEach((checkbox) => {
3636
checkbox.indeterminate = true;
3737
});
@@ -52,7 +52,7 @@ export const Checked: Story = {
5252
};
5353

5454
export const Indeterminate: Story = {
55-
experimental_afterEach: async ({ canvasElement }) => {
55+
afterEach: async ({ canvasElement }) => {
5656
canvasElement.getElementsByTagName('input')[0].indeterminate = true;
5757
},
5858
};
@@ -74,7 +74,7 @@ export const DisabledIndeterminate: Story = {
7474
args: {
7575
disabled: true,
7676
},
77-
experimental_afterEach: async ({ canvasElement }) => {
77+
afterEach: async ({ canvasElement }) => {
7878
canvasElement.getElementsByTagName('input')[0].indeterminate = true;
7979
},
8080
};

code/core/src/csf/story.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const simple: XMeta = {
5858
await doSomething();
5959
return cleanup;
6060
},
61-
async experimental_afterEach() {
61+
async afterEach() {
6262
await validateSomething();
6363
},
6464
args: { x: '1' },
@@ -77,7 +77,7 @@ const strict: XMeta<ButtonArgs> = {
7777
await doSomething();
7878
return cleanup;
7979
},
80-
async experimental_afterEach() {
80+
async afterEach() {
8181
await validateSomething();
8282
},
8383
argTypes: { x: { type: { name: 'string' } } },

code/core/src/csf/story.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export interface BaseAnnotations<TRenderer extends Renderer = Renderer, TArgs =
370370
* `afterEach` can be added to preview, the default export and to a specific story. They are run
371371
* (and awaited) reverse order: preview, default export, story
372372
*/
373-
experimental_afterEach?: AfterEach<TRenderer, TArgs>[] | AfterEach<TRenderer, TArgs>;
373+
afterEach?: AfterEach<TRenderer, TArgs>[] | AfterEach<TRenderer, TArgs>;
374374

375375
/**
376376
* Define a custom render function for the story(ies). If not passed, a default render function by

code/core/src/preview-api/modules/store/csf/composeConfigs.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('composeConfigs', () => {
2424
loaders: [],
2525
beforeAll: expect.any(Function),
2626
beforeEach: [],
27-
experimental_afterEach: [],
27+
afterEach: [],
2828
runStep: expect.any(Function),
2929
tags: [],
3030
});
@@ -52,7 +52,7 @@ describe('composeConfigs', () => {
5252
loaders: [],
5353
beforeAll: expect.any(Function),
5454
beforeEach: [],
55-
experimental_afterEach: [],
55+
afterEach: [],
5656
runStep: expect.any(Function),
5757
tags: [],
5858
});
@@ -84,7 +84,7 @@ describe('composeConfigs', () => {
8484
loaders: [],
8585
beforeAll: expect.any(Function),
8686
beforeEach: [],
87-
experimental_afterEach: [],
87+
afterEach: [],
8888
runStep: expect.any(Function),
8989
tags: [],
9090
});
@@ -122,7 +122,7 @@ describe('composeConfigs', () => {
122122
loaders: [],
123123
beforeAll: expect.any(Function),
124124
beforeEach: [],
125-
experimental_afterEach: [],
125+
afterEach: [],
126126
runStep: expect.any(Function),
127127
tags: [],
128128
});
@@ -163,7 +163,7 @@ describe('composeConfigs', () => {
163163
loaders: [],
164164
beforeAll: expect.any(Function),
165165
beforeEach: [],
166-
experimental_afterEach: [],
166+
afterEach: [],
167167
runStep: expect.any(Function),
168168
tags: [],
169169
});
@@ -195,7 +195,7 @@ describe('composeConfigs', () => {
195195
loaders: ['1', '2', '3', '4'],
196196
beforeAll: expect.any(Function),
197197
beforeEach: [],
198-
experimental_afterEach: [],
198+
afterEach: [],
199199
runStep: expect.any(Function),
200200
tags: [],
201201
});
@@ -227,7 +227,7 @@ describe('composeConfigs', () => {
227227
loaders: ['1', '2', '3'],
228228
beforeAll: expect.any(Function),
229229
beforeEach: [],
230-
experimental_afterEach: [],
230+
afterEach: [],
231231
runStep: expect.any(Function),
232232
tags: [],
233233
});
@@ -255,7 +255,7 @@ describe('composeConfigs', () => {
255255
loaders: [],
256256
beforeAll: expect.any(Function),
257257
beforeEach: [],
258-
experimental_afterEach: [],
258+
afterEach: [],
259259
runStep: expect.any(Function),
260260
tags: [],
261261
});
@@ -284,7 +284,7 @@ describe('composeConfigs', () => {
284284
loaders: [],
285285
beforeAll: expect.any(Function),
286286
beforeEach: [],
287-
experimental_afterEach: [],
287+
afterEach: [],
288288
runStep: expect.any(Function),
289289
tags: [],
290290
});
@@ -316,7 +316,7 @@ describe('composeConfigs', () => {
316316
loaders: [],
317317
beforeAll: expect.any(Function),
318318
beforeEach: [],
319-
experimental_afterEach: [],
319+
afterEach: [],
320320
render: 'render-2',
321321
renderToCanvas: 'renderToCanvas-2',
322322
applyDecorators: 'applyDecorators-2',

code/core/src/preview-api/modules/store/csf/composeConfigs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function composeConfigs<TRenderer extends Renderer>(
6464
loaders: getArrayField(moduleExportList, 'loaders'),
6565
beforeAll: composeBeforeAllHooks(beforeAllHooks),
6666
beforeEach: getArrayField(moduleExportList, 'beforeEach'),
67-
experimental_afterEach: getArrayField(moduleExportList, 'experimental_afterEach'),
67+
afterEach: getArrayField(moduleExportList, 'afterEach'),
6868
render: getSingletonField(moduleExportList, 'render'),
6969
renderToCanvas: getSingletonField(moduleExportList, 'renderToCanvas'),
7070
applyDecorators: getSingletonField(moduleExportList, 'applyDecorators'),

code/core/src/preview-api/modules/store/csf/normalizeProjectAnnotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function normalizeProjectAnnotations<TRenderer extends Renderer>({
2121
decorators,
2222
loaders,
2323
beforeEach,
24-
experimental_afterEach,
24+
afterEach,
2525
initialGlobals,
2626
...annotations
2727
}: ProjectAnnotations<TRenderer>): NormalizedProjectAnnotations<TRenderer> {
@@ -31,7 +31,7 @@ export function normalizeProjectAnnotations<TRenderer extends Renderer>({
3131
decorators: normalizeArrays(decorators),
3232
loaders: normalizeArrays(loaders),
3333
beforeEach: normalizeArrays(beforeEach),
34-
experimental_afterEach: normalizeArrays(experimental_afterEach),
34+
afterEach: normalizeArrays(afterEach),
3535
argTypesEnhancers: [
3636
...(argTypesEnhancers || []),
3737
inferArgTypes,

code/core/src/preview-api/modules/store/csf/normalizeStory.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ describe('normalizeStory', () => {
5252
const meta = { id: 'title', title: 'title' };
5353
expect(normalizeStory('storyExport', storyFn, meta)).toMatchInlineSnapshot(`
5454
{
55+
"afterEach": [],
5556
"argTypes": {},
5657
"args": {},
5758
"beforeEach": [],
5859
"decorators": [],
59-
"experimental_afterEach": [],
6060
"globals": {},
6161
"id": "title--story-export",
6262
"loaders": [],
@@ -124,11 +124,11 @@ describe('normalizeStory', () => {
124124
const normalized = normalizeStory('storyExport', storyObj, meta);
125125
expect(normalized).toMatchInlineSnapshot(`
126126
{
127+
"afterEach": [],
127128
"argTypes": {},
128129
"args": {},
129130
"beforeEach": [],
130131
"decorators": [],
131-
"experimental_afterEach": [],
132132
"globals": {},
133133
"id": "title--story-export",
134134
"loaders": [],
@@ -154,6 +154,7 @@ describe('normalizeStory', () => {
154154
const { moduleExport, ...normalized } = normalizeStory('storyExport', storyObj, meta);
155155
expect(normalized).toMatchInlineSnapshot(`
156156
{
157+
"afterEach": [],
157158
"argTypes": {
158159
"storyArgType": {
159160
"name": "storyArgType",
@@ -169,7 +170,6 @@ describe('normalizeStory', () => {
169170
"decorators": [
170171
[Function],
171172
],
172-
"experimental_afterEach": [],
173173
"globals": {},
174174
"id": "title--story-export",
175175
"loaders": [
@@ -205,6 +205,7 @@ describe('normalizeStory', () => {
205205
const { moduleExport, ...normalized } = normalizeStory('storyExport', storyObj, meta);
206206
expect(normalized).toMatchInlineSnapshot(`
207207
{
208+
"afterEach": [],
208209
"argTypes": {
209210
"storyArgType": {
210211
"name": "storyArgType",
@@ -228,7 +229,6 @@ describe('normalizeStory', () => {
228229
[Function],
229230
[Function],
230231
],
231-
"experimental_afterEach": [],
232232
"globals": {},
233233
"id": "title--story-export",
234234
"loaders": [

0 commit comments

Comments
 (0)