Skip to content

Commit

Permalink
Merge pull request #30029 from storybookjs/jeppe/fix-duplicate-include
Browse files Browse the repository at this point in the history
Vitest: Fix duplicate `test.include` patterns
  • Loading branch information
JReinhold authored Dec 12, 2024
2 parents db024c7 + 0ece491 commit 6e9fb74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
31 changes: 21 additions & 10 deletions code/addons/test/src/vitest-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
.replace('</head>', `${headHtmlSnippet ?? ''}</head>`)
.replace('<body>', `<body>${bodyHtmlSnippet ?? ''}`);
},
async config(inputConfig_DoNotMutate) {
async config(inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED) {
// ! We're not mutating the input config, instead we're returning a new partial config
// ! see https://vite.dev/guide/api-plugin.html#config
try {
Expand Down Expand Up @@ -155,8 +155,9 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
setupFiles: [
join(packageDir, 'dist/vitest-plugin/setup-file.mjs'),
// if the existing setupFiles is a string, we have to include it otherwise we're overwriting it
typeof inputConfig_DoNotMutate.test?.setupFiles === 'string' &&
inputConfig_DoNotMutate.test?.setupFiles,
typeof inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test
?.setupFiles === 'string' &&
inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test?.setupFiles,
].filter(Boolean) as string[],

...(finalOptions.storybookScript
Expand All @@ -181,7 +182,8 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
.map((path) => convertPathToPattern(path)),

// if the existing deps.inline is true, we keep it as-is, because it will inline everything
...(inputConfig_DoNotMutate.test?.server?.deps?.inline !== true
...(inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test?.server?.deps
?.inline !== true
? {
server: {
deps: {
Expand All @@ -192,7 +194,7 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
: {}),

browser: {
...inputConfig_DoNotMutate.test?.browser,
...inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test?.browser,
commands: {
getInitialGlobals: () => {
const envConfig = JSON.parse(process.env.VITEST_STORYBOOK_CONFIG ?? '{}');
Expand All @@ -209,8 +211,9 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
},
},
// if there is a test.browser config AND test.browser.screenshotFailures is not explicitly set, we set it to false
...(inputConfig_DoNotMutate.test?.browser &&
inputConfig_DoNotMutate.test.browser.screenshotFailures === undefined
...(inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test?.browser &&
inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test.browser
.screenshotFailures === undefined
? {
screenshotFailures: false,
}
Expand All @@ -219,7 +222,11 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
},

envPrefix: Array.from(
new Set([...(inputConfig_DoNotMutate.envPrefix || []), 'STORYBOOK_', 'VITE_'])
new Set([
...(inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.envPrefix || []),
'STORYBOOK_',
'VITE_',
])
),

resolve: {
Expand Down Expand Up @@ -258,8 +265,12 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin> => {
);

// alert the user of problems
if (inputConfig_DoNotMutate.test.include?.length > 0) {
console.warn(
if (
inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test.include?.length > 0
) {
// remove the user's existing include, because we're replacing it with our own heuristic based on main.ts#stories
inputConfig_ONLY_MUTATE_WHEN_STRICTLY_NEEDED_OR_YOU_WILL_BE_FIRED.test.include = [];
console.log(
picocolors.yellow(dedent`
Warning: Starting in Storybook 8.5.0-alpha.18, the "test.include" option in Vitest is discouraged in favor of just using the "stories" field in your Storybook configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default defineWorkspace([
test: {
name: "storybook",
pool: "threads",
include: [
"stories/AddonTest.stories.?(c|m)[jt]s?(x)",
],
deps: {
optimizer: {
web: {
Expand All @@ -30,4 +27,4 @@ export default defineWorkspace([
environment: "happy-dom",
},
},
]);
]);

0 comments on commit 6e9fb74

Please sign in to comment.