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 13ac3bb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 15 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 @@ -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,19 +21,68 @@ interface Schema {

export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();
await migrateExecutorToPlugin(
tree,
projectGraph,
'@nx/cypress:cypress',
'@nx/cypress/plugin',
(targetName) => ({
targetName,
ciTargetName: 'e2e-ci',
}),
postTargetTransformer,
createNodes,
options.project
);
let projectIsUsingLegacy =
options.project &&
Object.values(projectGraph.nodes[options.project].data.targets).some(
(target) => target.executor === '@nrwl/cypress:cypress'
);
if (options.project) {
if (projectIsUsingLegacy) {
await migrateExecutorToPlugin(
tree,
projectGraph,
'@nrwl/cypress:cypress',
'@nx/cypress/plugin',
(targetName) => ({
targetName,
ciTargetName: 'e2e-ci',
}),
postTargetTransformer,
createNodes,
options.project
);
} else {
await migrateExecutorToPlugin(
tree,
projectGraph,
'@nx/cypress:cypress',
'@nx/cypress/plugin',
(targetName) => ({
targetName,
ciTargetName: 'e2e-ci',
}),
postTargetTransformer,
createNodes,
options.project
);
}
} else {
await migrateExecutorToPlugin(
tree,
projectGraph,
'@nx/cypress:cypress',
'@nx/cypress/plugin',
(targetName) => ({
targetName,
ciTargetName: 'e2e-ci',
}),
postTargetTransformer,
createNodes
);

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

if (!options.skipFormat) {
await formatFiles(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ class ExecutorToPluginMigrator<T> {
this.#executor
}" executor. Please select a project that does.`
: `Could not find any targets using the "${this.#executor}" executor.`;
throw new Error(errorMsg);
if (this.#specificProjectToMigrate) {
throw new Error(errorMsg);
} else if (this.#executor.startsWith('@nx')) {
console.warn(errorMsg);
}
}
}

Expand Down

0 comments on commit 13ac3bb

Please sign in to comment.