Skip to content

Commit

Permalink
fix(remix): application generator should add e2e-ci target defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Aug 2, 2024
1 parent 2ff3798 commit 4ab91cd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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",
],
}
`);
});
});

Expand All @@ -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",
],
}
`);
});
});

Expand Down
77 changes: 75 additions & 2 deletions packages/remix/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -25,14 +33,48 @@ 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,
devServerTarget: `${options.projectName}:${options.e2eWebServerTarget}:development`,
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')
Expand All @@ -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,
Expand All @@ -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 () => {};
}
Expand Down

0 comments on commit 4ab91cd

Please sign in to comment.