Skip to content

Commit

Permalink
fix(testing): convert-to-inferred for cypress should handle nxE2EPres…
Browse files Browse the repository at this point in the history
…et with no options object (#23171)

<!-- 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 -->



## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## 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 f73d653 commit d71a324
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ export default defineConfig({
`);
});

it('should add options object if it does not exist', () => {
// ARRANGE
tree.write(
configFilePath,
`import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';
export default defineConfig({
e2e: {
...nxE2EPreset(__filename),
baseUrl: "http://localhost:4200",
},
});`
);
// ACT
addDevServerTargetToConfig(
tree,
configFilePath,
{
default: 'npx nx run myorg:serve',
},
'npx nx run myorg:serve-static'
);

// ASSERT
expect(tree.read(configFilePath, '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 myorg:serve-static", webServerCommands: {"default":"npx nx run myorg:serve"},}),
baseUrl: "http://localhost:4200",
},
});"
`);
});

it('should update the webServerCommands if it does not match', () => {
// ARRANGE
tree.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ export function addDevServerTargetToConfig(

let ast = tsquery.ast(configFileContents);

const NX_E2E_PRESET_OPTIONS_SELECTOR =
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
const nxE2ePresetOptionsNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
const NX_E2E_PRESET_SELECTOR =
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset])';
const nxE2ePresetOptionsNodes = tsquery(ast, NX_E2E_PRESET_SELECTOR, {
visitAllChildren: true,
});
if (nxE2ePresetOptionsNodes.length !== 0) {
const NX_E2E_PRESET_OPTIONS_SELECTOR =
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
const optionsObjectNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
visitAllChildren: true,
});
const hasObjectDefinition = optionsObjectNodes?.length > 0;

let nxE2ePresetOptionsNode = nxE2ePresetOptionsNodes[0];
const WEB_SERVER_COMMANDS_SELECTOR =
'PropertyAssignment:has(Identifier[name=webServerCommands])';
Expand All @@ -47,24 +54,43 @@ export function addDevServerTargetToConfig(
)}${configFileContents.slice(webServerCommandsNodes[0].getEnd())}`
);
} else {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
nxE2ePresetOptionsNode.getStart() + 1
)}webServerCommands: ${JSON.stringify(
webServerCommands
)},${configFileContents.slice(nxE2ePresetOptionsNode.getStart() + 1)}`
);
if (hasObjectDefinition) {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
optionsObjectNodes[0].getStart() + 1
)}webServerCommands: ${JSON.stringify(
webServerCommands
)},${configFileContents.slice(optionsObjectNodes[0].getStart() + 1)}`
);
} else {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
nxE2ePresetOptionsNode.getEnd() - 1
)},{ webServerCommands: ${JSON.stringify(
webServerCommands
)},}${configFileContents.slice(nxE2ePresetOptionsNode.getEnd() - 1)}`
);
}
}

if (ciDevServerTarget) {
configFileContents = tree.read(configFilePath, 'utf-8');
ast = tsquery.ast(configFileContents);
nxE2ePresetOptionsNode = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
nxE2ePresetOptionsNode = tsquery(ast, NX_E2E_PRESET_SELECTOR, {
visitAllChildren: true,
})[0];

const NX_E2E_PRESET_OPTIONS_SELECTOR =
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
const optionsObjectNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
visitAllChildren: true,
});
const hasObjectDefinition = optionsObjectNodes?.length > 0;

const CI_WEB_SERVER_COMMANDS_SELECTOR =
'PropertyAssignment:has(Identifier[name=ciWebServerCommand])';
const ciWebServerCommandsNodes = tsquery(
Expand All @@ -91,15 +117,27 @@ export function addDevServerTargetToConfig(
);
}
} else {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
nxE2ePresetOptionsNode.getStart() + 1
)}ciWebServerCommand: "${ciDevServerTarget}",${configFileContents.slice(
nxE2ePresetOptionsNode.getStart() + 1
)}`
);
if (hasObjectDefinition) {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
optionsObjectNodes[0].getStart() + 1
)}ciWebServerCommand: "${ciDevServerTarget}",${configFileContents.slice(
optionsObjectNodes[0].getStart() + 1
)}`
);
} else {
tree.write(
configFilePath,
`${configFileContents.slice(
0,
nxE2ePresetOptionsNode.getEnd() - 1
)},{ ciWebServerCommand: "${ciDevServerTarget}",}${configFileContents.slice(
nxE2ePresetOptionsNode.getEnd() - 1
)}`
);
}
}
}
}
Expand Down

0 comments on commit d71a324

Please sign in to comment.