From 36dae70fb0cb7ce3b046867fbbc266be25c189d1 Mon Sep 17 00:00:00 2001 From: Mike Gregson Date: Thu, 27 Jul 2023 18:49:56 +0100 Subject: [PATCH] feat(vite): add v8 coverage support to vitest generator (#18114) --- .../packages/vite/generators/vitest.json | 4 +-- .../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 | 1 + 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/docs/generated/packages/vite/generators/vitest.json b/docs/generated/packages/vite/generators/vitest.json index a4d5b38584f73..f9b72140e3a37 100644 --- a/docs/generated/packages/vite/generators/vitest.json +++ b/docs/generated/packages/vite/generators/vitest.json @@ -32,8 +32,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/schema.d.ts b/packages/vite/src/generators/vitest/schema.d.ts index 0cc157eabb83e..5b5039be21855 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 cb84f962042dd..6f98e0f49bcb2 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 c5ca7e709d524..0ff7d26f4d8f8 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 e3e4ab39984ee..ab7f9a103e3d0 100644 --- a/packages/vite/src/utils/versions.ts +++ b/packages/vite/src/utils/versions.ts @@ -11,4 +11,5 @@ export const edgeRuntimeVmVersion = '~3.0.2'; // Coverage providers export const vitestCoverageC8Version = '~0.32.0'; +export const vitestCoverageV8Version = '~0.32.0'; export const vitestCoverageIstanbulVersion = '~0.32.0';