Skip to content

Commit

Permalink
fix(testing): convert-to-inferred generator should handle legacy cypr…
Browse files Browse the repository at this point in the history
…ess executor
  • Loading branch information
Coly010 committed May 2, 2024
1 parent bacdc79 commit 85c641b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ interface CreateCypressTestProjectOptions {
appName: string;
appRoot: string;
e2eTargetName: string;
legacyExecutor?: boolean;
}

const defaultCreateCypressTestProjectOptions: CreateCypressTestProjectOptions =
{
appName: 'myapp-e2e',
appRoot: 'myapp-e2e',
e2eTargetName: 'e2e',
legacyExecutor: false,
};

function createTestProject(
Expand All @@ -111,7 +113,9 @@ function createTestProject(
projectType: 'application',
targets: {
[projectOpts.e2eTargetName]: {
executor: '@nx/cypress:cypress',
executor: projectOpts.legacyExecutor
? '@nrwl/cypress:cypress'
: '@nx/cypress:cypress',
options: {
cypressConfig: `${projectOpts.appRoot}/cypress.config.ts`,
testingType: `e2e`,
Expand Down Expand Up @@ -217,7 +221,7 @@ describe('Cypress - Convert Executors To Plugin', () => {
// project.json modifications
const updatedProject = readProjectConfiguration(tree, project.name);
const targetKeys = Object.keys(updatedProject.targets);
['test'].forEach((key) => expect(targetKeys).not.toContain(key));
expect(targetKeys).not.toContain('test');

// nx.json modifications
const nxJsonPlugins = readNxJson(tree).plugins;
Expand Down Expand Up @@ -305,6 +309,42 @@ describe('Cypress - Convert Executors To Plugin', () => {
}
});

it('should setup handle legacy executor', async () => {
// ARRANGE
const project = createTestProject(tree, {
e2eTargetName: 'test',
legacyExecutor: true,
});

// ACT
await convertToInferred(tree, { skipFormat: true });

// ASSERT
// project.json modifications
const updatedProject = readProjectConfiguration(tree, project.name);
const targetKeys = Object.keys(updatedProject.targets);
['test'].forEach((key) => expect(targetKeys).not.toContain(key));

// nx.json modifications
const nxJsonPlugins = readNxJson(tree).plugins;
const hasCypressPlugin = nxJsonPlugins.find((plugin) =>
typeof plugin === 'string'
? plugin === '@nx/cypress/plugin'
: plugin.plugin === '@nx/cypress/plugin'
);
expect(hasCypressPlugin).toBeTruthy();
if (typeof hasCypressPlugin !== 'string') {
[
['targetName', 'test'],
['ciTargetName', 'e2e-ci'],
].forEach(([targetOptionName, targetName]) => {
expect(hasCypressPlugin.options[targetOptionName]).toEqual(
targetName
);
});
}
});

it('should setup a new Cypress plugin to match only projects migrated', async () => {
// ARRANGE
const existingProject = createTestProject(tree, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Schema {

export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();
await migrateExecutorToPlugin(
let migratedProjects = await migrateExecutorToPlugin(
tree,
projectGraph,
'@nx/cypress:cypress',
Expand All @@ -35,6 +35,24 @@ export async function convertToInferred(tree: Tree, options: Schema) {
options.project
);

migratedProjects += await migrateExecutorToPlugin(
tree,
projectGraph,
'@nrwl/cypress:cypress',
'@nx/cypress/plugin',
(targetName) => ({
targetName,
ciTargetName: 'e2e-ci',
}),
postTargetTransformer,
createNodes,
options.project
);

if (migratedProjects === 0) {
throw new Error('Could not find any targets to migrate.');
}

if (!options.skipFormat) {
await formatFiles(tree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ class ExecutorToPluginMigrator<T> {
this.#skipTargetFilter = skipTargetFilter ?? ((...args) => [false, '']);
}

async run(): Promise<void> {
async run(): Promise<Map<string, Set<string>>> {
await this.#init();
for (const targetName of this.#targetAndProjectsToMigrate.keys()) {
this.#migrateTarget(targetName);
if (this.#targetAndProjectsToMigrate.size > 0) {
for (const targetName of this.#targetAndProjectsToMigrate.keys()) {
this.#migrateTarget(targetName);
}
this.#addPlugins();
}
this.#addPlugins();
return this.#targetAndProjectsToMigrate;
}

async #init() {
Expand Down Expand Up @@ -239,17 +242,6 @@ class ExecutorToPluginMigrator<T> {
}
}
);

if (this.#targetAndProjectsToMigrate.size === 0) {
const errorMsg = this.#specificProjectToMigrate
? `Project "${
this.#specificProjectToMigrate
}" does not contain any targets using the "${
this.#executor
}" executor. Please select a project that does.`
: `Could not find any targets using the "${this.#executor}" executor.`;
throw new Error(errorMsg);
}
}

#getTargetDefaultsForExecutor() {
Expand All @@ -270,6 +262,10 @@ class ExecutorToPluginMigrator<T> {
}

async #getCreateNodesResults() {
if (this.#targetAndProjectsToMigrate.size === 0) {
return;
}

for (const targetName of this.#targetAndProjectsToMigrate.keys()) {
const loadedPlugin = new LoadedNxPlugin(
{
Expand Down Expand Up @@ -312,7 +308,7 @@ export async function migrateExecutorToPlugin<T>(
createNodes: CreateNodes<T>,
specificProjectToMigrate?: string,
skipTargetFilter?: SkipTargetFilter
): Promise<void> {
): Promise<number> {
const migrator = new ExecutorToPluginMigrator<T>(
tree,
projectGraph,
Expand All @@ -324,5 +320,6 @@ export async function migrateExecutorToPlugin<T>(
specificProjectToMigrate,
skipTargetFilter
);
await migrator.run();
const migratedProjectsAndTargets = await migrator.run();
return migratedProjectsAndTargets.size;
}

0 comments on commit 85c641b

Please sign in to comment.