From 860edead57f8d6b380c44a80099d679d9be80a0f Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 9 Dec 2024 14:31:23 +0100 Subject: [PATCH 1/2] Detect presence of coverage reporter and install coverage-v8 if none is present --- code/addons/test/src/postinstall.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/code/addons/test/src/postinstall.ts b/code/addons/test/src/postinstall.ts index 3ea45bd01bf7..e9546690a86a 100644 --- a/code/addons/test/src/postinstall.ts +++ b/code/addons/test/src/postinstall.ts @@ -219,6 +219,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. + If you prefer Istanbul, you can remove ${picocolors.bold(colors.pink(`@vitest/coverage-v8`))} and install ${picocolors.bold(colors.pink(`@vitest/coverage-istanbul`))} instead. + ` + ); + dependencies.push(`@vitest/coverage-v8`); // Version specifier is added below + } + const versionedDependencies = dependencies.map((p) => { if (p.includes('vitest')) { return `${p}@${vitestVersionToInstall ?? 'latest'}`; @@ -295,7 +310,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: @@ -317,7 +332,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: @@ -353,7 +368,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} ], @@ -390,7 +405,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} ], From 57efbcbec9d107b730588794e5ab896702a39efe Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 9 Dec 2024 15:59:45 +0100 Subject: [PATCH 2/2] Link to Vitest docs for more info --- code/addons/test/src/postinstall.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/addons/test/src/postinstall.ts b/code/addons/test/src/postinstall.ts index e9546690a86a..dc4710a9ea7d 100644 --- a/code/addons/test/src/postinstall.ts +++ b/code/addons/test/src/postinstall.ts @@ -206,7 +206,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 { @@ -228,7 +228,7 @@ export default async function postInstall(options: PostinstallOptions) { 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. - If you prefer Istanbul, you can remove ${picocolors.bold(colors.pink(`@vitest/coverage-v8`))} and install ${picocolors.bold(colors.pink(`@vitest/coverage-istanbul`))} instead. + 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