Skip to content

Commit

Permalink
Merge pull request #29993 from storybookjs/29797-test-addon-should-in…
Browse files Browse the repository at this point in the history
…stall-coverage-provider-out-of-the-box

Addon Test: Add `@vitest/coverage-v8` during postinstall if no coverage reporter is installed
  • Loading branch information
ghengeveld authored Dec 10, 2024
2 parents 171304c + 464f367 commit 2c17aef
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions code/addons/test/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default async function postInstall(options: PostinstallOptions) {
Adding ${picocolors.bold(colors.pink(`@storybook/experimental-nextjs-vite/vite-plugin`))} so you can use it with Vitest.
More info about the plugin at: ${picocolors.cyan(`https://github.com/storybookjs/vite-plugin-storybook-nextjs`)}
More info about the plugin at ${picocolors.cyan(`https://github.com/storybookjs/vite-plugin-storybook-nextjs`)}
`
);
try {
Expand All @@ -218,6 +218,21 @@ export default async function postInstall(options: PostinstallOptions) {
}
}

const v8Version = await packageManager.getInstalledVersion('@vitest/coverage-v8');
const istanbulVersion = await packageManager.getInstalledVersion('@vitest/coverage-istanbul');
if (!v8Version && !istanbulVersion) {
printInfo(
'🙈 Let me cover this for you',
dedent`
You don't seem to have a coverage reporter installed. Vitest needs either V8 or Istanbul to generate coverage reports.
Adding ${picocolors.bold(colors.pink(`@vitest/coverage-v8`))} to enable coverage reporting.
Read more about Vitest coverage providers at ${picocolors.cyan(`https://vitest.dev/guide/coverage.html#coverage-providers`)}
`
);
dependencies.push(`@vitest/coverage-v8`); // Version specifier is added below
}

const versionedDependencies = dependencies.map((p) => {
if (p.includes('vitest')) {
return `${p}@${vitestVersionToInstall ?? 'latest'}`;
Expand Down Expand Up @@ -311,7 +326,7 @@ export default async function postInstall(options: PostinstallOptions) {
Found an existing Vitest workspace file:
${colors.gray(vitestWorkspaceFile)}
I was able to configure most of the addon but could not safely extend
I was able to configure most of the addon but could not safely extend
your existing workspace file automatically, you must do it yourself. This was the last step.
Please refer to the documentation to complete the setup manually:
Expand All @@ -333,7 +348,7 @@ export default async function postInstall(options: PostinstallOptions) {
You seem to have an existing test configuration in your Vite config file:
${colors.gray(vitestWorkspaceFile || '')}
I was able to configure most of the addon but could not safely extend
I was able to configure most of the addon but could not safely extend
your existing workspace file automatically, you must do it yourself. This was the last step.
Please refer to the documentation to complete the setup manually:
Expand Down Expand Up @@ -369,7 +384,7 @@ export default async function postInstall(options: PostinstallOptions) {
{
extends: '${viteConfigFile ? relative(dirname(browserWorkspaceFile), viteConfigFile) : ''}',
plugins: [
// The plugin will run tests for the stories defined in your Storybook config
// The plugin will run tests for the stories defined in your Storybook config
// See options at: https://storybook.js.org/docs/writing-tests/vitest-plugin#storybooktest
storybookTest({ configDir: '${options.configDir}' }),${vitestInfo.frameworkPluginDocs + vitestInfo.frameworkPluginCall}
],
Expand Down Expand Up @@ -406,7 +421,7 @@ export default async function postInstall(options: PostinstallOptions) {
// More info at: https://storybook.js.org/docs/writing-tests/vitest-plugin
export default defineConfig({
plugins: [
// The plugin will run tests for the stories defined in your Storybook config
// The plugin will run tests for the stories defined in your Storybook config
// See options at: https://storybook.js.org/docs/writing-tests/vitest-plugin#storybooktest
storybookTest({ configDir: '${options.configDir}' }),${vitestInfo.frameworkPluginDocs + vitestInfo.frameworkPluginCall}
],
Expand Down

0 comments on commit 2c17aef

Please sign in to comment.