From e0e5a60ef69a0d73fabf5e84334b5e097be366a6 Mon Sep 17 00:00:00 2001 From: Alejandro Estrada Date: Wed, 1 Jun 2022 08:26:18 -0500 Subject: [PATCH] fix: prevent adding default supportFile on migration (#21985) * fix: prevent adding default supportFile on migration * Update unit tests Co-authored-by: Tim Griesser --- .../src/sources/migration/codegen.ts | 7 ++++ .../unit/sources/migration/codegen.spec.ts | 10 ++++- .../launchpad/cypress/e2e/migration.cy.ts | 35 ++++++++++++++++ .../README.md | 40 +++++++++++++++++++ .../cypress.json | 3 ++ .../cypress/integration/basic.spec.js | 0 .../cypress/plugins/index.js | 0 .../cypress/support/index.js | 0 .../expected-cypress.config.js | 11 +++++ .../src/basic.spec.js | 0 10 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/README.md create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress.json create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/integration/basic.spec.js create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/plugins/index.js create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/support/index.js create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/expected-cypress.config.js create mode 100644 system-tests/projects/migration-e2e-custom-supportFile-default-value/src/basic.spec.js diff --git a/packages/data-context/src/sources/migration/codegen.ts b/packages/data-context/src/sources/migration/codegen.ts index 1f6c0fb52a84..ba1ace8ed12a 100644 --- a/packages/data-context/src/sources/migration/codegen.ts +++ b/packages/data-context/src/sources/migration/codegen.ts @@ -435,6 +435,13 @@ export function reduceConfig (cfg: LegacyCypressConfigJson, options: CreateConfi component: { ...acc.component, excludeSpecPattern: val }, } case 'supportFile': + // If the supportFile is set, but is the same value as the default one; where + // we migrate it, we do not want to put the legacy value in the migrated config. + // It can be .ts or .js + if (_.isNil(val) || !_.isBoolean(val) && val.match(/^cypress\/support($|\/index($|\.(ts|js)$))/)) { + return acc + } + return { ...acc, e2e: { ...acc.e2e, supportFile: val }, diff --git a/packages/data-context/test/unit/sources/migration/codegen.spec.ts b/packages/data-context/test/unit/sources/migration/codegen.spec.ts index 31192c3e4302..61f77c612a28 100644 --- a/packages/data-context/test/unit/sources/migration/codegen.spec.ts +++ b/packages/data-context/test/unit/sources/migration/codegen.spec.ts @@ -517,12 +517,20 @@ describe('reduceConfig', () => { }) it('should nest supportFile under component and e2e', () => { - const config = { supportFile: 'cypress/support/index.js' } + const config = { supportFile: 'cypress/support/mySupportFile.js' } const newConfig = reduceConfig(config, options) expect(newConfig.e2e.supportFile).to.eq(config.supportFile) }) + it('should not add supportFile if it is the default one', () => { + expect(reduceConfig({ supportFile: null }, options).e2e.supportFile).to.not.exist + expect(reduceConfig({ supportFile: undefined }, options).e2e.supportFile).to.not.exist + expect(reduceConfig({ supportFile: 'cypress/support' }, options).e2e.supportFile).to.not.exist + expect(reduceConfig({ supportFile: 'cypress/support/index' }, options).e2e.supportFile).to.not.exist + expect(reduceConfig({ supportFile: 'cypress/support/index.js' }, options).e2e.supportFile).to.not.exist + }) + it('should exclude the pluginsFile', () => { const config = { pluginsFile: 'cypress/plugins/index.js' } const newConfig = reduceConfig(config, options) diff --git a/packages/launchpad/cypress/e2e/migration.cy.ts b/packages/launchpad/cypress/e2e/migration.cy.ts index b077076543d9..c45af065ed0f 100644 --- a/packages/launchpad/cypress/e2e/migration.cy.ts +++ b/packages/launchpad/cypress/e2e/migration.cy.ts @@ -503,6 +503,41 @@ describe('Full migration flow for each project', { retries: { openMode: 0, runMo checkOutcome() }) + it('completes journey for migration-e2e-custom-supportFile-default-value', () => { + startMigrationFor('migration-e2e-custom-supportFile-default-value') + // default testFiles but custom integration - can rename automatically + cy.get(renameAutoStep).should('exist') + // no CT + cy.get(renameManualStep).should('not.exist') + // supportFile is false - cannot migrate + cy.get(renameSupportStep).should('exist') + cy.get(setupComponentStep).should('not.exist') + cy.get(configFileStep).should('exist') + + // Migration workflow + // before auto migration + cy.contains('cypress/integration/basic.spec.js') + + // after auto migration + cy.contains('cypress/e2e/basic.cy.js') + + runAutoRename() + + cy.withRetryableCtx(async (ctx) => { + const specs = ['cypress/e2e/basic.cy.js'] + + for (const spec of specs) { + const stats = await ctx.file.checkIfFileExists(ctx.path.join(spec)) + + expect(stats).to.not.be.null + } + }) + + renameSupport() + migrateAndVerifyConfig() + checkOutcome() + }) + it('completes journey for migration-e2e-custom-integration-default-value', () => { startMigrationFor('migration-e2e-custom-integration-default-value') // default testFiles but custom integration - can rename automatically diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/README.md b/system-tests/projects/migration-e2e-custom-supportFile-default-value/README.md new file mode 100644 index 000000000000..e68e63a9b508 --- /dev/null +++ b/system-tests/projects/migration-e2e-custom-supportFile-default-value/README.md @@ -0,0 +1,40 @@ +## Migration E2E Custom SupportFile with default value + +An e2e project with a custom `supportFile` named `cypress/support/index.js`. It uses the default `supportFile`. We will not +update the config file to show the supportFile because it is the same as the default one + +The following migration steps will be used during this migration: + +- [x] automatic file rename +- [ ] manual file rename +- [x] rename support +- [x] update config file +- [ ] setup component testing + + +## Automatic Migration + +Unless the user skips this step, after this step, the filesystem will be: + +| Before | After| +|---|---| +| `src/basic.test.js` | `src/basic.cy.js` | + +## Manual Files + +This step is not used. + +## Rename supportFile + +The project has a default support file, `cypress/support/index.js`. We can rename it for them to `cypress/support/e2e.js`. + +| Before | After| +|---|---| +| `cypress/support/index.js` | `cypress/support/e2e.js` | + +## Update Config + +We can migrate to the new `cypress.config.js`. The expected output is in `expected-cypress.config.js`. The main points are: + + +The expected output is in [`expected-cypress.config.js`](./expected-cypress.config.js). \ No newline at end of file diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress.json b/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress.json new file mode 100644 index 000000000000..b055da8fef2a --- /dev/null +++ b/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress.json @@ -0,0 +1,3 @@ +{ + "supportFile": "cypress/support/index.js" +} diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/integration/basic.spec.js b/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/integration/basic.spec.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/plugins/index.js b/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/plugins/index.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/support/index.js b/system-tests/projects/migration-e2e-custom-supportFile-default-value/cypress/support/index.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/expected-cypress.config.js b/system-tests/projects/migration-e2e-custom-supportFile-default-value/expected-cypress.config.js new file mode 100644 index 000000000000..294e9513923d --- /dev/null +++ b/system-tests/projects/migration-e2e-custom-supportFile-default-value/expected-cypress.config.js @@ -0,0 +1,11 @@ +const { defineConfig } = require('cypress') + +module.exports = defineConfig({ + e2e: { + // We've imported your old cypress plugins here. + // You may want to clean this up later by importing these. + setupNodeEvents (on, config) { + return require('./cypress/plugins/index.js')(on, config) + }, + }, +}) diff --git a/system-tests/projects/migration-e2e-custom-supportFile-default-value/src/basic.spec.js b/system-tests/projects/migration-e2e-custom-supportFile-default-value/src/basic.spec.js new file mode 100644 index 000000000000..e69de29bb2d1