diff --git a/packages/cypress/src/generators/migrate-to-cypress-ten/__snapshots__/migrate-to-cypress-ten.spec.ts.snap b/packages/cypress/src/generators/migrate-to-cypress-ten/__snapshots__/migrate-to-cypress-ten.spec.ts.snap index 18b83367ee6de..baa967a5b1d70 100644 --- a/packages/cypress/src/generators/migrate-to-cypress-ten/__snapshots__/migrate-to-cypress-ten.spec.ts.snap +++ b/packages/cypress/src/generators/migrate-to-cypress-ten/__snapshots__/migrate-to-cypress-ten.spec.ts.snap @@ -202,7 +202,7 @@ Object { exports[`convertToCypressTen convertCypressProject should update project w/customized config 1`] = ` "import { defineConfig } from 'cypress' import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset'; -import setupNodeEvents from './src/plugins/index.js'; +import setupNodeEvents from './src/plugins/index'; const cypressJsonConfig = { \\"fileServerFolder\\": \\".\\", diff --git a/packages/cypress/src/generators/migrate-to-cypress-ten/conversion.util.ts b/packages/cypress/src/generators/migrate-to-cypress-ten/conversion.util.ts index 7e8d7eaad6310..8959384924724 100644 --- a/packages/cypress/src/generators/migrate-to-cypress-ten/conversion.util.ts +++ b/packages/cypress/src/generators/migrate-to-cypress-ten/conversion.util.ts @@ -344,3 +344,45 @@ export function addConfigToTsConfig( ); } } + +export function updatePluginFile( + tree: Tree, + projectConfig: ProjectConfiguration, + cypressConfigs: { + cypressConfigTs: Record; + cypressConfigJson: Record; + } +) { + // if ts file change module.exports = to export default + // if js file don't do anything + // update cypressConfigTs.e2e to remove file extension + const pluginsFile = cypressConfigs.cypressConfigTs?.e2e?.pluginsFile; + if (!pluginsFile) { + return cypressConfigs; + } + const ext = extname(pluginsFile); + const updatedCypressConfigs = { + ...cypressConfigs, + cypressConfigTs: { + e2e: { + ...cypressConfigs.cypressConfigTs.e2e, + pluginsFile: pluginsFile.replace(ext, ''), + }, + }, + }; + + const pluginFilePath = joinPathFragments(projectConfig.root, pluginsFile); + + if (ext === '.ts' && tree.exists(pluginFilePath)) { + const pluginFileContent = tree.read(pluginFilePath, 'utf-8'); + + tree.write( + pluginFilePath, + pluginFileContent + .replace('module.exports =', 'export default') + .replace(/module\.exports\.(.*?)=/g, 'export const $1=') + ); + } + + return updatedCypressConfigs; +} diff --git a/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.spec.ts b/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.spec.ts index 82430223b9a50..1572ef72ccbac 100644 --- a/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.spec.ts +++ b/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.spec.ts @@ -13,6 +13,7 @@ import { cypressProjectGenerator } from '../cypress-project/cypress-project'; import { createSupportFileImport, updateImports, + updatePluginFile, updateProjectPaths, } from './conversion.util'; import { migrateCypressProject } from './migrate-to-cypress-ten'; @@ -447,4 +448,89 @@ describe('a', () => { }); }); }); + + describe(updatePluginFile.name, () => { + it('should update module.exports for ts files', () => { + tree.write( + 'apps/app-e2e/src/plugins/index.ts', + ` +function myCoolFunction() { + console.log('cool') +} + +module.exports = function(on, config) { + // do stuff +} + +module.exports.blah = myCoolFunction; +` + ); + const actual = updatePluginFile( + tree, + { root: 'apps/app-e2e' }, + { + cypressConfigJson: {}, + cypressConfigTs: { e2e: { pluginsFile: './src/plugins/index.ts' } }, + } + ); + + expect(actual.cypressConfigTs.e2e.pluginsFile).toEqual( + './src/plugins/index' + ); + expect(tree.read('apps/app-e2e/src/plugins/index.ts', 'utf-8')).toEqual(` +function myCoolFunction() { + console.log('cool') +} + +export default function(on, config) { + // do stuff +} + +export const blah = myCoolFunction; +`); + }); + it('should not update .js file', () => { + const pluginFileContent = ` +function myCoolFunction() { + console.log('cool') +} + +module.exports = function(on, config) { + // do stuff +} + +module.exports.blah = myCoolFunction; +`; + tree.write('apps/app-e2e/src/plugins/index.js', pluginFileContent); + const actual = updatePluginFile( + tree, + { root: 'apps/app-e2e' }, + { + cypressConfigJson: {}, + cypressConfigTs: { e2e: { pluginsFile: './src/plugins/index.js' } }, + } + ); + + expect(actual.cypressConfigTs.e2e.pluginsFile).toEqual( + './src/plugins/index' + ); + expect(tree.read('apps/app-e2e/src/plugins/index.js', 'utf-8')).toEqual( + pluginFileContent + ); + }); + + it('should not update if no file is preset', () => { + const actual = updatePluginFile( + tree, + { root: 'apps/app-e2e' }, + { + cypressConfigJson: {}, + cypressConfigTs: { e2e: { pluginsFile: false } }, + } + ); + + expect(actual.cypressConfigTs.e2e.pluginsFile).toBeFalsy(); + expect(tree.exists('apps/app-e2e/src/plugins/index.ts')).toBeFalsy(); + }); + }); }); diff --git a/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.ts b/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.ts index c46c7e9665452..98a86a5bde2ef 100644 --- a/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.ts +++ b/packages/cypress/src/generators/migrate-to-cypress-ten/migrate-to-cypress-ten.ts @@ -4,6 +4,7 @@ import { installPackagesTask, joinPathFragments, logger, + ProjectConfiguration, readProjectConfiguration, stripIndents, Tree, @@ -11,12 +12,15 @@ import { updateProjectConfiguration, } from '@nrwl/devkit'; import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; +import { tsquery } from '@phenomnomnominal/tsquery'; +import { extname } from 'path'; import { CypressExecutorOptions } from '../../executors/cypress/cypress.impl'; import { cypressVersion } from '../../utils/versions'; import { addConfigToTsConfig, createNewCypressConfig, findCypressConfigs, + updatePluginFile, updateProjectPaths, writeNewConfig, } from './conversion.util'; @@ -39,13 +43,18 @@ export async function migrateCypressProject(tree: Tree) { tree.exists(cypressConfigPathJson) && !tree.exists(cypressConfigPathTs) ) { - const cypressConfigs = createNewCypressConfig( + let cypressConfigs = createNewCypressConfig( tree, projectConfig, cypressConfigPathJson ); updateProjectPaths(tree, projectConfig, cypressConfigs); + cypressConfigs = updatePluginFile( + tree, + projectConfig, + cypressConfigs + ); writeNewConfig(tree, cypressConfigPathTs, cypressConfigs); addConfigToTsConfig( tree,