From bb5bb77883cc71f0b4a0c503d3396cb20c0b8592 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Wed, 13 Nov 2024 12:37:07 +0100 Subject: [PATCH 1/3] Add ReportingAPI interface and reporting property to StoryContext --- src/story.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/story.ts b/src/story.ts index 4bcf3c7..e0a26c9 100644 --- a/src/story.ts +++ b/src/story.ts @@ -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 @@ -278,6 +290,7 @@ export interface StoryContext Date: Thu, 7 Nov 2024 16:55:27 +0100 Subject: [PATCH 2/3] Add the afterEach hook --- src/story.test.ts | 8 ++++++++ src/story.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/story.test.ts b/src/story.test.ts index f49c18f..8948e3c 100644 --- a/src/story.test.ts +++ b/src/story.test.ts @@ -39,6 +39,8 @@ async function doSomething() { a = 2; } +async function validateSomething() {} + async function cleanup() { a = 1; } @@ -55,6 +57,9 @@ const simple: XMeta = { await doSomething(); return cleanup; }, + async afterEach() { + await validateSomething(); + }, args: { x: '1' }, argTypes: { x: { type: { name: 'string' } } }, }; @@ -71,6 +76,9 @@ const strict: XMeta = { await doSomething(); return cleanup; }, + async afterEach() { + await validateSomething(); + }, argTypes: { x: { type: { name: 'string' } } }, }; diff --git a/src/story.ts b/src/story.ts index e0a26c9..4ce3585 100644 --- a/src/story.ts +++ b/src/story.ts @@ -275,6 +275,10 @@ export type BeforeEach = ( context: StoryContext ) => Awaitable; +export type AfterEach = ( + context: StoryContext +) => Awaitable; + export interface Canvas {} export interface StoryContext @@ -394,6 +398,17 @@ export interface BaseAnnotations[] | BeforeEach; + /** + * 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[] | BeforeEach; + /** * Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used. */ From 7611f0c76ac05d880fe3c8954ab6bb461a669eba Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Tue, 12 Nov 2024 15:30:26 +0100 Subject: [PATCH 3/3] Fix typo --- src/story.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/story.ts b/src/story.ts index 4ce3585..047de7b 100644 --- a/src/story.ts +++ b/src/story.ts @@ -407,7 +407,7 @@ export interface BaseAnnotations[] | BeforeEach; + afterEach?: AfterEach[] | AfterEach; /** * Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used.