Skip to content

Commit

Permalink
Merge pull request #110 from ComponentDriven/valentin/add-reporting-api
Browse files Browse the repository at this point in the history
Add ReportingAPI interface and reporting property to StoryContext
  • Loading branch information
kasperpeulen authored Nov 29, 2024
2 parents 18dbdbe + c6e6023 commit 97a0ac9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- name: Create Release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
yarn release
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 experimental_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 experimental_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 {
type: 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
*/
experimental_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

0 comments on commit 97a0ac9

Please sign in to comment.