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

fix(vite): update vitest and use parseCLI #21890

Merged
merged 1 commit into from
Feb 22, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated/packages/vite/executors/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"type": "string",
"description": "Directory to write coverage report to."
},
"mode": { "type": "string", "description": "Mode for Vite." },
"testFiles": {
"aliases": ["testFile"],
"type": "array",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
"use-sync-external-store": "^1.2.0",
"verdaccio": "^5.0.4",
"vite": "5.0.8",
"vitest": "^1.0.4",
"vitest": "^1.3.1",
"webpack": "5.88.0",
"webpack-dev-server": "^4.9.3",
"webpack-merge": "^5.8.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('app', () => {
).toMatchSnapshot();
expect(tree.read('my-app/tsconfig.json', 'utf-8')).toMatchSnapshot();
const packageJson = readJson(tree, 'package.json');
expect(packageJson.devDependencies['vitest']).toEqual('^1.0.4');
expect(packageJson.devDependencies['vitest']).toEqual('^1.3.1');
});

it('should configure tsconfig and project.json correctly', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/vite/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@
}
},
"packageJsonUpdates": {
"18.1.0": {
"version": "18.1.0-beta.1",
"packages": {
"vitest": {
"version": "^1.3.1",
"alwaysAddToPackageJson": false
},
"@vitest/coverage-v8": {
"version": "^1.3.1",
"alwaysAddToPackageJson": false
},
"@vitest/ui": {
"version": "^1.3.1",
"alwaysAddToPackageJson": false
},
"@vitest/coverage-istanbul": {
"version": "^1.3.1",
"alwaysAddToPackageJson": false
}
}
},
"17.3.0": {
"version": "17.3.0-beta.0",
"packages": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"peerDependencies": {
"vite": "^5.0.0",
"vitest": "^1.0.0"
"vitest": "^1.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
63 changes: 23 additions & 40 deletions packages/vite/src/executors/test/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
import { VitestExecutorOptions } from '../schema';
import { normalizeViteConfigFilePath } from '../../../utils/options-utils';
import { relative } from 'path';
import { loadViteDynamicImport } from '../../../utils/executor-utils';
import {
loadViteDynamicImport,
loadVitestDynamicImport,
} from '../../../utils/executor-utils';

export async function getOptions(
options: VitestExecutorOptions,
context: ExecutorContext,
projectRoot: string,
extraArgs: Record<string, any>
projectRoot: string
) {
// Allows ESM to be required in CJS modules. Vite will be published as ESM in the future.
const { loadConfigFromFile, mergeConfig } = await loadViteDynamicImport();
Expand All @@ -38,7 +40,7 @@ export async function getOptions(

const resolved = await loadConfigFromFile(
{
mode: extraArgs?.mode ?? 'production',
mode: options?.mode ?? 'production',
command: 'serve',
},
viteConfigPath
Expand All @@ -59,7 +61,12 @@ export async function getOptions(
? process.cwd()
: relative(context.cwd, joinPathFragments(context.root, projectRoot));

const normalizedExtraArgs = normalizeArgs(extraArgs);
const { parseCLI } = await loadVitestDynamicImport();

const normalizedExtraArgs = parseCLI([
'vitest',
...getOptionsAsArgv(options),
]);

const settings = {
...normalizedExtraArgs,
Expand All @@ -72,42 +79,18 @@ export async function getOptions(
return mergeConfig(resolved?.config?.['test'] ?? {}, settings);
}

export async function getExtraArgs(
options: VitestExecutorOptions
): Promise<Record<string, any>> {
// support passing extra args to vite cli
const extraArgs: Record<string, any> = {};
for (const key of Object.keys(options)) {
extraArgs[key] = options[key];
}

return extraArgs;
}

// normalizes some args that were previously normalized by `startVitest` until this is fixed
// https://github.com/vitest-dev/vitest/pull/5126/files#diff-49ef635be88fe607c8682e81ab56b061ba9aafd5c94a5690a70b90a54604cd24L40-L62
function normalizeArgs(extraArgs: Record<string, any>) {
const args = { ...extraArgs };
export function getOptionsAsArgv(obj: Record<string, any>): string[] {
const argv: string[] = [];

if (typeof args.coverage === 'boolean') {
args.coverage = { enabled: args.coverage };
}
// running "vitest --browser", assumes browser name is set in the config
if (typeof args.browser === 'boolean') {
args.browser = { enabled: args.browser } as any;
}
// running "vitest --browser=chrome"
if (typeof args.browser === 'string') {
args.browser = { enabled: true, name: args.browser };
}
if (typeof args.typecheck === 'boolean') {
args.typecheck = { enabled: true };
}
if (typeof args.typecheck?.only === 'boolean') {
args.typecheck ??= {};
args.typecheck.only = true;
args.typecheck.enabled = true;
for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value)) {
value.forEach((item) => argv.push(`--${key}=${item}`));
} else if (typeof value === 'object' && value !== null) {
argv.push(`--${key}='${JSON.stringify(value)}'`);
} else {
argv.push(`--${key}=${value}`);
}
}

return args;
return argv;
}
1 change: 1 addition & 0 deletions packages/vite/src/executors/test/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface VitestExecutorOptions {
reportsDirectory?: string;
testFiles?: string[];
watch?: boolean;
mode?: string;
}
4 changes: 4 additions & 0 deletions packages/vite/src/executors/test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"type": "string",
"description": "Directory to write coverage report to."
},
"mode": {
"type": "string",
"description": "Mode for Vite."
},
"testFiles": {
"aliases": ["testFile"],
"type": "array",
Expand Down
10 changes: 4 additions & 6 deletions packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { VitestExecutorOptions } from './schema';
import { resolve } from 'path';
import { registerTsConfigPaths } from '@nx/js/src/internal';
import { NxReporter } from './lib/nx-reporter';
import { getExtraArgs, getOptions } from './lib/utils';
import { getOptions } from './lib/utils';
import { loadVitestDynamicImport } from '../../utils/executor-utils';

export async function* vitestExecutor(
options: VitestExecutorOptions,
Expand All @@ -16,13 +17,10 @@ export async function* vitestExecutor(

process.env.VITE_CJS_IGNORE_WARNING = 'true';
// Allows ESM to be required in CJS modules. Vite will be published as ESM in the future.
const { startVitest } = await (Function(
'return import("vitest/node")'
)() as Promise<typeof import('vitest/node')>);
const { startVitest } = await loadVitestDynamicImport();

const extraArgs = await getExtraArgs(options);
const resolvedOptions =
(await getOptions(options, context, projectRoot, extraArgs)) ?? {};
(await getOptions(options, context, projectRoot)) ?? {};

const nxReporter = new NxReporter(resolvedOptions['watch']);
if (resolvedOptions['reporters'] === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ exports[`@nx/vite:init dependencies for package.json should add required package
"devDependencies": {
"@nx/vite": "0.0.1",
"@nx/web": "0.0.1",
"@vitest/ui": "^1.0.4",
"@vitest/ui": "^1.3.1",
"existing": "1.0.0",
"vite": "~5.0.0",
"vitest": "^1.0.4",
"vitest": "^1.3.1",
},
"name": "@proj/source",
}
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/utils/executor-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ export function createBuildableTsConfig(
export function loadViteDynamicImport() {
return Function('return import("vite")')() as Promise<typeof import('vite')>;
}

export function loadVitestDynamicImport() {
return Function('return import("vitest/node")')() as Promise<
typeof import('vitest/node')
>;
}
2 changes: 1 addition & 1 deletion packages/vite/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const nxVersion = require('../../package.json').version;
export const viteVersion = '~5.0.0';
export const vitestVersion = '^1.0.4';
export const vitestVersion = '^1.3.1';
export const vitePluginReactVersion = '^4.2.0';
export const vitePluginReactSwcVersion = '^3.5.0';
export const jsdomVersion = '~22.1.0';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ exports[`lib should add vue, vite and vitest to package.json 1`] = `
"@typescript-eslint/parser": "^6.13.2",
"@vitejs/plugin-vue": "^4.5.0",
"@vitest/coverage-v8": "^1.0.4",
"@vitest/ui": "^1.0.4",
"@vitest/ui": "^1.3.1",
"@vue/eslint-config-prettier": "7.1.0",
"@vue/eslint-config-typescript": "^11.0.3",
"@vue/test-utils": "^2.4.1",
Expand All @@ -197,7 +197,7 @@ exports[`lib should add vue, vite and vitest to package.json 1`] = `
"prettier": "^2.6.2",
"typescript": "~5.3.2",
"vite": "~5.0.0",
"vitest": "^1.0.4",
"vitest": "^1.3.1",
"vue-tsc": "^1.8.8",
},
"name": "@proj/source",
Expand Down