fix(testing): do not update component configuration in cypress set-inject-document-domain migration (#31614)

## Current Behavior

The `set-inject-document-domain` migration updates the component
configuration. This is incorrect since the `inject-document-domain` is
not a property supported by the component configuration.

## Expected Behavior

The `set-inject-document-domain` migration should not update the
component configuration.

## Related Issue(s)

Fixes #31610
This commit is contained in:
Leosvel Pérez Espinosa 2025-06-20 15:45:36 +02:00 committed by GitHub
parent 6b2175bfcb
commit 617b8d49cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 169 deletions

View File

@ -500,7 +500,7 @@ export default defineConfig({
`);
});
it('should set the injectDocumentDomain property to true in the component config when defined and experimentalSkipDomainInjection is not set', async () => {
it('should not set the injectDocumentDomain property in the component config', async () => {
addProjectConfiguration(tree, 'app1-e2e', {
root: 'apps/app1-e2e',
projectType: 'application',
@ -521,120 +521,6 @@ export default defineConfig({
await migration(tree);
expect(tree.read('apps/app1-e2e/cypress.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename, { bundler: 'vite' }),
// Please ensure you use \`cy.origin()\` when navigating between domains and remove this option.
// See https://docs.cypress.io/app/references/migration-guide#Changes-to-cyorigin
injectDocumentDomain: true,
},
});
"
`);
});
it('should set the injectDocumentDomain property to true in the component config when it is not an object literal', async () => {
addProjectConfiguration(tree, 'app1-e2e', {
root: 'apps/app1-e2e',
projectType: 'application',
targets: {},
});
tree.write(
'apps/app1-e2e/cypress.config.ts',
`import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: nxComponentTestingPreset(__filename, { bundler: 'vite' }),
});
`
);
await migration(tree);
expect(tree.read('apps/app1-e2e/cypress.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename, { bundler: 'vite' }),
// Please ensure you use \`cy.origin()\` when navigating between domains and remove this option.
// See https://docs.cypress.io/app/references/migration-guide#Changes-to-cyorigin
injectDocumentDomain: true,
},
});
"
`);
});
it('should replace the experimentalSkipDomainInjection property in the component config with injectDocumentDomain when it is set to an empty array', async () => {
addProjectConfiguration(tree, 'app1-e2e', {
root: 'apps/app1-e2e',
projectType: 'application',
targets: {},
});
tree.write(
'apps/app1-e2e/cypress.config.ts',
`import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename, { bundler: 'vite' }),
experimentalSkipDomainInjection: [],
},
});
`
);
await migration(tree);
expect(tree.read('apps/app1-e2e/cypress.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename, { bundler: 'vite' }),
// Please ensure you use \`cy.origin()\` when navigating between domains and remove this option.
// See https://docs.cypress.io/app/references/migration-guide#Changes-to-cyorigin
injectDocumentDomain: true,
},
});
"
`);
});
it('should remove the experimentalSkipDomainInjection property in the component config when it is set to a non-empty array', async () => {
addProjectConfiguration(tree, 'app1-e2e', {
root: 'apps/app1-e2e',
projectType: 'application',
targets: {},
});
tree.write(
'apps/app1-e2e/cypress.config.ts',
`import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename, { bundler: 'vite' }),
experimentalSkipDomainInjection: ['https://example.com'],
},
});
`
);
await migration(tree);
expect(tree.read('apps/app1-e2e/cypress.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { nxComponentTestingPreset } from '@nx/react/plugins/component-testing';

View File

@ -49,7 +49,12 @@ function setInjectDocumentDomain(cypressConfig: string): string {
const sourceFile = tsquery.ast(cypressConfig);
let e2eProperty = getObjectProperty(config, 'e2e');
let componentProperty = getObjectProperty(config, 'component');
let hasOtherTopLevelProperties = config.properties.some(
(p): p is PropertyAssignment =>
ts.isPropertyAssignment(p) &&
p.name.getText() !== 'e2e' &&
p.name.getText() !== 'component'
);
let updatedConfig = config;
const topLevelExperimentalSkipDomainInjectionProperty = getObjectProperty(
@ -116,62 +121,10 @@ function setInjectDocumentDomain(cypressConfig: string): string {
}
}
let componentSkipDomainState: 'not-set' | 'skipping' | 'not-skipping' =
'not-set';
if (componentProperty) {
let experimentalSkipDomainInjectionProperty: PropertyAssignment | undefined;
let isObjectLiteral = false;
if (ts.isObjectLiteralExpression(componentProperty.initializer)) {
experimentalSkipDomainInjectionProperty = getObjectProperty(
componentProperty.initializer,
'experimentalSkipDomainInjection'
);
isObjectLiteral = true;
}
if (experimentalSkipDomainInjectionProperty) {
componentSkipDomainState =
!ts.isArrayLiteralExpression(
experimentalSkipDomainInjectionProperty.initializer
) ||
experimentalSkipDomainInjectionProperty.initializer.elements.length > 0
? 'skipping'
: 'not-skipping';
}
if (
componentSkipDomainState === 'not-set' &&
topLevelSkipDomainState === 'not-set'
) {
updatedConfig = updateObjectProperty(updatedConfig, componentProperty, {
newValue: setInjectDocumentDomainInObject(
componentProperty.initializer
),
});
} else if (componentSkipDomainState === 'not-skipping') {
updatedConfig = updateObjectProperty(updatedConfig, componentProperty, {
newValue: replaceExperimentalSkipDomainInjectionInObject(
componentProperty.initializer
),
});
} else if (componentSkipDomainState === 'skipping') {
updatedConfig = updateObjectProperty(updatedConfig, componentProperty, {
newValue: removeObjectProperty(
// we only determine that it's skipping if it's an object literal
componentProperty.initializer as ObjectLiteralExpression,
getObjectProperty(
componentProperty.initializer as ObjectLiteralExpression,
'experimentalSkipDomainInjection'
)
),
});
}
}
if (
topLevelSkipDomainState === 'not-set' &&
!e2eProperty &&
!componentProperty
hasOtherTopLevelProperties
) {
updatedConfig = setInjectDocumentDomainInObject(updatedConfig);
} else if (topLevelSkipDomainState === 'not-skipping') {