Skip to content

Commit

Permalink
fix(testing): convert-to-inferred generators should add includes only…
Browse files Browse the repository at this point in the history
… when needed (#23159)

- fix(devkit): executor-to-plugin-migrator should remove includes when
all config files are handled
- fix(testing): cypress convert-to-inferred should add
ciWebServerCommand correctly

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->
`includes` are being added to all new plugins that are being added to
nx.json.
It should only be added when not all config files are addressed by the
plugin to be added.

Cypress Convert To Inferred is not creating a `ciWebServerCommand` when
it is possible to do so.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Ensure `includes` is added only when required.
Ensure `cypress:convert-to-inferred` is adding a ciWebServerCommand
correctly


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
Coly010 committed May 3, 2024
1 parent acd0993 commit 21d7e92
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ function createTestProject(
testingType: `e2e`,
devServerTarget: 'myapp:serve',
},
configurations: {
ci: {
devServerTarget: 'myapp:static-serve',
},
},
},
},
};
Expand Down Expand Up @@ -263,6 +268,7 @@ describe('Cypress - Convert Executors To Plugin', () => {
);
expect(hasCypressPlugin).toBeTruthy();
if (typeof hasCypressPlugin !== 'string') {
expect(hasCypressPlugin.include).not.toBeDefined();
[
['targetName', 'e2e'],
['ciTargetName', 'e2e-ci'],
Expand All @@ -272,6 +278,21 @@ describe('Cypress - Convert Executors To Plugin', () => {
);
});
}

// cypress.config.ts modifications
expect(tree.read(`${project.root}/cypress.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';
export default defineConfig({
e2e: {
...nxE2EPreset(__filename, {ciWebServerCommand: "npx nx run myapp:static-serve",webServerCommands: {"default":"npx nx run myapp:serve","ci":"npx nx run myapp:static-serve"}, cypressDir: 'src' }),
baseUrl: 'http://localhost:4200',
},
});"
`);
});

it('should setup Cypress plugin to match projects', async () => {
Expand Down Expand Up @@ -429,12 +450,12 @@ describe('Cypress - Convert Executors To Plugin', () => {
// project.json modifications
const updatedProject = readProjectConfiguration(tree, project.name);
expect(updatedProject.targets.e2e).toMatchInlineSnapshot(`
{
"options": {
"runner-ui": true,
},
}
`);
{
"options": {
"runner-ui": true,
},
}
`);

// nx.json modifications
const nxJsonPlugins = readNxJson(tree).plugins;
Expand Down Expand Up @@ -475,12 +496,12 @@ describe('Cypress - Convert Executors To Plugin', () => {
// project.json modifications
const updatedProject = readProjectConfiguration(tree, project.name);
expect(updatedProject.targets.e2e).toMatchInlineSnapshot(`
{
"options": {
"no-exit": true,
},
}
`);
{
"options": {
"no-exit": true,
},
}
`);

// nx.json modifications
const nxJsonPlugins = readNxJson(tree).plugins;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function postTargetTransformer(
tree,
configFilePath,
webServerCommands,
target.configurations?.ci?.devServerTarget
webServerCommands?.['ci']
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default defineConfig({
tree,
configFilePath,
{ default: 'npx nx run myorg:serve' },
'myorg:static-serve'
'npx nx run myorg:static-serve'
);

// ASSERT
Expand Down Expand Up @@ -151,7 +151,7 @@ export default defineConfig({
tree,
configFilePath,
{ default: 'npx nx run myorg:serve' },
'myorg:static-serve'
'npx nx run myorg:static-serve'
);

// ASSERT
Expand Down Expand Up @@ -191,7 +191,7 @@ export default defineConfig({
production: 'npx nx run myorg:serve:production',
ci: 'npx nx run myorg-static-serve',
},
'myorg:static-serve'
'npx nx run myorg:static-serve'
);

// ASSERT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function addDevServerTargetToConfig(
`${configFileContents.slice(
0,
ciWebServerCommandNode.getStart()
)}"npx nx run ${ciDevServerTarget}"${configFileContents.slice(
)}"${ciDevServerTarget}"${configFileContents.slice(
ciWebServerCommandNode.getEnd()
)}`
);
Expand All @@ -96,7 +96,7 @@ export function addDevServerTargetToConfig(
`${configFileContents.slice(
0,
nxE2ePresetOptionsNode.getStart() + 1
)}ciWebServerCommand: "npx nx run ${ciDevServerTarget}",${configFileContents.slice(
)}ciWebServerCommand: "${ciDevServerTarget}",${configFileContents.slice(
nxE2ePresetOptionsNode.getStart() + 1
)}`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ class ExecutorToPluginMigrator<T> {
}

if (!existingPlugin) {
const allConfigFilesAreIncluded = this.#configFiles.every(
(configFile) => {
for (const includePattern of plugin.include) {
if (minimatch(configFile, includePattern, { dot: true })) {
return true;
}
}
return false;
}
);
if (allConfigFilesAreIncluded) {
plugin.include = undefined;
}
this.#nxJson.plugins.push(plugin);
}
}
Expand Down

0 comments on commit 21d7e92

Please sign in to comment.