cleanup(nx-plugin): migrate e2e executor to nx devkit (#6212)

This commit is contained in:
Leosvel Pérez Espinosa 2021-07-01 15:36:35 +01:00 committed by GitHub
parent 7fe757e8a8
commit 5ba6d01262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 113 additions and 68 deletions

View File

@ -1,6 +1,6 @@
# e2e # e2e
Creates and runs an e2e for a Nx Plugin Creates and runs the e2e tests for an Nx Plugin.
Properties can be configured in angular.json when defining the executor, or when invoking it. Properties can be configured in angular.json when defining the executor, or when invoking it.
@ -10,16 +10,18 @@ Properties can be configured in angular.json when defining the executor, or when
Type: `string` Type: `string`
Jest config file Jest config file.
### target ### target
Type: `string` Type: `string`
the target Nx Plugin project and build The build target for the Nx Plugin project.
### ~~tsSpecConfig~~ ### ~~tsSpecConfig~~
Type: `string` Type: `string`
**Deprecated:** Spec tsconfig file **Deprecated:** Use the `tsconfig` property for `ts-jest` in the e2e project `jest.config.js` file. It will be removed in the next major release.
The tsconfig file for specs.

View File

@ -1,6 +1,6 @@
# e2e # e2e
Creates and runs an e2e for a Nx Plugin Creates and runs the e2e tests for an Nx Plugin.
Properties can be configured in workspace.json when defining the executor, or when invoking it. Properties can be configured in workspace.json when defining the executor, or when invoking it.
Read more about how to use executors and the CLI here: https://nx.dev/node/getting-started/nx-cli#running-tasks. Read more about how to use executors and the CLI here: https://nx.dev/node/getting-started/nx-cli#running-tasks.
@ -11,16 +11,18 @@ Read more about how to use executors and the CLI here: https://nx.dev/node/getti
Type: `string` Type: `string`
Jest config file Jest config file.
### target ### target
Type: `string` Type: `string`
the target Nx Plugin project and build The build target for the Nx Plugin project.
### ~~tsSpecConfig~~ ### ~~tsSpecConfig~~
Type: `string` Type: `string`
**Deprecated:** Spec tsconfig file **Deprecated:** Use the `tsconfig` property for `ts-jest` in the e2e project `jest.config.js` file. It will be removed in the next major release.
The tsconfig file for specs.

View File

@ -1,6 +1,6 @@
# e2e # e2e
Creates and runs an e2e for a Nx Plugin Creates and runs the e2e tests for an Nx Plugin.
Properties can be configured in workspace.json when defining the executor, or when invoking it. Properties can be configured in workspace.json when defining the executor, or when invoking it.
Read more about how to use executors and the CLI here: https://nx.dev/react/getting-started/nx-cli#running-tasks. Read more about how to use executors and the CLI here: https://nx.dev/react/getting-started/nx-cli#running-tasks.
@ -11,16 +11,18 @@ Read more about how to use executors and the CLI here: https://nx.dev/react/gett
Type: `string` Type: `string`
Jest config file Jest config file.
### target ### target
Type: `string` Type: `string`
the target Nx Plugin project and build The build target for the Nx Plugin project.
### ~~tsSpecConfig~~ ### ~~tsSpecConfig~~
Type: `string` Type: `string`
**Deprecated:** Spec tsconfig file **Deprecated:** Use the `tsconfig` property for `ts-jest` in the e2e project `jest.config.js` file. It will be removed in the next major release.
The tsconfig file for specs.

View File

@ -14,6 +14,19 @@
{ {
"files": ["*.js", "*.jsx"], "files": ["*.js", "*.jsx"],
"rules": {} "rules": {}
},
{
"files": ["**/*.ts"],
"excludedFiles": ["./src/migrations/**"],
"rules": {
"no-restricted-imports": [
"error",
"@angular-devkit/architect",
"@angular-devkit/core",
"@angular-devkit/schematics",
"@nrwl/workspace"
]
}
} }
] ]
} }

