Skip to content

Commit

Permalink
Merge pull request #30054 from storybookjs/valentin/fix-environment-d…
Browse files Browse the repository at this point in the history
…etection-for-vitest

Addon Test: Set environment variable for Vitest to indicate Storybook integration
  • Loading branch information
valentinpalkovic authored Dec 13, 2024
2 parents a6a89d8 + fb986dc commit c20e364
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions code/addons/test/src/node/vitest-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type TagsFilter = {

const packageDir = dirname(require.resolve('@storybook/experimental-addon-test/package.json'));

// We have to tell Vitest that it runs as part of Storybook
process.env.VITEST_STORYBOOK = 'true';

export class VitestManager {
vitest: Vitest | null = null;

Expand Down Expand Up @@ -65,31 +68,17 @@ export class VitestManager {
: { enabled: false }
) as CoverageOptions;

this.vitest = await createVitest(
'test',
{
watch: true,
passWithNoTests: false,
// TODO:
// Do we want to enable Vite's default reporter?
// The output in the terminal might be too spamy and it might be better to
// find a way to just show errors and warnings for example
// Otherwise it might be hard for the user to discover Storybook related logs
reporters: ['default', new StorybookReporter(this.testManager)],
coverage: coverageOptions,
},
{
define: {
// polyfilling process.env.VITEST_STORYBOOK to 'true' in the browser
'process.env.VITEST_STORYBOOK': 'true',
},
}
);

this.vitest.configOverride.env = {
// We signal to the test runner that we are running it via Storybook
VITEST_STORYBOOK: 'true',
};
this.vitest = await createVitest('test', {
watch: true,
passWithNoTests: false,
// TODO:
// Do we want to enable Vite's default reporter?
// The output in the terminal might be too spamy and it might be better to
// find a way to just show errors and warnings for example
// Otherwise it might be hard for the user to discover Storybook related logs
reporters: ['default', new StorybookReporter(this.testManager)],
coverage: coverageOptions,
});

if (this.vitest) {
this.vitest.onCancel(() => {
Expand Down

0 comments on commit c20e364

Please sign in to comment.