From 6fdbf4ed34251663d4d093ddd48454eb49ae1cf7 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Mon, 18 Dec 2023 17:03:32 +0200 Subject: [PATCH] fix(vite): vitest migration add reporters --- packages/vite/migrations.json | 6 +- .../executors/dev-server/dev-server.impl.ts | 7 +- .../preview-server/preview-server.impl.ts | 2 +- .../vitest-coverage-threshold.spec.ts.snap | 9 +- .../lib/fix-coverage-and-reporters.ts | 148 ++++++++++++++++++ .../update-17-3-0/lib/fix-coverage.ts | 132 ---------------- ... => vitest-coverage-and-reporters.spec.ts} | 6 +- ...ld.ts => vitest-coverage-and-reporters.ts} | 6 +- 8 files changed, 165 insertions(+), 151 deletions(-) create mode 100644 packages/vite/src/migrations/update-17-3-0/lib/fix-coverage-and-reporters.ts delete mode 100644 packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts rename packages/vite/src/migrations/update-17-3-0/{vitest-coverage-threshold.spec.ts => vitest-coverage-and-reporters.spec.ts} (97%) rename packages/vite/src/migrations/update-17-3-0/{vitest-coverage-threshold.ts => vitest-coverage-and-reporters.ts} (89%) diff --git a/packages/vite/migrations.json b/packages/vite/migrations.json index 61d147093cbf7e..6a7b8709e3afa6 100644 --- a/packages/vite/migrations.json +++ b/packages/vite/migrations.json @@ -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": { diff --git a/packages/vite/src/executors/dev-server/dev-server.impl.ts b/packages/vite/src/executors/dev-server/dev-server.impl.ts index eaa16e03cc75d1..bcc2a27bf6342c 100644 --- a/packages/vite/src/executors/dev-server/dev-server.impl.ts +++ b/packages/vite/src/executors/dev-server/dev-server.impl.ts @@ -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 diff --git a/packages/vite/src/executors/preview-server/preview-server.impl.ts b/packages/vite/src/executors/preview-server/preview-server.impl.ts index 085b2d212c10ff..4f6bc219fc9703 100644 --- a/packages/vite/src/executors/preview-server/preview-server.impl.ts +++ b/packages/vite/src/executors/preview-server/preview-server.impl.ts @@ -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 diff --git a/packages/vite/src/migrations/update-17-3-0/__snapshots__/vitest-coverage-threshold.spec.ts.snap b/packages/vite/src/migrations/update-17-3-0/__snapshots__/vitest-coverage-threshold.spec.ts.snap index 971479c7fca0b1..ddb37ceb52c0c6 100644 --- a/packages/vite/src/migrations/update-17-3-0/__snapshots__/vitest-coverage-threshold.spec.ts.snap +++ b/packages/vite/src/migrations/update-17-3-0/__snapshots__/vitest-coverage-threshold.spec.ts.snap @@ -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, @@ -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, @@ -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, @@ -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', @@ -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', diff --git a/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage-and-reporters.ts b/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage-and-reporters.ts new file mode 100644 index 00000000000000..6e0a1418842c13 --- /dev/null +++ b/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage-and-reporters.ts @@ -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; +} diff --git a/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts b/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts deleted file mode 100644 index 7f1d26accd3fe6..00000000000000 --- a/packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts +++ /dev/null @@ -1,132 +0,0 @@ -import { ChangeType, applyChangesToString } from '@nx/devkit'; -import { tsquery } from '@phenomnomnominal/tsquery'; -import ts = require('typescript'); - -export function fixCoverage(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) { - return; - } - const testObjectLiteralExpressionNode = tsquery.query( - testHasCoverage, - `ObjectLiteralExpression:has(Identifier[name="coverage"])` - )?.[0]; - const coverageNode = findCoverageNode(testObjectLiteralExpressionNode); - console.log('coverage node', coverageNode?.getText()); - console.log('coverage node getStart', coverageNode?.getStart()); - - 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() + ',' : ''} - },`, - }); - } - - 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; -} diff --git a/packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.spec.ts b/packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.spec.ts similarity index 97% rename from packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.spec.ts rename to packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.spec.ts index 25f2b9736913f9..4f8686797d71be 100644 --- a/packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.spec.ts +++ b/packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.spec.ts @@ -1,6 +1,6 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { Tree, addProjectConfiguration } from '@nx/devkit'; -import fixCoverageThreshold from './vitest-coverage-threshold'; +import fixCoverageThreshold from './vitest-coverage-and-reporters'; describe('vitest-coverage-threshold migration', () => { let tree: Tree; @@ -110,7 +110,6 @@ export default defineConfig({ }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], - reporters: ['default'], coverage: { reportsDirectory: '../coverage/${name}', provider: 'v8', @@ -150,7 +149,6 @@ export default defineConfig({ }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], - reporters: ['default'], coverage: { reportsDirectory: '../coverage/${name}', provider: 'v8', @@ -235,7 +233,6 @@ export default defineConfig({ }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], - reporters: ['default'], coverage: { reportsDirectory: '../coverage/${name}', provider: 'v8', @@ -278,7 +275,6 @@ export default defineConfig({ }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], - reporters: ['default'], coverage: { reportsDirectory: '../coverage/${name}', provider: 'v8', diff --git a/packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.ts b/packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.ts similarity index 89% rename from packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.ts rename to packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.ts index 824afb4bdf360c..2d3b272d748bc8 100644 --- a/packages/vite/src/migrations/update-17-3-0/vitest-coverage-threshold.ts +++ b/packages/vite/src/migrations/update-17-3-0/vitest-coverage-and-reporters.ts @@ -6,7 +6,7 @@ import { joinPathFragments, } from '@nx/devkit'; import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils'; -import { fixCoverage } from './lib/fix-coverage'; +import { fixCoverageAndRerporters } from './lib/fix-coverage-and-reporters'; export default async function fixCoverageThreshold(tree: Tree) { const projects = getProjects(tree); @@ -19,9 +19,7 @@ export default async function fixCoverageThreshold(tree: Tree) { return; } const configContents = tree.read(configPath, 'utf-8') as string; - - const updatedConfigContents = fixCoverage(configContents); - + const updatedConfigContents = fixCoverageAndRerporters(configContents); if (updatedConfigContents) { tree.write(configPath, updatedConfigContents); }