Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): correctly migrate plugins file if present #11200

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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\\": \\".\\",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import {
installPackagesTask,
joinPathFragments,
logger,
ProjectConfiguration,
readProjectConfiguration,
stripIndents,
Tree,
updateJson,
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';
Expand All @@ -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,
Expand Down