Skip to content

Commit

Permalink
Merge pull request #30022 from storybookjs/valentin/fix-accessibility…
Browse files Browse the repository at this point in the history
…-always-running

Addon A11y: Refactor environment variable handling for Vitest integration
  • Loading branch information
valentinpalkovic authored Dec 11, 2024
2 parents 48d1d42 + 41f52fa commit ed64194
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
24 changes: 8 additions & 16 deletions code/addons/a11y/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
export function getIsVitestStandaloneRun() {
try {
return process.env.VITEST_STORYBOOK === 'false';
} catch {
try {
// @ts-expect-error Suppress TypeScript warning about wrong setting. Doesn't matter, because we don't use tsc for bundling.
return import.meta.env.VITEST_STORYBOOK === 'false';
} catch (e) {
return false;
}
// @ts-expect-error Suppress TypeScript warning about wrong setting. Doesn't matter, because we don't use tsc for bundling.
return import.meta.env.VITEST_STORYBOOK === 'false';
} catch (e) {
return false;
}
}

export function getIsVitestRunning() {
try {
return process?.env.MODE === 'test';
} catch {
try {
// @ts-expect-error Suppress TypeScript warning about wrong setting. Doesn't matter, because we don't use tsc for bundling.
return import.meta.env.MODE === 'test';
} catch (e) {
return false;
}
// @ts-expect-error Suppress TypeScript warning about wrong setting. Doesn't matter, because we don't use tsc for bundling.
return import.meta.env.MODE === 'test';
} catch (e) {
return false;
}
}
12 changes: 6 additions & 6 deletions code/addons/test/src/vitest-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import sirv from 'sirv';
import { convertPathToPattern } from 'tinyglobby';
import { dedent } from 'ts-dedent';

import { TestManager } from '../node/test-manager';
import type { InternalOptions, UserOptions } from './types';

const WORKING_DIR = process.cwd();
Expand Down Expand Up @@ -143,6 +142,10 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
// plugin.name?.startsWith('vitest:browser')
// )

// We signal the test runner that we are not running it via Storybook
// We are overriding the environment variable to 'true' if vitest runs via @storybook/addon-test's backend
const vitestStorybook = process.env.VITEST_STORYBOOK ?? 'false';

const baseConfig: Omit<ViteUserConfig, 'plugins'> = {
test: {
setupFiles: [
Expand All @@ -162,9 +165,8 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
...storybookEnv,
// To be accessed by the setup file
__STORYBOOK_URL__: finalOptions.storybookUrl,
// We signal the test runner that we are not running it via Storybook
// We are overriding the environment variable to 'true' if vitest runs via @storybook/addon-test's backend
VITEST_STORYBOOK: 'false',

VITEST_STORYBOOK: vitestStorybook,
__VITEST_INCLUDE_TAGS__: finalOptions.tags.include.join(','),
__VITEST_EXCLUDE_TAGS__: finalOptions.tags.exclude.join(','),
__VITEST_SKIP_TAGS__: finalOptions.tags.skip.join(','),
Expand Down Expand Up @@ -239,8 +241,6 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
},

define: {
// polyfilling process.env.VITEST_STORYBOOK to 'false' in the browser
'process.env.VITEST_STORYBOOK': JSON.stringify('false'),
...(frameworkName?.includes('vue3')
? { __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false' }
: {}),
Expand Down

0 comments on commit ed64194

Please sign in to comment.