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 diff --git a/src/test-storybook.ts b/src/test-storybook.ts index baaa5bd2..a8115ffb 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,9 @@ const main = async () => { process.env.TEST_MATCH = '**/*.test.js'; } + const { storiesPaths, lazyCompilation, disableTelemetry, enableCrashReports } = + 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 @@ -395,6 +397,23 @@ const main = async () => { process.env.TEST_CHECK_CONSOLE = 'true'; } + 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. + 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/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..bf496bd8 100644 --- a/src/util/getStorybookMetadata.ts +++ b/src/util/getStorybookMetadata.ts @@ -25,11 +25,16 @@ 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, enableCrashReports } = main.core || {}; + return { configDir, workingDir, storiesPaths, normalizedStoriesEntries, lazyCompilation, + disableTelemetry, + enableCrashReports, }; };