diff --git a/packages/remix/src/generators/application/application.impl.spec.ts b/packages/remix/src/generators/application/application.impl.spec.ts index 75d8ed5cad92d5..f9b0d2576758ab 100644 --- a/packages/remix/src/generators/application/application.impl.spec.ts +++ b/packages/remix/src/generators/application/application.impl.spec.ts @@ -1,6 +1,6 @@ import 'nx/src/internal-testing-utils/mock-project-graph'; -import { joinPathFragments, readJson, type Tree } from '@nx/devkit'; +import { joinPathFragments, readJson, readNxJson, type Tree } from '@nx/devkit'; import * as devkit from '@nx/devkit'; import { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; @@ -129,6 +129,14 @@ describe('Remix Application', () => { expectTargetsToBeCorrect(tree, '.'); expect(tree.read('e2e/cypress.config.ts', 'utf-8')).toMatchSnapshot(); + expect(readNxJson(tree).targetDefaults['e2e-ci--**/*']) + .toMatchInlineSnapshot(` + { + "dependsOn": [ + "^build", + ], + } + `); }); }); @@ -148,6 +156,14 @@ describe('Remix Application', () => { expectTargetsToBeCorrect(tree, '.'); expect(tree.read('e2e/playwright.config.ts', 'utf-8')).toMatchSnapshot(); + expect(readNxJson(tree).targetDefaults['e2e-ci--**/*']) + .toMatchInlineSnapshot(` + { + "dependsOn": [ + "^build", + ], + } + `); }); }); diff --git a/packages/remix/src/generators/application/lib/add-e2e.ts b/packages/remix/src/generators/application/lib/add-e2e.ts index 6f5fec99e607c4..43283705b7b02f 100644 --- a/packages/remix/src/generators/application/lib/add-e2e.ts +++ b/packages/remix/src/generators/application/lib/add-e2e.ts @@ -6,11 +6,19 @@ import { updateProjectConfiguration, ensurePackage, getPackageManagerCommand, + readNxJson, } from '@nx/devkit'; import { type NormalizedSchema } from './normalize-options'; import { getPackageVersion } from '../../../utils/versions'; +import { findPluginForConfigFile } from '@nx/devkit/src/utils/find-plugin-for-config-file'; +import { addE2eCiTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils'; export async function addE2E(tree: Tree, options: NormalizedSchema) { + const hasRemixPlugin = readNxJson(tree).plugins?.find((p) => + typeof p === 'string' + ? p === '@nx/remix/plugin' + : p.plugin === '@nx/remix/plugin' + ); if (options.e2eTestRunner === 'cypress') { const { configurationGenerator } = ensurePackage< typeof import('@nx/cypress') @@ -25,7 +33,7 @@ export async function addE2E(tree: Tree, options: NormalizedSchema) { implicitDependencies: [options.projectName], }); - return await configurationGenerator(tree, { + const e2eTask = await configurationGenerator(tree, { project: options.e2eProjectName, directory: 'src', skipFormat: true, @@ -33,6 +41,40 @@ export async function addE2E(tree: Tree, options: NormalizedSchema) { baseUrl: options.e2eWebServerAddress, addPlugin: options.addPlugin, }); + + if ( + options.addPlugin || + readNxJson(tree).plugins?.find((p) => + typeof p === 'string' + ? p === '@nx/cypress/plugin' + : p.plugin === '@nx/cypress/plugin' + ) + ) { + let buildTarget = '^build'; + if (hasRemixPlugin) { + const matchingPlugin = await findPluginForConfigFile( + tree, + `@nx/remix/plugin`, + joinPathFragments(options.projectRoot, 'remix.config.js') + ); + if (matchingPlugin && typeof matchingPlugin !== 'string') { + buildTarget = `^${ + (matchingPlugin.options as any)?.buildTargetName ?? 'build' + }`; + } + } + await addE2eCiTargetDefaults( + tree, + '@nx/cypress/plugin', + buildTarget, + joinPathFragments( + options.e2eProjectRoot, + `cypress.config.${options.js ? 'js' : 'ts'}` + ) + ); + } + + return e2eTask; } else if (options.e2eTestRunner === 'playwright') { const { configurationGenerator } = ensurePackage< typeof import('@nx/playwright') @@ -47,7 +89,7 @@ export async function addE2E(tree: Tree, options: NormalizedSchema) { implicitDependencies: [options.projectName], }); - return configurationGenerator(tree, { + const e2eTask = await configurationGenerator(tree, { project: options.e2eProjectName, skipFormat: true, skipPackageJson: false, @@ -62,6 +104,37 @@ export async function addE2E(tree: Tree, options: NormalizedSchema) { rootProject: options.rootProject, addPlugin: options.addPlugin, }); + + if ( + options.addPlugin || + readNxJson(tree).plugins?.find((p) => + typeof p === 'string' + ? p === '@nx/playwright/plugin' + : p.plugin === '@nx/playwright/plugin' + ) + ) { + let buildTarget = '^build'; + if (hasRemixPlugin) { + const matchingPlugin = await findPluginForConfigFile( + tree, + `@nx/remix/plugin`, + joinPathFragments(options.projectRoot, 'remix.config.js') + ); + if (matchingPlugin && typeof matchingPlugin !== 'string') { + buildTarget = `^${ + (matchingPlugin.options as any)?.buildTargetName ?? 'build' + }`; + } + } + await addE2eCiTargetDefaults( + tree, + '@nx/playwright/plugin', + buildTarget, + joinPathFragments(options.e2eProjectRoot, `playwright.config.ts`) + ); + } + + return e2eTask; } else { return () => {}; }