-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Vitest: Fix add command #28975
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
24f6fc0
Only log Next.js message when dealing with a Next.js project
ghengeveld 896e3f0
Also create workspace file if there's a Vite config file, and extend …
ghengeveld d3d6fd2
Remove (disable) isolate option because it sometimes doesn't show sto…
ghengeveld ad31d48
Support JSX file extensions as well
ghengeveld 02ba82a
Merge branch 'next' into vitest-addon-fixes
kasperpeulen 534fb2b
Use relative path from workspace to vite.config in extends path
ghengeveld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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( | ||
|
@@ -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 || ''}', | ||
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: '',/, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
` | ||
|
@@ -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: [ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)}',