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

Add the afterEach hook #109

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
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
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 @@ -263,6 +263,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 @@ -381,6 +385,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>[] | AfterEach<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
Loading