Skip to content

Commit

Permalink
fix(vite): vitest migration add reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Dec 18, 2023
1 parent 1b464d8 commit fa85165
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 151 deletions.
6 changes: 3 additions & 3 deletions packages/vite/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"description": "Update vite config.",
"implementation": "./src/migrations/update-17-2-0/update-vite-config"
},
"vitest-coverage-threshold": {
"vitest-coverage-and-reporters": {
"version": "17.3.0-beta.0",
"description": "Move the vitest coverage thresholds in their own object if exists.",
"implementation": "./src/migrations/update-17-3-0/vitest-coverage-threshold"
"description": "Move the vitest coverage thresholds in their own object if exists and add reporters.",
"implementation": "./src/migrations/update-17-3-0/vitest-coverage-and-reporters"
}
},
"packageJsonUpdates": {
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ export async function* viteDevServerExecutor(
projectRoot,
buildTargetOptions.configFile
);
const { serverOptions, otherOptions } = await getServerExtraArgs(options);
const { serverOptions, otherOptions } = await getServerExtraArgs({
...options,
...buildTargetOptions,
});
const resolved = await loadConfigFromFile(
{
mode: otherOptions?.mode ?? 'development',
mode: otherOptions?.mode ?? buildTargetOptions?.['mode'] ?? 'development',
command: 'serve',
},
viteConfigPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function* vitePreviewServerExecutor(
const { previewOptions, otherOptions } = await getExtraArgs(options);
const resolved = await loadConfigFromFile(
{
mode: otherOptions?.mode ?? 'production',
mode: otherOptions?.mode ?? otherOptionsFromBuild?.mode ?? 'production',
command: 'build',
},
viteConfigPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
cacheDir: '../../node_modules/.vite/demo6',
test: {
reporters: ['default'],
globals: true,
cache: {
dir: '../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
thresholds: {
branches: 75,
Expand All @@ -37,13 +37,13 @@ import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
cacheDir: '../../node_modules/.vite/demo3',
test: {
reporters: ['default'],
globals: true,
cache: {
dir: '../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
thresholds: {
lines: 100,
Expand Down Expand Up @@ -97,13 +97,13 @@ import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
cacheDir: '../../node_modules/.vite/demo5',
test: {
reporters: ['default'],
globals: true,
cache: {
dir: '../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
thresholds: {
lines: 100,
Expand All @@ -128,13 +128,13 @@ import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
cacheDir: '../../node_modules/.vite/demo2',
test: {
reporters: ['default'],
globals: true,
cache: {
dir: '../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../coverage/demo2',
provider: 'v8',
Expand All @@ -153,6 +153,7 @@ import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
cacheDir: '../../node_modules/.vite/demo',
test: {
reporters: ['default'],
globals: true,
cache: {
dir: '../../node_modules/.vitest',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { ChangeType, applyChangesToString } from '@nx/devkit';
import { tsquery } from '@phenomnomnominal/tsquery';
import ts = require('typescript');

export function fixCoverageAndRerporters(
configContents: string
): string | undefined {
const configNode = getConfigNode(configContents);
if (!configNode) {
return;
}

const testHasCoverage = tsquery.query(
configNode,
`PropertyAssignment:has(Identifier[name="test"]):has(PropertyAssignment:has(Identifier[name="coverage"]))`
)?.[0];
let changes = [];

if (testHasCoverage) {
const testObjectLiteralExpressionNode = tsquery.query(
testHasCoverage,
`ObjectLiteralExpression:has(Identifier[name="coverage"])`
)?.[0];
const coverageNode = findCoverageNode(testObjectLiteralExpressionNode);

if (!coverageNode) {
return;
}

const linesNode = tsquery.query(
coverageNode,
`PropertyAssignment:has(Identifier[name="lines"])`
)?.[0];

const statementsNode = tsquery.query(
coverageNode,
`PropertyAssignment:has(Identifier[name="statements"])`
)?.[0];

const functionsNode = tsquery.query(
coverageNode,
`PropertyAssignment:has(Identifier[name="functions"])`
)?.[0];

const branchesNode = tsquery.query(
coverageNode,
`PropertyAssignment:has(Identifier[name="branches"])`
)?.[0];

if (linesNode) {
changes.push({
type: ChangeType.Delete,
start: linesNode.getStart(),
length: linesNode.getWidth() + 1,
});
}
if (statementsNode) {
changes.push({
type: ChangeType.Delete,
start: statementsNode.getStart(),
length: statementsNode.getWidth() + 1,
});
}

if (functionsNode) {
changes.push({
type: ChangeType.Delete,
start: functionsNode.getStart(),
length: functionsNode.getWidth() + 1,
});
}

if (branchesNode) {
changes.push({
type: ChangeType.Delete,
start: branchesNode.getStart(),
length: branchesNode.getWidth() + 1,
});
}

if (branchesNode || functionsNode || statementsNode || linesNode) {
changes.push({
type: ChangeType.Insert,
index: coverageNode.getStart() + 1,
text: `thresholds: {
${linesNode ? linesNode.getText() + ',' : ''}
${statementsNode ? statementsNode.getText() + ',' : ''}
${functionsNode ? functionsNode.getText() + ',' : ''}
${branchesNode ? branchesNode.getText() + ',' : ''}
},`,
});
}
}

const testHasReporters = tsquery.query(
configNode,
`PropertyAssignment:has(Identifier[name="test"]):has(PropertyAssignment:has(Identifier[name="reporters"]))`
)?.[0];

if (!testHasReporters) {
const testObject = tsquery.query(
configNode,
`PropertyAssignment:has(Identifier[name="test"])`
)?.[0];
changes.push({
type: ChangeType.Insert,
index: testObject.getStart() + `test: {`.length + 1,
text: `reporters: ['default'],`,
});
}

if (changes.length > 0) {
return applyChangesToString(configContents, changes);
} else {
return;
}
}

export function getConfigNode(configFileContents: string): ts.Node | undefined {
if (!configFileContents) {
return;
}
let configNode = tsquery.query(
configFileContents,
`ObjectLiteralExpression`
)?.[0];

const arrowFunctionReturnStatement = tsquery.query(
configFileContents,
`ArrowFunction Block ReturnStatement ObjectLiteralExpression`
)?.[0];

if (arrowFunctionReturnStatement) {
configNode = arrowFunctionReturnStatement;
}

return configNode;
}

function findCoverageNode(testNode: ts.Node) {
let coverageNode: ts.Node | undefined;
testNode.forEachChild((child) => {
if (ts.isPropertyAssignment(child) && child.name.getText() === 'coverage') {
coverageNode = child.initializer;
}
});
return coverageNode;
}
132 changes: 0 additions & 132 deletions packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts

This file was deleted.

Loading

0 comments on commit fa85165

Please sign in to comment.