Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): convert-to-inferred generator should handle legacy cypress executor #23151

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Coly010 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ interface Schema {

export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();
await migrateExecutorToPlugin<PlaywrightPluginOptions>(
tree,
projectGraph,
'@nx/playwright:playwright',
'@nx/playwright/plugin',
(targetName) => ({ targetName, ciTargetName: 'e2e-ci' }),
postTargetTransformer,
createNodes,
options.project
);
const migratedProjects =
await migrateExecutorToPlugin<PlaywrightPluginOptions>(
tree,
projectGraph,
'@nx/playwright:playwright',
'@nx/playwright/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