Skip to content

Commit

Permalink
feat(nx-plugin): added support for vitest test runner (#29140)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Nx Plugin do not have the option to use Vitest as test runner

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
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 #29139 is fixed

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #22882
  • Loading branch information
gperdomor authored Dec 20, 2024
1 parent 2143ea5 commit 3a164a2
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .verdaccio/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ auth:
htpasswd:
file: ./htpasswd

max_body_size: 15mb

# a list of other known repositories we can talk to
uplinks:
npmjs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["none", "jest"],
"enum": ["none", "jest", "vitest"],
"description": "Test runner to use for unit tests."
},
"linter": {
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/plugin/generators/executor.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"enum": ["jest", "vitest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/plugin/generators/generator.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"enum": ["jest", "vitest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/plugin/generators/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/create-package/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/create-package/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["none", "jest"],
"enum": ["none", "jest", "vitest"],
"description": "Test runner to use for unit tests."
},
"linter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/executor/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/executor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"enum": ["jest", "vitest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/generator/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface Schema {
path: string;
name?: string;
description?: string;
unitTestRunner: 'jest' | 'none';
unitTestRunner: 'jest' | 'vitest' | 'none';
skipLintChecks?: boolean;
skipFormat?: boolean;
}
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/generator/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"enum": ["jest", "vitest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
Expand Down
52 changes: 52 additions & 0 deletions packages/plugin/src/generators/plugin/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/generators/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/plugin/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/plugin/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +35,7 @@ export async function normalizeOptions(
const unitTestRunner = await normalizeUnitTestRunnerOption(
host,
options.unitTestRunner,
['jest']
['jest', 'vitest']
);

const isTsSolutionSetup = isUsingTsSolutionSetup(host);
Expand Down

0 comments on commit 3a164a2

Please sign in to comment.