View File

@ -1,9 +0,0 @@
{
"builders": {
"e2e": {
"implementation": "./src/executors/e2e/e2e.impl",
"schema": "./src/executors/e2e/schema.json",
"description": "Creates and runs an e2e for a Nx Plugin"
}
}
}

View File

@ -0,0 +1,16 @@
{
"builders": {
"e2e": {
"implementation": "./src/executors/e2e/e2e.impl#nxPluginE2EBuilder",
"schema": "./src/executors/e2e/schema.json",
"description": "Creates and runs the e2e tests for an Nx Plugin."
}
},
"executors": {
"e2e": {
"implementation": "./src/executors/e2e/e2e.impl",
"schema": "./src/executors/e2e/schema.json",
"description": "Creates and runs the e2e tests for an Nx Plugin."
}
}
}

View File

@ -20,15 +20,12 @@
}, },
"homepage": "https://nx.dev", "homepage": "https://nx.dev",
"schematics": "./collection.json", "schematics": "./collection.json",
"builders": "./builders.json", "builders": "./executors.json",
"ng-update": { "ng-update": {
"requirements": {}, "requirements": {},
"migrations": "./migrations.json" "migrations": "./migrations.json"
}, },
"dependencies": { "dependencies": {
"@angular-devkit/architect": "^0.1200.0",
"@angular-devkit/core": "^12.0.0",
"@angular-devkit/schematics": "^12.0.0",
"@nrwl/devkit": "*", "@nrwl/devkit": "*",
"@nrwl/jest": "*", "@nrwl/jest": "*",
"@nrwl/linter": "*", "@nrwl/linter": "*",

View File

@ -1,38 +1,67 @@
import type { ExecutorContext } from '@nrwl/devkit';
import { import {
BuilderContext, convertNxExecutor,
createBuilder, logger,
scheduleTargetAndForget, parseTargetString,
targetFromTargetString, readTargetOptions,
} from '@angular-devkit/architect'; runExecutor,
import { from } from 'rxjs'; } from '@nrwl/devkit';
import { concatMap, switchMap } from 'rxjs/operators'; import { jestExecutor } from '@nrwl/jest/src/executors/jest/jest.impl';
import { Schema } from './schema'; import type { NxPluginE2EExecutorOptions } from './schema';
try { try {
require('dotenv').config(); require('dotenv').config();
// eslint-disable-next-line no-empty // eslint-disable-next-line no-empty
} catch (e) {} } catch (e) {}
export type NxPluginE2EBuilderOptions = Schema; export async function* nxPluginE2EExecutor(
options: NxPluginE2EExecutorOptions,
function buildTarget(context: BuilderContext, target: string) { context: ExecutorContext
return scheduleTargetAndForget(context, targetFromTargetString(target)); ): AsyncGenerator<{ success: boolean }> {
let success: boolean;
for await (const _ of runBuildTarget(options.target, context)) {
try {
success = await runTests(options.jestConfig, context);
} catch (e) {
logger.error(e.message);
success = false;
}
} }
export function runNxPluginE2EBuilder( return { success };
options: NxPluginE2EBuilderOptions, }
context: BuilderContext
) { async function* runBuildTarget(
return buildTarget(context, options.target).pipe( buildTarget: string,
switchMap(() => { context: ExecutorContext
return from( ): AsyncGenerator<boolean> {
context.scheduleBuilder('@nrwl/jest:jest', { const { project, target, configuration } = parseTargetString(buildTarget);
jestConfig: options.jestConfig, const buildTargetOptions = readTargetOptions(
watch: false, { project, target, configuration },
}) context
).pipe(concatMap((run) => run.output));
})
); );
const targetSupportsWatch = Object.keys(buildTargetOptions).includes('watch');
for await (const output of await runExecutor<{ success: boolean }>(
{ project, target, configuration },
targetSupportsWatch ? { watch: false } : {},
context
)) {
if (!output.success)
throw new Error('Could not compile application files.');
yield output.success;
}
} }
export default createBuilder(runNxPluginE2EBuilder); async function runTests(
jestConfig: string,
context: ExecutorContext
): Promise<boolean> {
const { success } = await jestExecutor({ jestConfig, watch: false }, context);
return success;
}
export default nxPluginE2EExecutor;
export const nxPluginE2EBuilder = convertNxExecutor(nxPluginE2EExecutor);

View File

@ -1,6 +1,4 @@
import { JsonObject } from '@angular-devkit/core'; export interface NxPluginE2EExecutorOptions {
export interface Schema extends JsonObject {
target: string; target: string;
jestConfig: string; jestConfig: string;
} }

View File

@ -1,21 +1,23 @@
{ {
"title": "Nx Plugin Playground Target", "title": "Nx Plugin Playground Target",
"description": "Creates a playground for a Nx Plugin", "description": "Creates a playground for a Nx Plugin",
"cli": "nx",
"type": "object", "type": "object",
"properties": { "properties": {
"target": { "target": {
"type": "string", "description": "The build target for the Nx Plugin project.",
"description": "the target Nx Plugin project and build" "type": "string"
}, },
"jestConfig": { "jestConfig": {
"type": "string", "type": "string",
"description": "Jest config file" "description": "Jest config file."
}, },
"tsSpecConfig": { "tsSpecConfig": {
"type": "string", "type": "string",
"description": "Spec tsconfig file", "description": "The tsconfig file for specs.",
"x-deprecated": true "x-deprecated": "Use the `tsconfig` property for `ts-jest` in the e2e project `jest.config.js` file. It will be removed in the next major release."
} }
}, },
"additionalProperties": false,
"required": ["target", "jestConfig"] "required": ["target", "jestConfig"]
} }

View File

@ -99,11 +99,7 @@ describe('NxPlugin e2e-project Generator', () => {
expect(project.targets.e2e).toBeTruthy(); expect(project.targets.e2e).toBeTruthy();
expect(project.targets.e2e).toMatchObject({ expect(project.targets.e2e).toMatchObject({
executor: '@nrwl/nx-plugin:e2e', executor: '@nrwl/nx-plugin:e2e',
options: expect.objectContaining({ options: expect.objectContaining({ target: 'my-plugin:build' }),
target: 'my-plugin:build',
npmPackageName: '@proj/my-plugin',
pluginOutputPath: 'dist/libs/my-plugin',
}),
}); });
}); });

View File

@ -66,11 +66,7 @@ function updateWorkspaceConfiguration(host: Tree, options: NormalizedSchema) {
targets: { targets: {
e2e: { e2e: {
executor: '@nrwl/nx-plugin:e2e', executor: '@nrwl/nx-plugin:e2e',
options: { options: { target: `${options.pluginName}:build` },
target: `${options.pluginName}:build`,
npmPackageName: options.npmPackageName,
pluginOutputPath: options.pluginOutputPath,
},
}, },
}, },
tags: [], tags: [],

View File

@ -30,6 +30,7 @@
"@nrwl/express": ["./packages/express"], "@nrwl/express": ["./packages/express"],
"@nrwl/gatsby": ["./packages/gatsby"], "@nrwl/gatsby": ["./packages/gatsby"],
"@nrwl/jest": ["./packages/jest"], "@nrwl/jest": ["./packages/jest"],
"@nrwl/jest/*": ["./packages/jest/*"],
"@nrwl/linter": ["./packages/linter"], "@nrwl/linter": ["./packages/linter"],
"@nrwl/nest": ["./packages/nest"], "@nrwl/nest": ["./packages/nest"],
"@nrwl/next": ["./packages/next"], "@nrwl/next": ["./packages/next"],