fix(testing): update exports for .ts plugin files (#11200)
This commit is contained in:
parent
8173887fbb
commit
0cd8d8c531
@ -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\\": \\".\\",
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user