Skip to content

Commit

Permalink
fix(testing): update exports for .ts plugin files (nrwl#11200)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Jul 19, 2022
1 parent 8173887 commit 0cd8d8c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
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

0 comments on commit 0cd8d8c

Please sign in to comment.