Skip to content

Commit

Permalink
feat(vite): add v8 coverage support to vitest generator
Browse files Browse the repository at this point in the history
closed nrwl#17886
  • Loading branch information
mpgregson committed Jul 14, 2023
1 parent 4f9f04f commit cc454b7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 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
12 changes: 12 additions & 0 deletions packages/vite/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"@vitest/coverage-istanbul": {
"version": "^0.32.0",
"alwaysAddToPackageJson": false
},
"@vitest/coverage-v8": {
"version": "^0.32.0",
"alwaysAddToPackageJson": false
}
}
},
Expand Down Expand Up @@ -104,6 +108,10 @@
"@vitest/coverage-istanbul": {
"version": "^0.31.0",
"alwaysAddToPackageJson": false
},
"@vitest/coverage-v8": {
"version": "^0.31.0",
"alwaysAddToPackageJson": false
}
}
},
Expand Down Expand Up @@ -150,6 +158,10 @@
"@vitest/coverage-istanbul": {
"version": "^0.25.8",
"alwaysAddToPackageJson": false
},
"@vitest/coverage-v8": {
"version": "^0.25.8",
"alwaysAddToPackageJson": false
}
}
}
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);
5 changes: 3 additions & 2 deletions packages/vite/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit cc454b7

Please sign in to comment.