Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vitest: Fix add command #28975

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Changes from 3 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
118 changes: 57 additions & 61 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ export default async function postInstall(options: PostinstallOptions) {

const packages = ['vitest@latest', '@vitest/browser@latest', 'playwright@latest'];

logger.info(
dedent`
We detected that you're using Next.js.
We will configure the vite-plugin-storybook-nextjs plugin to allow you to run tests in Vitest.
`
);

if (info.frameworkPackageName === '@storybook/nextjs') {
logger.info(
dedent`
We detected that you're using Next.js.
We will configure the vite-plugin-storybook-nextjs plugin to allow you to run tests in Vitest.
`
);
packages.push('vite-plugin-storybook-nextjs@latest');
}

logger.info(c.bold('Installing packages...'));
logger.info(packages.join(', '));
await packageManager.addDependencies({ installAsDevDependencies: true }, packages);
Expand All @@ -92,21 +92,22 @@ export default async function postInstall(options: PostinstallOptions) {
import { beforeAll } from 'vitest'
import { setProjectAnnotations } from '${annotationsImport}'
${previewExists ? `import * as projectAnnotations from './preview'` : ''}

const project = setProjectAnnotations(${previewExists ? 'projectAnnotations' : '[]'})

beforeAll(project.beforeAll)
`
);

const configFiles = extensions.map((ext) => 'vitest.config' + ext);

const rootConfig = await findUp(configFiles, {
cwd: process.cwd(),
});
// Check for an existing config file. Can be from Vitest (preferred) or Vite (with `test` option).
const viteConfigFiles = extensions.map((ext) => 'vite.config' + ext);
const viteConfig = await findUp(viteConfigFiles, { cwd: process.cwd() });
const vitestConfigFiles = extensions.map((ext) => 'vitest.config' + ext);
const rootConfig = (await findUp(vitestConfigFiles, { cwd: process.cwd() })) || viteConfig;

if (rootConfig) {
const extname = rootConfig ? path.extname(rootConfig) : '.ts';
// If there's an existing config, we create a workspace file so we can run Storybook tests alongside.
const extname = path.extname(rootConfig);
const browserWorkspaceFile = resolve(dirname(rootConfig), `vitest.workspace${extname}`);
if (existsSync(browserWorkspaceFile)) {
logger.info(
Expand All @@ -121,66 +122,62 @@ export default async function postInstall(options: PostinstallOptions) {
await writeFile(
browserWorkspaceFile,
dedent`
import { defineWorkspace } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-vitest/plugin';
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineWorkspace([
'${relative(dirname(browserWorkspaceFile), rootConfig)}',
{
plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
include: ['**/*.stories.?(m)[jt]s?(x)'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
import { defineWorkspace } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-vitest/plugin';
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineWorkspace([
'${relative(dirname(browserWorkspaceFile), rootConfig)}',
{
extends: '${viteConfig || ''}',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no viteConfig it now extends an empty string.
But in that case, I think we should just not extend anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we want to make this a relative path, similar as the one above:
'${relative(dirname(browserWorkspaceFile), rootConfig)}',

plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
browser: {
enabled: true,
headless: true,
name: 'chromium',
provider: 'playwright',
},
include: ['**/*.stories.?(m)[jt]s?(x)'],
setupFiles: ['./.storybook/vitest.setup.ts'],
},
// Disabling isolation is faster and is similar to how tests are isolated in storybook itself.
// Consider removing this if you are seeing problems with your tests.
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
},
]);
`
]);
`.replace(/\s+extends: '',/, '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see

);
}
} else {
// If there's no existing Vitest/Vite config, we create a new Vitest config file.
logger.info(c.bold('Writing vitest.config.ts file...'));
await writeFile(
resolve('vitest.config.ts'),
dedent`
import { defineConfig } from "vitest/config";
import { storybookTest } from "@storybook/experimental-addon-vitest/plugin";
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineConfig({
plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
include: ['**/*.stories.?(m)[jt]s?(x)'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
import { defineConfig } from "vitest/config";
import { storybookTest } from "@storybook/experimental-addon-vitest/plugin";
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineConfig({
plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
browser: {
enabled: true,
headless: true,
name: 'chromium',
provider: 'playwright',
},
include: ['**/*.stories.?(m)[jt]s?(x)'],
setupFiles: ['./.storybook/vitest.setup.ts'],
},
// Disabling isolation is faster and is similar to how tests are isolated in storybook itself.
// Consider removing this, if you have flaky tests.
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
});
`
});
`
);
}

logger.info(
dedent`
The Vitest addon is now configured and you're ready to run your tests!
The Vitest addon is now configured and you're ready to run your tests!
Check the documentation for more information about its features and options at:
https://storybook.js.org/docs/writing-tests/test-runner-with-vitest
`
Expand Down Expand Up @@ -215,7 +212,6 @@ async function getFrameworkInfo({ configDir, packageManager: pkgMgr }: Postinsta
validateFrameworkName(frameworkName);
const frameworkPackageName = extractProperFrameworkName(frameworkName);

console.log(configDir);
const presets = await loadAllPresets({
corePresets: [join(frameworkName, 'preset')],
overridePresets: [
Expand Down
Loading