UNPKG

@nx/cypress

Version:

The Nx Plugin for Cypress contains executors and generators allowing your workspace to use the powerful Cypress integration testing capabilities.

192 lines (191 loc) 7.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.componentConfigurationGenerator = componentConfigurationGenerator; exports.componentConfigurationGeneratorInternal = componentConfigurationGeneratorInternal; exports.updateTsConfigForComponentTesting = updateTsConfigForComponentTesting; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const internal_1 = require("@nx/devkit/internal"); const internal_2 = require("@nx/js/internal"); const assert_supported_cypress_version_1 = require("../../utils/assert-supported-cypress-version"); const deprecation_1 = require("../../utils/deprecation"); const versions_1 = require("../../utils/versions"); const base_setup_1 = require("../base-setup/base-setup"); const init_1 = tslib_1.__importDefault(require("../init/init")); function componentConfigurationGenerator(tree, options) { return componentConfigurationGeneratorInternal(tree, { addPlugin: false, ...options, }); } async function componentConfigurationGeneratorInternal(tree, options) { (0, assert_supported_cypress_version_1.assertSupportedCypressVersion)(tree); (0, internal_2.assertNotUsingTsSolutionSetup)(tree, 'cypress', 'component-configuration'); const tasks = []; const opts = normalizeOptions(tree, options); if (!(0, versions_1.getInstalledCypressMajorVersion)(tree)) { tasks.push(await (0, init_1.default)(tree, { ...opts, skipFormat: true, })); } const nxJson = (0, devkit_1.readNxJson)(tree); const hasPlugin = nxJson.plugins?.some((p) => typeof p === 'string' ? p === '@nx/cypress/plugin' : p.plugin === '@nx/cypress/plugin'); const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, opts.project); if (!opts.skipPackageJson) { tasks.push(updateDeps(tree, opts)); } addProjectFiles(tree, projectConfig, opts); if (!hasPlugin || opts.addExplicitTargets) { (0, deprecation_1.warnCypressExecutorGenerating)(); addTargetToProject(tree, projectConfig, opts); } updateNxJsonConfiguration(tree, hasPlugin); updateTsConfigForComponentTesting(tree, projectConfig); if (!opts.skipFormat) { await (0, devkit_1.formatFiles)(tree); } return (0, devkit_1.runTasksInSerial)(...tasks); } function normalizeOptions(tree, options) { const nxJson = (0, devkit_1.readNxJson)(tree); const addPlugin = process.env.NX_ADD_PLUGINS !== 'false' && nxJson.useInferencePlugins !== false; return { addPlugin, ...options, framework: options.framework ?? null, directory: options.directory ?? 'cypress', }; } function updateDeps(tree, opts) { const pkgVersions = (0, versions_1.versions)(tree); const devDeps = { cypress: pkgVersions.cypressVersion, }; if (opts.bundler === 'vite') { devDeps['@cypress/vite-dev-server'] = pkgVersions.cypressViteDevServerVersion; } else { devDeps['@cypress/webpack-dev-server'] = pkgVersions.cypressWebpackVersion; devDeps['html-webpack-plugin'] = pkgVersions.htmlWebpackPluginVersion; } return (0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDeps, undefined, true); } function addProjectFiles(tree, projectConfig, opts) { (0, base_setup_1.addBaseCypressSetup)(tree, { project: opts.project, directory: opts.directory, jsx: opts.jsx, }); (0, devkit_1.generateFiles)(tree, (0, devkit_1.joinPathFragments)(__dirname, 'files'), projectConfig.root, { ...opts, projectRoot: projectConfig.root, offsetFromRoot: (0, devkit_1.offsetFromRoot)(projectConfig.root), linter: isEslintInstalled(tree) ? 'eslint' : 'none', ext: '', }); } function addTargetToProject(tree, projectConfig, opts) { projectConfig.targets['component-test'] = { executor: '@nx/cypress:cypress', options: { cypressConfig: (0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress.config.ts'), testingType: 'component', }, }; (0, devkit_1.updateProjectConfiguration)(tree, opts.project, projectConfig); } function updateNxJsonConfiguration(tree, hasPlugin) { const nxJson = (0, devkit_1.readNxJson)(tree); const productionFileSet = nxJson.namedInputs?.production; if (productionFileSet) { nxJson.namedInputs.production = Array.from(new Set([ ...productionFileSet, '!{projectRoot}/cypress/**/*', '!{projectRoot}/**/*.cy.[jt]s?(x)', '!{projectRoot}/cypress.config.[jt]s', ])); } if (!hasPlugin) { const cacheableOperations = nxJson.tasksRunnerOptions?.default?.options?.cacheableOperations; if (cacheableOperations && !cacheableOperations.includes('component-test')) { cacheableOperations.push('component-test'); } // Either a `target: 'component-test'` default or a default keyed on // the executor we're scaffolding will apply to the new target — pick // the more specific (target-keyed) when both are present. const existingForTarget = (0, internal_1.findTargetDefault)(nxJson.targetDefaults, { target: 'component-test', }); const existingForExecutor = (0, internal_1.findTargetDefault)(nxJson.targetDefaults, { executor: '@nx/cypress:cypress', }); const existing = existingForTarget ?? existingForExecutor; (0, internal_1.upsertTargetDefault)(tree, nxJson, { target: 'component-test', cache: existing?.cache ?? true, inputs: existing?.inputs ?? [ 'default', productionFileSet ? '^production' : '^default', ], }); } (0, devkit_1.updateNxJson)(tree, nxJson); } function updateTsConfigForComponentTesting(tree, projectConfig) { let tsConfigPath = null; for (const candidate of ['tsconfig.lib.json', 'tsconfig.app.json']) { const p = (0, devkit_1.joinPathFragments)(projectConfig.root, candidate); if (tree.exists(p)) tsConfigPath = p; } if (tsConfigPath !== null) { (0, devkit_1.updateJson)(tree, tsConfigPath, (json) => { const excluded = new Set([ ...(json.exclude || []), 'cypress/**/*', 'cypress.config.ts', '**/*.cy.ts', '**/*.cy.js', '**/*.cy.tsx', '**/*.cy.jsx', ]); json.exclude = Array.from(excluded); return json; }); } const projectBaseTsConfig = (0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json'); if (tree.exists(projectBaseTsConfig)) { (0, devkit_1.updateJson)(tree, projectBaseTsConfig, (json) => { if (json.references) { const hasCyTsConfig = json.references.some((r) => r.path.includes('./cypress/tsconfig.json')); if (!hasCyTsConfig) { json.references.push({ path: './cypress/tsconfig.json' }); } } else { const excluded = new Set([ ...(json.exclude || []), 'cypress/**/*', 'cypress.config.ts', '**/*.cy.ts', '**/*.cy.js', '**/*.cy.tsx', '**/*.cy.jsx', ]); json.exclude = Array.from(excluded); } return json; }); } } function isEslintInstalled(tree) { const { dependencies, devDependencies } = (0, devkit_1.readJson)(tree, 'package.json'); return !!(dependencies?.eslint || devDependencies?.eslint); } exports.default = componentConfigurationGenerator;