From 41f52fab6044fbdee4a7f5ad66ace9cf05b2015c Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Wed, 11 Dec 2024 12:37:13 +0100 Subject: [PATCH] Refactor environment variable handling for Vitest integration --- code/addons/a11y/src/utils.ts | 24 +++++++-------------- code/addons/test/src/vitest-plugin/index.ts | 12 +++++------ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/code/addons/a11y/src/utils.ts b/code/addons/a11y/src/utils.ts index 0864a2e3b2f7..f0612d5dfc73 100644 --- a/code/addons/a11y/src/utils.ts +++ b/code/addons/a11y/src/utils.ts @@ -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; } } diff --git a/code/addons/test/src/vitest-plugin/index.ts b/code/addons/test/src/vitest-plugin/index.ts index eadadb4440e5..f7274cd9dc22 100644 --- a/code/addons/test/src/vitest-plugin/index.ts +++ b/code/addons/test/src/vitest-plugin/index.ts @@ -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(); @@ -143,6 +142,10 @@ export const storybookTest = async (options?: UserOptions): Promise => { // 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 = { test: { setupFiles: [ @@ -162,9 +165,8 @@ export const storybookTest = async (options?: UserOptions): Promise => { ...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(','), @@ -239,8 +241,6 @@ export const storybookTest = async (options?: UserOptions): Promise => { }, 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' } : {}),