From 676964857fbce1489eb7d5981772360e283f75db Mon Sep 17 00:00:00 2001 From: Mike Gregson Date: Fri, 14 Jul 2023 08:40:27 +0100 Subject: [PATCH] feat(vite): add v8 coverage support to vitest generator closed #17886 --- packages/vite/migrations.json | 12 +++++++ .../vite/src/generators/vitest/schema.d.ts | 2 +- .../vite/src/generators/vitest/schema.json | 8 +++-- .../src/generators/vitest/vitest-generator.ts | 32 +++++++++++++++---- packages/vite/src/utils/versions.ts | 5 +-- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/packages/vite/migrations.json b/packages/vite/migrations.json index b86d5bfcdee8bc..94ec6de19bf1a6 100644 --- a/packages/vite/migrations.json +++ b/packages/vite/migrations.json @@ -75,6 +75,10 @@ "@vitest/coverage-istanbul": { "version": "^0.32.0", "alwaysAddToPackageJson": false + }, + "@vitest/coverage-v8": { + "version": "^0.32.0", + "alwaysAddToPackageJson": false } } }, @@ -104,6 +108,10 @@ "@vitest/coverage-istanbul": { "version": "^0.31.0", "alwaysAddToPackageJson": false + }, + "@vitest/coverage-v8": { + "version": "^0.31.0", + "alwaysAddToPackageJson": false } } }, @@ -150,6 +158,10 @@ "@vitest/coverage-istanbul": { "version": "^0.25.8", "alwaysAddToPackageJson": false + }, + "@vitest/coverage-v8": { + "version": "^0.25.8", + "alwaysAddToPackageJson": false } } } diff --git a/packages/vite/src/generators/vitest/schema.d.ts b/packages/vite/src/generators/vitest/schema.d.ts index 0cc157eabb83ed..5b5039be218551 100644 --- a/packages/vite/src/generators/vitest/schema.d.ts +++ b/packages/vite/src/generators/vitest/schema.d.ts @@ -1,7 +1,7 @@ export interface VitestGeneratorSchema { project: string; uiFramework: 'react' | 'none'; - coverageProvider: 'c8' | 'istanbul'; + coverageProvider: 'v8' | 'c8' | 'istanbul'; inSourceTests?: boolean; skipViteConfig?: boolean; testTarget?: string; diff --git a/packages/vite/src/generators/vitest/schema.json b/packages/vite/src/generators/vitest/schema.json index cb84f962042dde..6f98e0f49bcb28 100644 --- a/packages/vite/src/generators/vitest/schema.json +++ b/packages/vite/src/generators/vitest/schema.json @@ -9,7 +9,9 @@ "project": { "type": "string", "description": "The name of the project to test.", - "$default": { "$source": "projectName" } + "$default": { + "$source": "projectName" + } }, "uiFramework": { "type": "string", @@ -29,8 +31,8 @@ }, "coverageProvider": { "type": "string", - "enum": ["c8", "istanbul"], - "default": "c8", + "enum": ["v8", "c8", "istanbul"], + "default": "v8", "description": "Coverage provider to use." }, "testTarget": { diff --git a/packages/vite/src/generators/vitest/vitest-generator.ts b/packages/vite/src/generators/vitest/vitest-generator.ts index c5ca7e709d524a..0ff7d26f4d8f8a 100644 --- a/packages/vite/src/generators/vitest/vitest-generator.ts +++ b/packages/vite/src/generators/vitest/vitest-generator.ts @@ -22,6 +22,7 @@ import initGenerator from '../init/init'; import { vitestCoverageC8Version, vitestCoverageIstanbulVersion, + vitestCoverageV8Version, } from '../../utils/versions'; import { addTsLibDependencies } from '@nx/js'; @@ -64,16 +65,14 @@ export async function vitestGenerator( createFiles(tree, schema, root); updateTsConfig(tree, schema, root); + const coverageProviderDependency = getCoverageProviderDependency( + schema.coverageProvider + ); + const installCoverageProviderTask = addDependenciesToPackageJson( tree, {}, - schema.coverageProvider === 'istanbul' - ? { - '@vitest/coverage-istanbul': vitestCoverageIstanbulVersion, - } - : { - '@vitest/coverage-c8': vitestCoverageC8Version, - } + coverageProviderDependency ); tasks.push(installCoverageProviderTask); @@ -150,5 +149,24 @@ function createFiles( }); } +function getCoverageProviderDependency( + coverageProvider: VitestGeneratorSchema['coverageProvider'] +) { + switch (coverageProvider) { + case 'c8': + return { + '@vitest/coverage-c8': vitestCoverageC8Version, + }; + case 'istanbul': + return { + '@vitest/coverage-istanbul': vitestCoverageIstanbulVersion, + }; + default: + return { + '@vitest/coverage-v8': vitestCoverageV8Version, + }; + } +} + export default vitestGenerator; export const vitestSchematic = convertNxGenerator(vitestGenerator); diff --git a/packages/vite/src/utils/versions.ts b/packages/vite/src/utils/versions.ts index cd3c3210791a3b..2af9c80c0d5de4 100644 --- a/packages/vite/src/utils/versions.ts +++ b/packages/vite/src/utils/versions.ts @@ -11,5 +11,6 @@ export const happyDomVersion = '~9.20.3'; export const edgeRuntimeVmVersion = '~3.0.2'; // Coverage providers -export const vitestCoverageC8Version = '~0.32.0'; -export const vitestCoverageIstanbulVersion = '~0.32.0'; +export const vitestCoverageC8Version = '^0.32.0'; +export const vitestCoverageV8Version = '^0.32.0'; +export const vitestCoverageIstanbulVersion = '^0.32.0';