From e560ccdb11e2a527612239565aba35dea513234c Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Thu, 15 May 2025 17:59:10 +1000 Subject: [PATCH 1/6] Revert "Revert "Send telemetry after test completion"" This reverts commit 7f3f6f527e700b5213990e784c26ddc7b47dbb04. --- src/test-storybook.ts | 12 +++++++++++- src/util/getCliOptions.ts | 2 ++ src/util/getParsedCliOptions.ts | 13 ++++++++----- src/util/getStorybookMetadata.ts | 4 ++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/test-storybook.ts b/src/test-storybook.ts index baaa5bd2..ce56cf8e 100644 --- a/src/test-storybook.ts +++ b/src/test-storybook.ts @@ -9,6 +9,7 @@ import path, { join, resolve } from 'path'; import tempy from 'tempy'; import { getInterpretedFile } from 'storybook/internal/common'; import { readConfig } from 'storybook/internal/csf-tools'; +import { telemetry } from 'storybook/internal/telemetry'; import { glob } from 'glob'; import { JestOptions, getCliOptions } from './util/getCliOptions'; @@ -376,8 +377,8 @@ const main = async () => { process.env.TEST_MATCH = '**/*.test.js'; } + const { storiesPaths, lazyCompilation, disableTelemetry } = getStorybookMetadata(); if (!shouldRunIndexJson) { - const { storiesPaths, lazyCompilation } = getStorybookMetadata(); process.env.STORYBOOK_STORIES_PATTERN = storiesPaths; // 1 - We extract tags from preview file statically like it's done by the Storybook indexer. We only do this in non-index-json mode because it's not needed in that mode @@ -396,6 +397,15 @@ const main = async () => { } await executeJestPlaywright(jestOptions); + + if (!disableTelemetry && !runnerOptions.disableTelemetry) { + const t = new Date(); + // @ts-expect-error -- need to update storybookv version + await telemetry('test-run', { + runner: 'test-runner', + watch: jestOptions.includes('--watch'), + }); + } }; main().catch((e) => { diff --git a/src/util/getCliOptions.ts b/src/util/getCliOptions.ts index 99b27197..af4e4a12 100644 --- a/src/util/getCliOptions.ts +++ b/src/util/getCliOptions.ts @@ -17,6 +17,7 @@ export type CliOptions = { includeTags?: string; excludeTags?: string; skipTags?: string; + disableTelemetry?: boolean; } & Record; jestOptions: JestOptions; }; @@ -36,6 +37,7 @@ const STORYBOOK_RUNNER_COMMANDS: StorybookRunnerCommand[] = [ 'includeTags', 'excludeTags', 'skipTags', + 'disableTelemetry', ]; function copyOption( diff --git a/src/util/getParsedCliOptions.ts b/src/util/getParsedCliOptions.ts index 89e70d01..eb15a9d7 100644 --- a/src/util/getParsedCliOptions.ts +++ b/src/util/getParsedCliOptions.ts @@ -65,10 +65,7 @@ export const getParsedCliOptions = (): ParsedCliOptions => { 'coverage/storybook' ) .option('--junit', 'Indicates that test information should be reported in a junit file') - .option( - '--listTests', - 'Lists all test files that will be run, and exits' - ) + .option('--listTests', 'Lists all test files that will be run, and exits') .option( '--eject', 'Creates a local configuration file to override defaults of the test-runner. Use it only if you want to have better control over the runner configurations' @@ -84,7 +81,13 @@ export const getParsedCliOptions = (): ParsedCliOptions => { .option('--failOnConsole', 'Makes tests fail on browser console errors') .option('--includeTags ', 'Only test stories that match the specified tags') .option('--excludeTags ', 'Do not test stories that match the specified tags') - .option('--skipTags ', 'Skip test stories that match the specified tags'); + .option('--skipTags ', 'Skip test stories that match the specified tags') + .option( + '--disable-telemetry', + 'Disable sending telemetry data', + // default value is false, but if the user sets STORYBOOK_DISABLE_TELEMETRY, it can be true + process.env.STORYBOOK_DISABLE_TELEMETRY && process.env.STORYBOOK_DISABLE_TELEMETRY !== 'false' + ); program.exitOverride(); diff --git a/src/util/getStorybookMetadata.ts b/src/util/getStorybookMetadata.ts index 7ede0125..2f4e7528 100644 --- a/src/util/getStorybookMetadata.ts +++ b/src/util/getStorybookMetadata.ts @@ -25,11 +25,15 @@ export const getStorybookMetadata = () => { // @ts-expect-error -- this is added in storybook/internal/common@6.5, which we don't depend on const lazyCompilation = !!main.core?.builder?.options?.lazyCompilation; + // @ts-expect-error -- need to update to latest sb version + const disableTelemetry = !!main.core?.disableTelemetry; + return { configDir, workingDir, storiesPaths, normalizedStoriesEntries, lazyCompilation, + disableTelemetry, }; }; From d4862d00bbcd32f4bd03643a0c8269673da0651f Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Thu, 15 May 2025 22:06:06 +1000 Subject: [PATCH 2/6] Small cleanup --- src/test-storybook.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test-storybook.ts b/src/test-storybook.ts index ce56cf8e..7be82939 100644 --- a/src/test-storybook.ts +++ b/src/test-storybook.ts @@ -399,8 +399,7 @@ const main = async () => { await executeJestPlaywright(jestOptions); if (!disableTelemetry && !runnerOptions.disableTelemetry) { - const t = new Date(); - // @ts-expect-error -- need to update storybookv version + // @ts-expect-error -- need to update storybook version await telemetry('test-run', { runner: 'test-runner', watch: jestOptions.includes('--watch'), From 2963daca660699facf4a09bb40e88d8a48773e78 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Mon, 19 May 2025 12:59:43 +1000 Subject: [PATCH 3/6] Move telemetry command before executing tests; do not wait --- src/test-storybook.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test-storybook.ts b/src/test-storybook.ts index 7be82939..01fe8162 100644 --- a/src/test-storybook.ts +++ b/src/test-storybook.ts @@ -396,15 +396,17 @@ const main = async () => { process.env.TEST_CHECK_CONSOLE = 'true'; } - await executeJestPlaywright(jestOptions); - if (!disableTelemetry && !runnerOptions.disableTelemetry) { + // NOTE: we start telemetry immediately but do not wait on it. Typically it should complete + // before the tests do. If not we may miss the event, we are OK with that. // @ts-expect-error -- need to update storybook version - await telemetry('test-run', { + telemetry('test-run', { runner: 'test-runner', watch: jestOptions.includes('--watch'), }); } + + await executeJestPlaywright(jestOptions); }; main().catch((e) => { From 8f3c73555158aa423b85c1298f4e2c31b7cc3be7 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Mon, 19 May 2025 13:01:01 +1000 Subject: [PATCH 4/6] Pass `configDir` and `enableCrashReports` --- src/test-storybook.ts | 20 ++++++++++++++------ src/util/getStorybookMetadata.ts | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/test-storybook.ts b/src/test-storybook.ts index 01fe8162..a8115ffb 100644 --- a/src/test-storybook.ts +++ b/src/test-storybook.ts @@ -377,7 +377,8 @@ const main = async () => { process.env.TEST_MATCH = '**/*.test.js'; } - const { storiesPaths, lazyCompilation, disableTelemetry } = getStorybookMetadata(); + const { storiesPaths, lazyCompilation, disableTelemetry, enableCrashReports } = + getStorybookMetadata(); if (!shouldRunIndexJson) { process.env.STORYBOOK_STORIES_PATTERN = storiesPaths; @@ -399,11 +400,18 @@ const main = async () => { if (!disableTelemetry && !runnerOptions.disableTelemetry) { // NOTE: we start telemetry immediately but do not wait on it. Typically it should complete // before the tests do. If not we may miss the event, we are OK with that. - // @ts-expect-error -- need to update storybook version - telemetry('test-run', { - runner: 'test-runner', - watch: jestOptions.includes('--watch'), - }); + telemetry( + // @ts-expect-error -- need to update storybook version + 'test-run', + { + runner: 'test-runner', + watch: jestOptions.includes('--watch'), + }, + { + configDir: runnerOptions.configDir, + enableCrashReports, + } + ); } await executeJestPlaywright(jestOptions); diff --git a/src/util/getStorybookMetadata.ts b/src/util/getStorybookMetadata.ts index 2f4e7528..fbd15213 100644 --- a/src/util/getStorybookMetadata.ts +++ b/src/util/getStorybookMetadata.ts @@ -26,7 +26,7 @@ export const getStorybookMetadata = () => { const lazyCompilation = !!main.core?.builder?.options?.lazyCompilation; // @ts-expect-error -- need to update to latest sb version - const disableTelemetry = !!main.core?.disableTelemetry; + const { disableTelemetry, enableCrashReports } = !!main.core || {}; return { configDir, @@ -35,5 +35,6 @@ export const getStorybookMetadata = () => { normalizedStoriesEntries, lazyCompilation, disableTelemetry, + enableCrashReports, }; }; From 6ce285d513ec4cd3cb0abef4fb27f35b30b695c5 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Sat, 7 Jun 2025 13:26:27 +0200 Subject: [PATCH 5/6] fix bug --- src/util/getStorybookMetadata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/getStorybookMetadata.ts b/src/util/getStorybookMetadata.ts index fbd15213..bf496bd8 100644 --- a/src/util/getStorybookMetadata.ts +++ b/src/util/getStorybookMetadata.ts @@ -26,7 +26,7 @@ export const getStorybookMetadata = () => { const lazyCompilation = !!main.core?.builder?.options?.lazyCompilation; // @ts-expect-error -- need to update to latest sb version - const { disableTelemetry, enableCrashReports } = !!main.core || {}; + const { disableTelemetry, enableCrashReports } = main.core || {}; return { configDir, From fb48a465a5e92b767f7a784bb6efd4f291bdf1bf Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Sat, 7 Jun 2025 13:29:55 +0200 Subject: [PATCH 6/6] document flag --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a7ca95fd..eda91f85 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,7 @@ Usage: test-storybook [options] | `--includeTags` | (experimental) Only test stories that match the specified tags, comma separated
`test-storybook --includeTags="test-only"` | | `--excludeTags` | (experimental) Do not test stories that match the specified tags, comma separated
`test-storybook --excludeTags="broken-story,todo"` | | `--skipTags` | (experimental) Do not test stories that match the specified tags and mark them as skipped in the CLI output, comma separated
`test-storybook --skipTags="design"` | +| `--disable-telemetry` | Disable sending telemetry data
`test-storybook --disable-telemetry` | ## Ejecting configuration