Skip to content

Commit

Permalink
feat(vite): add v8 coverage support to vitest generator (#18114)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgregson authored Jul 27, 2023
1 parent 2dc6656 commit 36dae70
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/generated/packages/vite/generators/vitest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"coverageProvider": {
"type": "string",
"enum": ["c8", "istanbul"],
"default": "c8",
"enum": ["v8", "c8", "istanbul"],
"default": "v8",
"description": "Coverage provider to use."
},
"testTarget": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/generators/vitest/schema.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/generators/vitest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"project": {
"type": "string",
"description": "The name of the project to test.",
"$default": { "$source": "projectName" }
"$default": {
"$source": "projectName"
}
},
"uiFramework": {
"type": "string",
Expand All @@ -29,8 +31,8 @@
},
"coverageProvider": {
"type": "string",
"enum": ["c8", "istanbul"],
"default": "c8",
"enum": ["v8", "c8", "istanbul"],
"default": "v8",
"description": "Coverage provider to use."
},
"testTarget": {
Expand Down
32 changes: 25 additions & 7 deletions packages/vite/src/generators/vitest/vitest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import initGenerator from '../init/init';
import {
vitestCoverageC8Version,
vitestCoverageIstanbulVersion,
vitestCoverageV8Version,
} from '../../utils/versions';

import { addTsLibDependencies } from '@nx/js';
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
1 change: 1 addition & 0 deletions packages/vite/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

1 comment on commit 36dae70

@vercel
Copy link

@vercel vercel bot commented on 36dae70 Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.