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 ReportingAPI interface and reporting property to StoryContext #110

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ interface ControlBase {
disable?: boolean;
}

interface Report {
id: string;
version: number;
result: unknown;
status: 'failed' | 'passed' | 'warning';
}

interface ReportingAPI {
reports: Report[];
addReport: (report: Report) => void;
}

type Control =
| ControlType
| false
Expand Down Expand Up @@ -263,6 +275,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 All @@ -278,6 +294,7 @@ export interface StoryContext<TRenderer extends Renderer = Renderer, TArgs = Arg
context: this;
canvas: Canvas;
mount: TRenderer['mount'];
reporting: ReportingAPI;
}

/** @deprecated Use {@link StoryContext} instead. */
Expand Down Expand Up @@ -381,6 +398,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