Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Usage: test-storybook [options]
| `--includeTags` | (experimental) Only test stories that match the specified tags, comma separated<br/>`test-storybook --includeTags="test-only"` |
| `--excludeTags` | (experimental) Do not test stories that match the specified tags, comma separated<br/>`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<br/>`test-storybook --skipTags="design"` |
| `--disable-telemetry` | Disable sending telemetry data<br/>`test-storybook --disable-telemetry` |

## Ejecting configuration

Expand Down
21 changes: 20 additions & 1 deletion src/test-storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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);
};

Expand Down
2 changes: 2 additions & 0 deletions src/util/getCliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type CliOptions = {
includeTags?: string;
excludeTags?: string;
skipTags?: string;
disableTelemetry?: boolean;
} & Record<string, string | boolean>;
jestOptions: JestOptions;
};
Expand All @@ -36,6 +37,7 @@ const STORYBOOK_RUNNER_COMMANDS: StorybookRunnerCommand[] = [
'includeTags',
'excludeTags',
'skipTags',
'disableTelemetry',
];

function copyOption<ObjType extends object, KeyType extends keyof ObjType>(
Expand Down
13 changes: 8 additions & 5 deletions src/util/getParsedCliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -84,7 +81,13 @@ export const getParsedCliOptions = (): ParsedCliOptions => {
.option('--failOnConsole', 'Makes tests fail on browser console errors')
.option('--includeTags <tags...>', 'Only test stories that match the specified tags')
.option('--excludeTags <tags...>', 'Do not test stories that match the specified tags')
.option('--skipTags <tags...>', 'Skip test stories that match the specified tags');
.option('--skipTags <tags...>', '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();

Expand Down
5 changes: 5 additions & 0 deletions src/util/getStorybookMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};