Skip to content

Commit 3b3ebeb

Browse files
authored
Merge pull request #566 from storybookjs/telemetry-next
Add telemetry to test run
2 parents 5f51026 + 8dec3cc commit 3b3ebeb

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Usage: test-storybook [options]
176176
| `--includeTags` | (experimental) Only test stories that match the specified tags, comma separated<br/>`test-storybook --includeTags="test-only"` |
177177
| `--excludeTags` | (experimental) Do not test stories that match the specified tags, comma separated<br/>`test-storybook --excludeTags="broken-story,todo"` |
178178
| `--skipTags` | (experimental) Do not test stories that match the specified tags and mark them as skipped in the CLI output, comma separated<br/>`test-storybook --skipTags="design"` |
179+
| `--disable-telemetry` | Disable sending telemetry data<br/>`test-storybook --disable-telemetry` |
179180

180181
## Ejecting configuration
181182

src/test-storybook.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import path, { join, resolve } from 'path';
99
import tempy from 'tempy';
1010
import { getInterpretedFile } from 'storybook/internal/common';
1111
import { readConfig } from 'storybook/internal/csf-tools';
12+
import { telemetry } from 'storybook/internal/telemetry';
1213
import { glob } from 'glob';
1314

1415
import { JestOptions, getCliOptions } from './util/getCliOptions';
@@ -376,8 +377,9 @@ const main = async () => {
376377
process.env.TEST_MATCH = '**/*.test.js';
377378
}
378379

380+
const { storiesPaths, lazyCompilation, disableTelemetry, enableCrashReports } =
381+
getStorybookMetadata();
379382
if (!shouldRunIndexJson) {
380-
const { storiesPaths, lazyCompilation } = getStorybookMetadata();
381383
process.env.STORYBOOK_STORIES_PATTERN = storiesPaths;
382384

383385
// 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 () => {
395397
process.env.TEST_CHECK_CONSOLE = 'true';
396398
}
397399

400+
if (!disableTelemetry && !runnerOptions.disableTelemetry) {
401+
// NOTE: we start telemetry immediately but do not wait on it. Typically it should complete
402+
// before the tests do. If not we may miss the event, we are OK with that.
403+
telemetry(
404+
// @ts-expect-error -- need to update storybook version
405+
'test-run',
406+
{
407+
runner: 'test-runner',
408+
watch: jestOptions.includes('--watch'),
409+
},
410+
{
411+
configDir: runnerOptions.configDir,
412+
enableCrashReports,
413+
}
414+
);
415+
}
416+
398417
await executeJestPlaywright(jestOptions);
399418
};
400419

src/util/getCliOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type CliOptions = {
1717
includeTags?: string;
1818
excludeTags?: string;
1919
skipTags?: string;
20+
disableTelemetry?: boolean;
2021
} & Record<string, string | boolean>;
2122
jestOptions: JestOptions;
2223
};
@@ -36,6 +37,7 @@ const STORYBOOK_RUNNER_COMMANDS: StorybookRunnerCommand[] = [
3637
'includeTags',
3738
'excludeTags',
3839
'skipTags',
40+
'disableTelemetry',
3941
];
4042

4143
function copyOption<ObjType extends object, KeyType extends keyof ObjType>(

src/util/getParsedCliOptions.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ export const getParsedCliOptions = (): ParsedCliOptions => {
6565
'coverage/storybook'
6666
)
6767
.option('--junit', 'Indicates that test information should be reported in a junit file')
68-
.option(
69-
'--listTests',
70-
'Lists all test files that will be run, and exits'
71-
)
68+
.option('--listTests', 'Lists all test files that will be run, and exits')
7269
.option(
7370
'--eject',
7471
'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 => {
8481
.option('--failOnConsole', 'Makes tests fail on browser console errors')
8582
.option('--includeTags <tags...>', 'Only test stories that match the specified tags')
8683
.option('--excludeTags <tags...>', 'Do not test stories that match the specified tags')
87-
.option('--skipTags <tags...>', 'Skip test stories that match the specified tags');
84+
.option('--skipTags <tags...>', 'Skip test stories that match the specified tags')
85+
.option(
86+
'--disable-telemetry',
87+
'Disable sending telemetry data',
88+
// default value is false, but if the user sets STORYBOOK_DISABLE_TELEMETRY, it can be true
89+
process.env.STORYBOOK_DISABLE_TELEMETRY && process.env.STORYBOOK_DISABLE_TELEMETRY !== 'false'
90+
);
8891

8992
program.exitOverride();
9093

src/util/getStorybookMetadata.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ export const getStorybookMetadata = () => {
2525
// @ts-expect-error -- this is added in storybook/internal/[email protected], which we don't depend on
2626
const lazyCompilation = !!main.core?.builder?.options?.lazyCompilation;
2727

28+
// @ts-expect-error -- need to update to latest sb version
29+
const { disableTelemetry, enableCrashReports } = main.core || {};
30+
2831
return {
2932
configDir,
3033
workingDir,
3134
storiesPaths,
3235
normalizedStoriesEntries,
3336
lazyCompilation,
37+
disableTelemetry,
38+
enableCrashReports,
3439
};
3540
};

0 commit comments

Comments
 (0)