Skip to content

Commit

Permalink
fix(remix): set up e2e so it runs out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jun 29, 2023
1 parent d6b3b68 commit 03848b2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
47 changes: 47 additions & 0 deletions packages/remix/src/generators/cypress/cypress.impl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readProjectConfiguration, Tree } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import generator from './cypress.impl';

describe('Cypress generator', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should generate cypress project', async () => {
await generator(tree, { project: 'demo', name: 'demo-e2e' });

const config = readProjectConfiguration(tree, 'demo-e2e');
expect(config.targets).toEqual({
e2e: {
dependsOn: ['dev-server'],
executor: '@nx/cypress:cypress',
options: {
baseUrl: 'http://localhost:3000',
cypressConfig: 'demo-e2e/cypress.config.ts',
skipServe: true,
testingType: 'e2e',
},
},
lint: {
executor: '@nx/linter:eslint',
options: {
lintFilePatterns: ['demo-e2e/**/*.{js,ts}'],
},
outputs: ['{options.outputFile}'],
},
'dev-server': {
command: 'nx serve demo',
configurations: {
production: {
command: 'nx serve demo --configuration=production',
},
},
options: {
readyWhen: 'Server started',
},
},
});
});
});
40 changes: 35 additions & 5 deletions packages/remix/src/generators/cypress/cypress.impl.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import {
ensurePackage,
GeneratorCallback,
joinPathFragments,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';

import { version as nxVersion } from 'nx/package.json';
import { CypressGeneratorSchema } from './schema';

export default async function (tree: Tree, options: any) {
export default async function (
tree: Tree,
options: CypressGeneratorSchema
): Promise<GeneratorCallback> {
options.baseUrl ??= 'http://localhost:3000';
const { cypressInitGenerator, cypressProjectGenerator } = ensurePackage(
'@nx/cypress',
nxVersion
Expand All @@ -19,10 +26,12 @@ export default async function (tree: Tree, options: any) {
standaloneConfig: true,
});

const config = readProjectConfiguration(tree, options.name);
tree.delete(joinPathFragments(config.sourceRoot, 'support', 'app.po.ts'));
const projectConfig = readProjectConfiguration(tree, options.name);
tree.delete(
joinPathFragments(projectConfig.sourceRoot, 'support', 'app.po.ts')
);
tree.write(
joinPathFragments(config.sourceRoot, 'e2e', 'app.cy.ts'),
joinPathFragments(projectConfig.sourceRoot, 'e2e', 'app.cy.ts'),
`describe('webapp', () => {
beforeEach(() => cy.visit('/'));
Expand All @@ -33,7 +42,7 @@ export default async function (tree: Tree, options: any) {
);

const supportFilePath = joinPathFragments(
config.sourceRoot,
projectConfig.sourceRoot,
'support',
'e2e.ts'
);
Expand All @@ -58,6 +67,27 @@ Cypress.on("uncaught:exception", (err) => {
});`
);

// run-commands won't emit { success: true, baseUrl: '...' } to Cypress executor.
// We'll wire it up manually and skip serve from Cypress.
projectConfig.targets.e2e.options.skipServe = true;
projectConfig.targets.e2e.options.baseUrl =
options.baseUrl ?? 'http://localhost:3000';
projectConfig.targets.e2e.dependsOn = ['dev-server'];
delete projectConfig.targets.e2e.options.devServerTarget;
delete projectConfig.targets.e2e?.configurations?.production.devServerTarget;
projectConfig.targets['dev-server'] = {
command: `nx serve ${options.project}`,
options: {
readyWhen: 'Server started',
},
configurations: {
production: {
command: `nx serve ${options.project} --configuration=production`,
},
},
};
updateProjectConfiguration(tree, options.name, projectConfig);

// returning this in case the cypress generator has any side effects
return async () => {
await initSideEffects();
Expand Down
10 changes: 10 additions & 0 deletions packages/remix/src/generators/cypress/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface CypressGeneratorSchema {
project: string;
name: string;
baseUrl?: string;
directory?: string;
linter?: 'none' | 'eslint';
js?: boolean;
skipFormat?: boolean;
setParserOptionsProject?: boolean;
}

0 comments on commit 03848b2

Please sign in to comment.