Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vite): add v8 coverage support to vitest generator #18114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';