Skip to content

Commit

Permalink
feat(testing): add convert-to-inferred migration generator for jest
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jun 17, 2024
1 parent 4456526 commit 889aff3
Show file tree
Hide file tree
Showing 11 changed files with 1,541 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -7749,6 +7749,14 @@
"children": [],
"isExternal": false,
"disableCollapsible": false
},
{
"id": "convert-to-inferred",
"path": "/nx-api/jest/generators/convert-to-inferred",
"name": "convert-to-inferred",
"children": [],
"isExternal": false,
"disableCollapsible": false
}
],
"isExternal": false,
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/manifests/nx-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,15 @@
"originalFilePath": "/packages/jest/src/generators/configuration/schema.json",
"path": "/nx-api/jest/generators/configuration",
"type": "generator"
},
"/nx-api/jest/generators/convert-to-inferred": {
"description": "Convert existing Jest project(s) using `@nx/jest:jest` executor to use `@nx/jest/plugin`.",
"file": "generated/packages/jest/generators/convert-to-inferred.json",
"hidden": false,
"name": "convert-to-inferred",
"originalFilePath": "/packages/jest/src/generators/convert-to-inferred/schema.json",
"path": "/nx-api/jest/generators/convert-to-inferred",
"type": "generator"
}
},
"path": "/nx-api/jest"
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,15 @@
"originalFilePath": "/packages/jest/src/generators/configuration/schema.json",
"path": "jest/generators/configuration",
"type": "generator"
},
{
"description": "Convert existing Jest project(s) using `@nx/jest:jest` executor to use `@nx/jest/plugin`.",
"file": "generated/packages/jest/generators/convert-to-inferred.json",
"hidden": false,
"name": "convert-to-inferred",
"originalFilePath": "/packages/jest/src/generators/convert-to-inferred/schema.json",
"path": "jest/generators/convert-to-inferred",
"type": "generator"
}
],
"githubRoot": "https://github.com/nrwl/nx/blob/master",
Expand Down
30 changes: 30 additions & 0 deletions docs/generated/packages/jest/generators/convert-to-inferred.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "convert-to-inferred",
"factory": "./src/generators/convert-to-inferred/convert-to-inferred",
"schema": {
"$schema": "https://json-schema.org/schema",
"$id": "NxJestConvertToInferred",
"description": "Convert existing Jest project(s) using `@nx/jest:jest` executor to use `@nx/jest/plugin`.",
"title": "Convert Jest project from executor to plugin",
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "The project to convert from using the `@nx/jest:jest` executor to use `@nx/jest/plugin`. If not provided, all projects using the `@nx/jest:jest` executor will be converted.",
"x-priority": "important"
},
"skipFormat": {
"type": "boolean",
"description": "Whether to format files.",
"default": false
}
},
"presets": []
},
"description": "Convert existing Jest project(s) using `@nx/jest:jest` executor to use `@nx/jest/plugin`.",
"implementation": "/packages/jest/src/generators/convert-to-inferred/convert-to-inferred.ts",
"aliases": [],
"hidden": false,
"path": "/packages/jest/src/generators/convert-to-inferred/schema.json",
"type": "generator"
}
1 change: 1 addition & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@
- [generators](/nx-api/jest/generators)
- [init](/nx-api/jest/generators/init)
- [configuration](/nx-api/jest/generators/configuration)
- [convert-to-inferred](/nx-api/jest/generators/convert-to-inferred)
- [js](/nx-api/js)
- [documents](/nx-api/js/documents)
- [Overview](/nx-api/js/documents/overview)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PostTargetTransformer = (
tree: Tree,
projectDetails: { projectName: string; root: string },
inferredTargetConfiguration: TargetConfiguration
) => TargetConfiguration;
) => TargetConfiguration | Promise<TargetConfiguration>;
type SkipTargetFilter = (
targetConfiguration: TargetConfiguration
) => [boolean, string];
Expand Down Expand Up @@ -85,7 +85,7 @@ class ExecutorToPluginMigrator<T> {
await this.#init();
if (this.#targetAndProjectsToMigrate.size > 0) {
for (const targetName of this.#targetAndProjectsToMigrate.keys()) {
this.#migrateTarget(targetName);
await this.#migrateTarget(targetName);
}
await this.#addPlugins();
}
Expand All @@ -105,12 +105,12 @@ class ExecutorToPluginMigrator<T> {
await this.#getCreateNodesResults();
}

#migrateTarget(targetName: string) {
async #migrateTarget(targetName: string) {
const include: string[] = [];
for (const projectName of this.#targetAndProjectsToMigrate.get(
targetName
)) {
include.push(this.#migrateProject(projectName, targetName));
include.push(await this.#migrateProject(projectName, targetName));
}

this.#pluginToAddForTarget.set(targetName, {
Expand All @@ -120,7 +120,7 @@ class ExecutorToPluginMigrator<T> {
});
}

#migrateProject(projectName: string, targetName: string) {
async #migrateProject(projectName: string, targetName: string) {
const projectFromGraph = this.#projectGraph.nodes[projectName];
const projectConfig = readProjectConfiguration(this.tree, projectName);

Expand All @@ -141,7 +141,7 @@ class ExecutorToPluginMigrator<T> {
this.#mergeInputs(projectTarget, createdTarget);
}

projectTarget = this.#postTargetTransformer(
projectTarget = await this.#postTargetTransformer(
projectTarget,
this.tree,
{ projectName, root: projectFromGraph.data.root },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TargetConfiguration } from 'nx/src/devkit-exports';
import { relative, resolve } from 'node:path/posix';
import { workspaceRoot, type TargetConfiguration } from 'nx/src/devkit-exports';
import { interpolate } from 'nx/src/devkit-internals';

/**
Expand Down Expand Up @@ -128,6 +129,24 @@ export function processTargetOutputs(
target.outputs = targetOutputs;
}

export function toProjectRelativePath(
path: string,
projectRoot: string
): string {
if (projectRoot === '.') {
// workspace and project root are the same, we add a leading './' which is
// required by some tools (e.g. Jest)
return path.startsWith('.') ? path : `./${path}`;
}

const relativePath = relative(
resolve(workspaceRoot, projectRoot),
resolve(workspaceRoot, path)
);

return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
}

function updateOutputRenamingOption(
output: string,
option: string,
Expand Down
5 changes: 5 additions & 0 deletions packages/jest/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"schema": "./src/generators/configuration/schema.json",
"description": "Add Jest configuration to a project.",
"hidden": true
},
"convert-to-inferred": {
"factory": "./src/generators/convert-to-inferred/convert-to-inferred",
"schema": "./src/generators/convert-to-inferred/schema.json",
"description": "Convert existing Jest project(s) using `@nx/jest:jest` executor to use `@nx/jest/plugin`."
}
}
}
Loading

0 comments on commit 889aff3

Please sign in to comment.