UNPKG

@nx/cypress

Version:

The Nx Plugin for Cypress contains executors and generators allowing your workspace to use the powerful Cypress integration testing capabilities.

129 lines (128 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const devkit_1 = require("@nx/devkit"); const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript"); const tsquery_1 = require("@phenomnomnominal/tsquery"); const config_1 = require("../../utils/config"); const migrations_1 = require("../../utils/migrations"); let printer; let ts; // https://docs.cypress.io/app/references/migration-guide#Changes-to-cyorigin // https://docs.cypress.io/app/references/changelog#:~:text=The%20experimentalSkipDomainInjection%20configuration%20has%20been,injectDocumentDomain%20configuration async function default_1(tree) { for await (const { cypressConfigPath } of (0, migrations_1.cypressProjectConfigs)(tree)) { if (!tree.exists(cypressConfigPath)) { // cypress config file doesn't exist, so skip continue; } ts ??= (0, ensure_typescript_1.ensureTypescript)(); printer ??= ts.createPrinter(); const cypressConfig = tree.read(cypressConfigPath, 'utf-8'); const updatedConfig = setInjectDocumentDomain(cypressConfig); tree.write(cypressConfigPath, updatedConfig); } await (0, devkit_1.formatFiles)(tree); } function setInjectDocumentDomain(cypressConfig) { const config = (0, config_1.resolveCypressConfigObject)(cypressConfig); if (!config) { // couldn't find the config object, leave as is return cypressConfig; } const sourceFile = tsquery_1.tsquery.ast(cypressConfig); let e2eProperty = (0, migrations_1.getObjectProperty)(config, 'e2e'); let hasOtherTopLevelProperties = config.properties.some((p) => ts.isPropertyAssignment(p) && p.name.getText() !== 'e2e' && p.name.getText() !== 'component'); let updatedConfig = config; const topLevelExperimentalSkipDomainInjectionProperty = (0, migrations_1.getObjectProperty)(updatedConfig, 'experimentalSkipDomainInjection'); const topLevelSkipDomainState = !topLevelExperimentalSkipDomainInjectionProperty ? 'not-set' : !ts.isArrayLiteralExpression(topLevelExperimentalSkipDomainInjectionProperty.initializer) || topLevelExperimentalSkipDomainInjectionProperty.initializer.elements .length > 0 ? 'skipping' : 'not-skipping'; let e2eSkipDomainState = 'not-set'; if (e2eProperty) { let experimentalSkipDomainInjectionProperty; let isObjectLiteral = false; if (ts.isObjectLiteralExpression(e2eProperty.initializer)) { experimentalSkipDomainInjectionProperty = (0, migrations_1.getObjectProperty)(e2eProperty.initializer, 'experimentalSkipDomainInjection'); isObjectLiteral = true; } if (experimentalSkipDomainInjectionProperty) { e2eSkipDomainState = !ts.isArrayLiteralExpression(experimentalSkipDomainInjectionProperty.initializer) || experimentalSkipDomainInjectionProperty.initializer.elements.length > 0 ? 'skipping' : 'not-skipping'; } if (e2eSkipDomainState === 'not-set' && topLevelSkipDomainState === 'not-set') { updatedConfig = (0, migrations_1.updateObjectProperty)(updatedConfig, e2eProperty, { newValue: setInjectDocumentDomainInObject(e2eProperty.initializer), }); } else if (e2eSkipDomainState === 'not-skipping') { updatedConfig = (0, migrations_1.updateObjectProperty)(updatedConfig, e2eProperty, { newValue: replaceExperimentalSkipDomainInjectionInObject(e2eProperty.initializer), }); } else if (e2eSkipDomainState === 'skipping') { updatedConfig = (0, migrations_1.updateObjectProperty)(updatedConfig, e2eProperty, { newValue: (0, migrations_1.removeObjectProperty)( // we only determine that it's skipping if it's an object literal e2eProperty.initializer, (0, migrations_1.getObjectProperty)(e2eProperty.initializer, 'experimentalSkipDomainInjection')), }); } } if (topLevelSkipDomainState === 'not-set' && !e2eProperty && hasOtherTopLevelProperties) { updatedConfig = setInjectDocumentDomainInObject(updatedConfig); } else if (topLevelSkipDomainState === 'not-skipping') { updatedConfig = replaceExperimentalSkipDomainInjectionInObject(updatedConfig); } else if (topLevelSkipDomainState === 'skipping') { updatedConfig = (0, migrations_1.removeObjectProperty)(updatedConfig, topLevelExperimentalSkipDomainInjectionProperty); } return cypressConfig.replace(config.getText(), printer.printNode(ts.EmitHint.Unspecified, updatedConfig, sourceFile)); } function setInjectDocumentDomainInObject(config) { let configToUpdate; if (ts.isObjectLiteralExpression(config)) { configToUpdate = config; } else { // spread the current expression into a new object literal configToUpdate = ts.factory.createObjectLiteralExpression([ ts.factory.createSpreadAssignment(config), ]); } return ts.factory.updateObjectLiteralExpression(configToUpdate, ts.factory.createNodeArray([ ...configToUpdate.properties, getInjectDocumentDomainPropertyAssignment(), ])); } function replaceExperimentalSkipDomainInjectionInObject(config) { let configToUpdate; if (ts.isObjectLiteralExpression(config)) { configToUpdate = config; } else { // spread the current expression into a new object literal configToUpdate = ts.factory.createObjectLiteralExpression([ ts.factory.createSpreadAssignment(config), ]); } return ts.factory.updateObjectLiteralExpression(configToUpdate, configToUpdate.properties.map((property) => property.name?.getText() === 'experimentalSkipDomainInjection' ? getInjectDocumentDomainPropertyAssignment() : property)); } function getInjectDocumentDomainPropertyAssignment() { return ts.addSyntheticLeadingComment(ts.addSyntheticLeadingComment(ts.factory.createPropertyAssignment(ts.factory.createIdentifier('injectDocumentDomain'), ts.factory.createTrue()), ts.SyntaxKind.SingleLineCommentTrivia, ' Please ensure you use `cy.origin()` when navigating between domains and remove this option.'), ts.SyntaxKind.SingleLineCommentTrivia, ' See https://docs.cypress.io/app/references/migration-guide#Changes-to-cyorigin'); }