From bb5bb77883cc71f0b4a0c503d3396cb20c0b8592 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Wed, 13 Nov 2024 12:37:07 +0100 Subject: [PATCH 1/7] 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/7] 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/7] 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. From 0513a2b469b75825938233fdffea7ab1d08317ef Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Mon, 25 Nov 2024 14:02:10 +0100 Subject: [PATCH 4/7] Update type for reporting api --- src/story.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/story.ts b/src/story.ts index 047de7b..5894edd 100644 --- a/src/story.ts +++ b/src/story.ts @@ -65,7 +65,7 @@ interface ControlBase { interface Report { id: string; - version: number; + version?: number; result: unknown; status: 'failed' | 'passed' | 'warning'; } From 100825f894cf450265bca15653ce85f244314ee3 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Mon, 25 Nov 2024 14:16:15 +0100 Subject: [PATCH 5/7] Fix GitHub token issue --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d803cb8..1eccc71 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 From be293af84482a76639e2d71688ba554c090109bd Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Fri, 29 Nov 2024 08:56:44 +0100 Subject: [PATCH 6/7] Update Report interface to use 'type' instead of 'id' and rename afterEach to experimental_afterEach --- src/story.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/story.ts b/src/story.ts index 5894edd..2596758 100644 --- a/src/story.ts +++ b/src/story.ts @@ -64,7 +64,7 @@ interface ControlBase { } interface Report { - id: string; + type: string; version?: number; result: unknown; status: 'failed' | 'passed' | 'warning'; @@ -407,7 +407,7 @@ export interface BaseAnnotations[] | AfterEach; + experimental_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. From c6e6023e1a95a3cb36cef20a42b0c2b9a2d53f4a Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Fri, 29 Nov 2024 08:58:57 +0100 Subject: [PATCH 7/7] Rename afterEach to experimental_afterEach in story test cases --- src/story.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/story.test.ts b/src/story.test.ts index 8948e3c..f7c2de4 100644 --- a/src/story.test.ts +++ b/src/story.test.ts @@ -57,7 +57,7 @@ const simple: XMeta = { await doSomething(); return cleanup; }, - async afterEach() { + async experimental_afterEach() { await validateSomething(); }, args: { x: '1' }, @@ -76,7 +76,7 @@ const strict: XMeta = { await doSomething(); return cleanup; }, - async afterEach() { + async experimental_afterEach() { await validateSomething(); }, argTypes: { x: { type: { name: 'string' } } },