Skip to content

Commit

Permalink
fix(testing): update the cypress plugin implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Nov 17, 2023
1 parent 30d94f7 commit ea952fb
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 316 deletions.
5 changes: 0 additions & 5 deletions packages/cypress/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
"version": "17.2.0-beta.0",
"description": "Add devServerTargets into cypress.config.ts files for @nx/cypress/plugin",
"implementation": "./src/migrations/update-17-2-0/add-dev-server-targets-to-cypress-configs"
},
"add-nx-cypress-plugin": {
"version": "17.2.0-beta.0",
"description": "Add the @nx/cypress/plugin to nx.json plugins",
"implementation": "./src/migrations/update-17-2-0/add-nx-cypress-plugin"
}
},
"packageJsonUpdates": {
Expand Down
95 changes: 56 additions & 39 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
createProjectGraphAsync,
logger,
parseTargetString,
workspaceRoot,
} from '@nx/devkit';
import { dirname, join, relative } from 'path';
import { lstatSync } from 'fs';

import vitePreprocessor from '../src/plugins/preprocessor-vite';
import { ChildProcess, fork } from 'node:child_process';
import { createExecutorContext } from '../src/utils/ct-helpers';
import { startDevServer } from '../src/utils/start-dev-server';

Expand Down Expand Up @@ -90,46 +88,56 @@ export function nxE2EPreset(
devServerTargetOptions: {},
ciDevServerTarget: options?.ciDevServerTarget,
},
async setupNodeEvents(on, config) {
if (options?.bundler === 'vite') {
on('file:preprocessor', vitePreprocessor());
}
if (!config.env.devServerTargets) {
return;
}
const devServerTarget =
config.env.devServerTarget ?? config.env.devServerTargets['default'];

if (!devServerTarget) {
return;
}
if (!config.baseUrl && devServerTarget) {
const graph = await createProjectGraphAsync();
const target = parseTargetString(devServerTarget, graph);
const context = createExecutorContext(
graph,
graph.nodes[target.project].data?.targets,
target.project,
target.target,
target.configuration
);
setupNodeEvents(on, config) {
return new Promise(async (resolve, reject) => {
if (options?.bundler === 'vite') {
on('file:preprocessor', vitePreprocessor());
}
if (!config.env.devServerTargets) {
resolve(undefined);
return;
}
const devServerTarget =
config.env.devServerTarget ?? config.env.devServerTargets['default'];

const devServer = startDevServer(
{
devServerTarget,
...config.env.devServerTargetOptions,
},
context
);
on('after:run', () => {
devServer.return();
});
const devServerValue = (await devServer.next()).value;
if (!devServerValue) {
if (!devServerTarget) {
resolve(undefined);
return;
}
return { ...config, baseUrl: devServerValue.baseUrl };
}
if (!config.baseUrl && devServerTarget) {
const graph = await createProjectGraphAsync();
const target = parseTargetString(devServerTarget, graph);
const context = createExecutorContext(
graph,
graph.nodes[target.project].data?.targets,
target.project,
target.target,
target.configuration
);

const devServer = startDevServer(
{
devServerTarget,
...config.env.devServerTargetOptions,
},
context
);
on('after:run', () => {
devServer.return();
});
process.on('message', (msg) => {
if (isServerReadyMessage(msg)) {
resolve({ ...config, baseUrl: msg.baseUrl });
}
});
const devServerValue = (await devServer.next()).value;
if (!devServerValue) {
resolve(undefined);
return;
}
resolve({ ...config, baseUrl: devServerValue.baseUrl });
}
});
},
};

Expand All @@ -148,3 +156,12 @@ export type NxCypressE2EPresetOptions = {
devServerTargets?: Record<string, string>;
ciDevServerTarget?: string;
};
function isServerReadyMessage(
msg: any
): msg is { type: string; baseUrl: string } {
return (
typeof msg === 'object' &&
msg.type === 'dev-server-ready' &&
typeof msg.baseUrl === 'string'
);
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit ea952fb

Please sign in to comment.