From 3a164a22de1170622e5901d0aea25be287b0d206 Mon Sep 17 00:00:00 2001 From: Gustavo Perdomo Date: Fri, 20 Dec 2024 10:53:29 -0300 Subject: [PATCH] feat(nx-plugin): added support for vitest test runner (#29140) ## Current Behavior Nx Plugin do not have the option to use Vitest as test runner ## Expected Behavior This PR allow us to use Vitest as test runner in plugins projects. Vitest for e2e targets is still missing and a new PR will be raised after https://github.com/nrwl/nx/issues/29139 is fixed ## Related Issue(s) Fixes #22882 --- .verdaccio/config.yml | 2 + .../plugin/generators/create-package.json | 2 +- .../packages/plugin/generators/executor.json | 2 +- .../packages/plugin/generators/generator.json | 2 +- .../packages/plugin/generators/plugin.json | 2 +- .../src/generators/create-package/schema.d.ts | 2 +- .../src/generators/create-package/schema.json | 2 +- .../create-package/utils/normalize-schema.ts | 2 +- .../src/generators/executor/schema.d.ts | 2 +- .../src/generators/executor/schema.json | 2 +- .../src/generators/generator/schema.d.ts | 2 +- .../src/generators/generator/schema.json | 2 +- .../src/generators/plugin/plugin.spec.ts | 52 +++++++++++++++++++ .../plugin/src/generators/plugin/plugin.ts | 3 +- .../plugin/src/generators/plugin/schema.d.ts | 2 +- .../plugin/src/generators/plugin/schema.json | 2 +- .../plugin/utils/normalize-schema.ts | 4 +- 17 files changed, 71 insertions(+), 16 deletions(-) diff --git a/.verdaccio/config.yml b/.verdaccio/config.yml index 1293b915662c4..5dec1a2695fb6 100644 --- a/.verdaccio/config.yml +++ b/.verdaccio/config.yml @@ -5,6 +5,8 @@ auth: htpasswd: file: ./htpasswd +max_body_size: 15mb + # a list of other known repositories we can talk to uplinks: npmjs: diff --git a/docs/generated/packages/plugin/generators/create-package.json b/docs/generated/packages/plugin/generators/create-package.json index e2ecc9d1f9c12..e6506f6a06fcd 100644 --- a/docs/generated/packages/plugin/generators/create-package.json +++ b/docs/generated/packages/plugin/generators/create-package.json @@ -31,7 +31,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["none", "jest"], + "enum": ["none", "jest", "vitest"], "description": "Test runner to use for unit tests." }, "linter": { diff --git a/docs/generated/packages/plugin/generators/executor.json b/docs/generated/packages/plugin/generators/executor.json index fbab6e9ae2380..21fe476852dfb 100644 --- a/docs/generated/packages/plugin/generators/executor.json +++ b/docs/generated/packages/plugin/generators/executor.json @@ -28,7 +28,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["jest", "none"], + "enum": ["jest", "vitest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" }, diff --git a/docs/generated/packages/plugin/generators/generator.json b/docs/generated/packages/plugin/generators/generator.json index 6f22046feeef9..5613c5eaf9b18 100644 --- a/docs/generated/packages/plugin/generators/generator.json +++ b/docs/generated/packages/plugin/generators/generator.json @@ -41,7 +41,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["jest", "none"], + "enum": ["jest", "vitest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" }, diff --git a/docs/generated/packages/plugin/generators/plugin.json b/docs/generated/packages/plugin/generators/plugin.json index e8c594a6b4bdc..3f7fab0890aa6 100644 --- a/docs/generated/packages/plugin/generators/plugin.json +++ b/docs/generated/packages/plugin/generators/plugin.json @@ -40,7 +40,7 @@ "unitTestRunner": { "description": "Test runner to use for unit tests.", "type": "string", - "enum": ["none", "jest"], + "enum": ["none", "jest", "vitest"], "x-priority": "important" }, "tags": { diff --git a/packages/plugin/src/generators/create-package/schema.d.ts b/packages/plugin/src/generators/create-package/schema.d.ts index 925036d54140c..f42d573df4881 100644 --- a/packages/plugin/src/generators/create-package/schema.d.ts +++ b/packages/plugin/src/generators/create-package/schema.d.ts @@ -8,7 +8,7 @@ export interface CreatePackageSchema { // options to create cli package, passed to js library generator skipFormat?: boolean; tags?: string; - unitTestRunner?: 'jest' | 'none'; + unitTestRunner?: 'jest' | 'vitest' | 'none'; linter?: Linter | LinterType; compiler?: 'swc' | 'tsc'; diff --git a/packages/plugin/src/generators/create-package/schema.json b/packages/plugin/src/generators/create-package/schema.json index cdfaf44afa959..8a9cdc52203c5 100644 --- a/packages/plugin/src/generators/create-package/schema.json +++ b/packages/plugin/src/generators/create-package/schema.json @@ -33,7 +33,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["none", "jest"], + "enum": ["none", "jest", "vitest"], "description": "Test runner to use for unit tests." }, "linter": { diff --git a/packages/plugin/src/generators/create-package/utils/normalize-schema.ts b/packages/plugin/src/generators/create-package/utils/normalize-schema.ts index 85f28a3aa1dcf..b9f2961b5f1fa 100644 --- a/packages/plugin/src/generators/create-package/utils/normalize-schema.ts +++ b/packages/plugin/src/generators/create-package/utils/normalize-schema.ts @@ -12,7 +12,7 @@ export interface NormalizedSchema extends CreatePackageSchema { bundler: 'swc' | 'tsc'; projectName: string; projectRoot: string; - unitTestRunner: 'jest' | 'none'; + unitTestRunner: 'jest' | 'vitest' | 'none'; linter: LinterType; useProjectJson: boolean; addPlugin: boolean; diff --git a/packages/plugin/src/generators/executor/schema.d.ts b/packages/plugin/src/generators/executor/schema.d.ts index b329653a3970d..ff6781ece96ae 100644 --- a/packages/plugin/src/generators/executor/schema.d.ts +++ b/packages/plugin/src/generators/executor/schema.d.ts @@ -2,7 +2,7 @@ export interface Schema { path: string; name?: string; description?: string; - unitTestRunner: 'jest' | 'none'; + unitTestRunner: 'jest' | 'vitest' | 'none'; includeHasher: boolean; skipLintChecks?: boolean; skipFormat?: boolean; diff --git a/packages/plugin/src/generators/executor/schema.json b/packages/plugin/src/generators/executor/schema.json index f9c513abb5b63..3272959bf769a 100644 --- a/packages/plugin/src/generators/executor/schema.json +++ b/packages/plugin/src/generators/executor/schema.json @@ -28,7 +28,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["jest", "none"], + "enum": ["jest", "vitest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" }, diff --git a/packages/plugin/src/generators/generator/schema.d.ts b/packages/plugin/src/generators/generator/schema.d.ts index b8b4e3fbf9ecd..4e284303c83fc 100644 --- a/packages/plugin/src/generators/generator/schema.d.ts +++ b/packages/plugin/src/generators/generator/schema.d.ts @@ -2,7 +2,7 @@ export interface Schema { path: string; name?: string; description?: string; - unitTestRunner: 'jest' | 'none'; + unitTestRunner: 'jest' | 'vitest' | 'none'; skipLintChecks?: boolean; skipFormat?: boolean; } diff --git a/packages/plugin/src/generators/generator/schema.json b/packages/plugin/src/generators/generator/schema.json index 9c94dbb5f0f0d..f488ac2c34eb8 100644 --- a/packages/plugin/src/generators/generator/schema.json +++ b/packages/plugin/src/generators/generator/schema.json @@ -41,7 +41,7 @@ }, "unitTestRunner": { "type": "string", - "enum": ["jest", "none"], + "enum": ["jest", "vitest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" }, diff --git a/packages/plugin/src/generators/plugin/plugin.spec.ts b/packages/plugin/src/generators/plugin/plugin.spec.ts index 82f66a7ae6db7..6b36c8b1831eb 100644 --- a/packages/plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/plugin/src/generators/plugin/plugin.spec.ts @@ -172,11 +172,63 @@ describe('NxPlugin Plugin Generator', () => { expect(tree.exists(path)).toBeFalsy() ); + ['my-plugin/vite.config.ts'].forEach((path) => + expect(tree.exists(path)).toBeFalsy() + ); + expect( readProjectConfiguration(tree, 'my-plugin').targets.test ).not.toBeDefined(); }); }); + + describe('jest', () => { + it('should generate test files with jest.config.ts', async () => { + await pluginGenerator( + tree, + getSchema({ + directory: 'my-plugin', + unitTestRunner: 'jest', + }) + ); + + ['my-plugin/jest.config.ts'].forEach((path) => + expect(tree.exists(path)).toBeTruthy() + ); + + const projectTargets = readProjectConfiguration( + tree, + 'my-plugin' + ).targets; + + expect(projectTargets.test).toBeDefined(); + expect(projectTargets.test?.executor).toEqual('@nx/jest:jest'); + }); + }); + + describe('vitest', () => { + it('should generate test files with vite.config.ts', async () => { + await pluginGenerator( + tree, + getSchema({ + directory: 'my-plugin', + unitTestRunner: 'vitest', + }) + ); + + ['my-plugin/vite.config.ts'].forEach((path) => + expect(tree.exists(path)).toBeTruthy() + ); + + const projectTargets = readProjectConfiguration( + tree, + 'my-plugin' + ).targets; + + expect(projectTargets.test).toBeDefined(); + expect(projectTargets.test?.executor).toEqual('@nx/vite:test'); + }); + }); }); describe('--compiler', () => { diff --git a/packages/plugin/src/generators/plugin/plugin.ts b/packages/plugin/src/generators/plugin/plugin.ts index 5d7e0483017ee..d73d2b289a932 100644 --- a/packages/plugin/src/generators/plugin/plugin.ts +++ b/packages/plugin/src/generators/plugin/plugin.ts @@ -118,7 +118,8 @@ export async function pluginGeneratorInternal(host: Tree, schema: Schema) { '@nx/devkit': nxVersion, }, { - '@nx/jest': nxVersion, + [options.unitTestRunner === 'vitest' ? '@nx/vite' : '@nx/jest']: + nxVersion, '@nx/js': nxVersion, '@nx/plugin': nxVersion, } diff --git a/packages/plugin/src/generators/plugin/schema.d.ts b/packages/plugin/src/generators/plugin/schema.d.ts index f6e512c866675..082eb02cfa631 100644 --- a/packages/plugin/src/generators/plugin/schema.d.ts +++ b/packages/plugin/src/generators/plugin/schema.d.ts @@ -9,7 +9,7 @@ export interface Schema { skipLintChecks?: boolean; // default is false e2eTestRunner?: 'jest' | 'none'; tags?: string; - unitTestRunner?: 'jest' | 'none'; + unitTestRunner?: 'jest' | 'vitest' | 'none'; linter?: Linter | LinterType; setParserOptionsProject?: boolean; compiler?: 'swc' | 'tsc'; diff --git a/packages/plugin/src/generators/plugin/schema.json b/packages/plugin/src/generators/plugin/schema.json index 34469240b14d1..7bd9253090bb9 100644 --- a/packages/plugin/src/generators/plugin/schema.json +++ b/packages/plugin/src/generators/plugin/schema.json @@ -40,7 +40,7 @@ "unitTestRunner": { "description": "Test runner to use for unit tests.", "type": "string", - "enum": ["none", "jest"], + "enum": ["none", "jest", "vitest"], "x-priority": "important" }, "tags": { diff --git a/packages/plugin/src/generators/plugin/utils/normalize-schema.ts b/packages/plugin/src/generators/plugin/utils/normalize-schema.ts index 632ec818a13c3..8cc585b253056 100644 --- a/packages/plugin/src/generators/plugin/utils/normalize-schema.ts +++ b/packages/plugin/src/generators/plugin/utils/normalize-schema.ts @@ -20,7 +20,7 @@ export interface NormalizedSchema extends Schema { npmPackageName: string; bundler: 'swc' | 'tsc'; publishable: boolean; - unitTestRunner: 'jest' | 'none'; + unitTestRunner: 'jest' | 'vitest' | 'none'; linter: LinterType; useProjectJson: boolean; addPlugin: boolean; @@ -35,7 +35,7 @@ export async function normalizeOptions( const unitTestRunner = await normalizeUnitTestRunnerOption( host, options.unitTestRunner, - ['jest'] + ['jest', 'vitest'] ); const isTsSolutionSetup = isUsingTsSolutionSetup(host);