Skip to content

Commit d71a324

Browse files
authored
fix(testing): convert-to-inferred for cypress should handle nxE2EPreset 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 #
1 parent f73d653 commit d71a324

File tree

2 files changed

+98
-22
lines changed

2 files changed

+98
-22
lines changed

packages/cypress/src/generators/convert-to-inferred/lib/add-dev-server-target-to-config.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,44 @@ export default defineConfig({
7474
`);
7575
});
7676

77+
it('should add options object if it does not exist', () => {
78+
// ARRANGE
79+
tree.write(
80+
configFilePath,
81+
`import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
82+
import { defineConfig } from 'cypress';
83+
84+
export default defineConfig({
85+
e2e: {
86+
...nxE2EPreset(__filename),
87+
baseUrl: "http://localhost:4200",
88+
},
89+
});`
90+
);
91+
// ACT
92+
addDevServerTargetToConfig(
93+
tree,
94+
configFilePath,
95+
{
96+
default: 'npx nx run myorg:serve',
97+
},
98+
'npx nx run myorg:serve-static'
99+
);
100+
101+
// ASSERT
102+
expect(tree.read(configFilePath, 'utf-8')).toMatchInlineSnapshot(`
103+
"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
104+
import { defineConfig } from 'cypress';
105+
106+
export default defineConfig({
107+
e2e: {
108+
...nxE2EPreset(__filename,{ciWebServerCommand: "npx nx run myorg:serve-static", webServerCommands: {"default":"npx nx run myorg:serve"},}),
109+
baseUrl: "http://localhost:4200",
110+
},
111+
});"
112+
`);
113+
});
114+
77115
it('should update the webServerCommands if it does not match', () => {
78116
// ARRANGE
79117
tree.write(

packages/cypress/src/generators/convert-to-inferred/lib/add-dev-server-target-to-config.ts

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ export function addDevServerTargetToConfig(
2121

2222
let ast = tsquery.ast(configFileContents);
2323

24-
const NX_E2E_PRESET_OPTIONS_SELECTOR =
25-
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
26-
const nxE2ePresetOptionsNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
24+
const NX_E2E_PRESET_SELECTOR =
25+
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset])';
26+
const nxE2ePresetOptionsNodes = tsquery(ast, NX_E2E_PRESET_SELECTOR, {
2727
visitAllChildren: true,
2828
});
2929
if (nxE2ePresetOptionsNodes.length !== 0) {
30+
const NX_E2E_PRESET_OPTIONS_SELECTOR =
31+
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
32+
const optionsObjectNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
33+
visitAllChildren: true,
34+
});
35+
const hasObjectDefinition = optionsObjectNodes?.length > 0;
36+
3037
let nxE2ePresetOptionsNode = nxE2ePresetOptionsNodes[0];
3138
const WEB_SERVER_COMMANDS_SELECTOR =
3239
'PropertyAssignment:has(Identifier[name=webServerCommands])';
@@ -47,24 +54,43 @@ export function addDevServerTargetToConfig(
4754
)}${configFileContents.slice(webServerCommandsNodes[0].getEnd())}`
4855
);
4956
} else {
50-
tree.write(
51-
configFilePath,
52-
`${configFileContents.slice(
53-
0,
54-
nxE2ePresetOptionsNode.getStart() + 1
55-
)}webServerCommands: ${JSON.stringify(
56-
webServerCommands
57-
)},${configFileContents.slice(nxE2ePresetOptionsNode.getStart() + 1)}`
58-
);
57+
if (hasObjectDefinition) {
58+
tree.write(
59+
configFilePath,
60+
`${configFileContents.slice(
61+
0,
62+
optionsObjectNodes[0].getStart() + 1
63+
)}webServerCommands: ${JSON.stringify(
64+
webServerCommands
65+
)},${configFileContents.slice(optionsObjectNodes[0].getStart() + 1)}`
66+
);
67+
} else {
68+
tree.write(
69+
configFilePath,
70+
`${configFileContents.slice(
71+
0,
72+
nxE2ePresetOptionsNode.getEnd() - 1
73+
)},{ webServerCommands: ${JSON.stringify(
74+
webServerCommands
75+
)},}${configFileContents.slice(nxE2ePresetOptionsNode.getEnd() - 1)}`
76+
);
77+
}
5978
}
6079

6180
if (ciDevServerTarget) {
6281
configFileContents = tree.read(configFilePath, 'utf-8');
6382
ast = tsquery.ast(configFileContents);
64-
nxE2ePresetOptionsNode = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
83+
nxE2ePresetOptionsNode = tsquery(ast, NX_E2E_PRESET_SELECTOR, {
6584
visitAllChildren: true,
6685
})[0];
6786

87+
const NX_E2E_PRESET_OPTIONS_SELECTOR =
88+
'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression';
89+
const optionsObjectNodes = tsquery(ast, NX_E2E_PRESET_OPTIONS_SELECTOR, {
90+
visitAllChildren: true,
91+
});
92+
const hasObjectDefinition = optionsObjectNodes?.length > 0;
93+
6894
const CI_WEB_SERVER_COMMANDS_SELECTOR =
6995
'PropertyAssignment:has(Identifier[name=ciWebServerCommand])';
7096
const ciWebServerCommandsNodes = tsquery(
@@ -91,15 +117,27 @@ export function addDevServerTargetToConfig(
91117
);
92118
}
93119
} else {
94-
tree.write(
95-
configFilePath,
96-
`${configFileContents.slice(
97-
0,
98-
nxE2ePresetOptionsNode.getStart() + 1
99-
)}ciWebServerCommand: "${ciDevServerTarget}",${configFileContents.slice(
100-
nxE2ePresetOptionsNode.getStart() + 1
101-
)}`
102-
);
120+
if (hasObjectDefinition) {
121+
tree.write(
122+
configFilePath,
123+
`${configFileContents.slice(
124+
0,
125+
optionsObjectNodes[0].getStart() + 1
126+
)}ciWebServerCommand: "${ciDevServerTarget}",${configFileContents.slice(
127+
optionsObjectNodes[0].getStart() + 1
128+
)}`
129+
);
130+
} else {
131+
tree.write(
132+
configFilePath,
133+
`${configFileContents.slice(
134+
0,
135+
nxE2ePresetOptionsNode.getEnd() - 1
136+
)},{ ciWebServerCommand: "${ciDevServerTarget}",}${configFileContents.slice(
137+
nxE2ePresetOptionsNode.getEnd() - 1
138+
)}`
139+
);
140+
}
103141
}
104142
}
105143
}

0 commit comments

Comments
 (0)