fix(testing): update exports for .ts plugin files (#11200)

This commit is contained in:
Caleb Ukle 2022-07-19 17:28:02 -05:00 committed by GitHub
parent 8173887fbb
commit 0cd8d8c531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 139 additions and 2 deletions

View File

@ -202,7 +202,7 @@ Object {
exports[`convertToCypressTen convertCypressProject should update project w/customized config 1`] = ` exports[`convertToCypressTen convertCypressProject should update project w/customized config 1`] = `
"import { defineConfig } from 'cypress' "import { defineConfig } from 'cypress'
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset'; import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
import setupNodeEvents from './src/plugins/index.js'; import setupNodeEvents from './src/plugins/index';
const cypressJsonConfig = { const cypressJsonConfig = {
\\"fileServerFolder\\": \\".\\", \\"fileServerFolder\\": \\".\\",

View File

@ -344,3 +344,45 @@ export function addConfigToTsConfig(
); );
} }
} }
export function updatePluginFile(
tree: Tree,
projectConfig: ProjectConfiguration,
cypressConfigs: {
cypressConfigTs: Record<string, any>;
cypressConfigJson: Record<string, any>;
}
) {
// 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;
}

View File

@ -13,6 +13,7 @@ import { cypressProjectGenerator } from '../cypress-project/cypress-project';
import { import {
createSupportFileImport, createSupportFileImport,
updateImports, updateImports,
updatePluginFile,
updateProjectPaths, updateProjectPaths,
} from './conversion.util'; } from './conversion.util';
import { migrateCypressProject } from './migrate-to-cypress-ten'; 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();
});
});
}); });

View File

@ -4,6 +4,7 @@ import {
installPackagesTask, installPackagesTask,
joinPathFragments, joinPathFragments,
logger, logger,
ProjectConfiguration,
readProjectConfiguration, readProjectConfiguration,
stripIndents, stripIndents,
Tree, Tree,
@ -11,12 +12,15 @@ import {
updateProjectConfiguration, updateProjectConfiguration,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; 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 { CypressExecutorOptions } from '../../executors/cypress/cypress.impl';
import { cypressVersion } from '../../utils/versions'; import { cypressVersion } from '../../utils/versions';
import { import {
addConfigToTsConfig, addConfigToTsConfig,
createNewCypressConfig, createNewCypressConfig,
findCypressConfigs, findCypressConfigs,
updatePluginFile,
updateProjectPaths, updateProjectPaths,
writeNewConfig, writeNewConfig,
} from './conversion.util'; } from './conversion.util';
@ -39,13 +43,18 @@ export async function migrateCypressProject(tree: Tree) {
tree.exists(cypressConfigPathJson) && tree.exists(cypressConfigPathJson) &&
!tree.exists(cypressConfigPathTs) !tree.exists(cypressConfigPathTs)
) { ) {
const cypressConfigs = createNewCypressConfig( let cypressConfigs = createNewCypressConfig(
tree, tree,
projectConfig, projectConfig,
cypressConfigPathJson cypressConfigPathJson
); );
updateProjectPaths(tree, projectConfig, cypressConfigs); updateProjectPaths(tree, projectConfig, cypressConfigs);
cypressConfigs = updatePluginFile(
tree,
projectConfig,
cypressConfigs
);
writeNewConfig(tree, cypressConfigPathTs, cypressConfigs); writeNewConfig(tree, cypressConfigPathTs, cypressConfigs);
addConfigToTsConfig( addConfigToTsConfig(
tree, tree,