Skip to content

Commit

Permalink
Add the afterEach hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Nov 7, 2024
1 parent 877a297 commit a093bac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ async function doSomething() {
a = 2;
}

async function validateSomething() {}

async function cleanup() {
a = 1;
}
Expand All @@ -55,6 +57,9 @@ const simple: XMeta = {
await doSomething();
return cleanup;
},
async afterEach() {
await validateSomething();
},
args: { x: '1' },
argTypes: { x: { type: { name: 'string' } } },
};
Expand All @@ -71,6 +76,9 @@ const strict: XMeta<ButtonArgs> = {
await doSomething();
return cleanup;
},
async afterEach() {
await validateSomething();
},
argTypes: { x: { type: { name: 'string' } } },
};

Expand Down
15 changes: 15 additions & 0 deletions src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export type BeforeEach<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: StoryContext<TRenderer, TArgs>
) => Awaitable<CleanupCallback | void>;

export type AfterEach<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: StoryContext<TRenderer, TArgs>
) => Awaitable<void>;

export interface Canvas {}

export interface StoryContext<TRenderer extends Renderer = Renderer, TArgs = Args>
Expand Down Expand Up @@ -379,6 +383,17 @@ export interface BaseAnnotations<TRenderer extends Renderer = Renderer, TArgs =
*/
beforeEach?: BeforeEach<TRenderer, TArgs>[] | BeforeEach<TRenderer, TArgs>;

/**
* Function to be called after each play function for post-test assertions.
* Don't use this function for cleaning up state.
* You can use the return callback of `beforeEach` for that, which is run when switching stories.
* When the function is async, it will be awaited.
*
* `afterEach` can be added to preview, the default export and to a specific story.
* They are run (and awaited) reverse order: preview, default export, story
*/
afterEach?: AfterEach<TRenderer, TArgs>[] | BeforeEach<TRenderer, TArgs>;

/**
* Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used.
*/
Expand Down

0 comments on commit a093bac

Please sign in to comment.