Skip to content

Commit

Permalink
feat(testing): add cypress create nodes plugin (#19840)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Nov 9, 2023
1 parent 5973784 commit a86d926
Show file tree
Hide file tree
Showing 39 changed files with 1,851 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ describe('Cypress Component Testing Configuration', () => {
generateTests: false,
});
}).resolves;

expect(
require('@nx/devkit').createProjectGraphAsync
).not.toHaveBeenCalled();
});
it('should use own project config', async () => {
await generateTestApplication(tree, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export default defineConfig({

exports[`e2e migrator cypress with project root at "" cypress version >=10 should create a cypress.config.ts file when it does not exist 1`] = `
"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';
export default defineConfig({
e2e: nxE2EPreset(__filename, { cypressDir: 'src' })
e2e: { ...nxE2EPreset(__filename, { cypressDir: 'src' }) },
});
"
`;
Expand Down Expand Up @@ -91,10 +92,11 @@ export default defineConfig({

exports[`e2e migrator cypress with project root at "projects/app1" cypress version >=10 should create a cypress.config.ts file when it does not exist 1`] = `
"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';
export default defineConfig({
e2e: nxE2EPreset(__filename, { cypressDir: 'src' })
e2e: { ...nxE2EPreset(__filename, { cypressDir: 'src' }) },
});
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,6 @@ describe('app migrator', () => {
).toStrictEqual([
'build',
'lint',
'e2e',
'myCustomTest',
'myCustomLint',
'myCustomBuild',
Expand Down Expand Up @@ -1742,7 +1741,7 @@ describe('app migrator', () => {
const { targetDefaults } = readNxJson(tree);
expect(
Object.keys(targetDefaults).filter((f) => targetDefaults[f].cache)
).toStrictEqual(['build', 'lint', 'e2e', 'myCustomTest']);
).toStrictEqual(['build', 'lint', 'myCustomTest', 'e2e']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock('fs', () => {
});

import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version';
import type { ProjectConfiguration, Tree } from '@nx/devkit';
import { formatFiles, ProjectConfiguration, Tree } from '@nx/devkit';
import {
joinPathFragments,
offsetFromRoot,
Expand Down Expand Up @@ -826,6 +826,8 @@ describe('e2e migrator', () => {

await migrator.migrate();

await formatFiles(tree);

expect(tree.exists('apps/app1-e2e/cypress.config.ts')).toBe(true);
const cypressConfig = tree.read(
'apps/app1-e2e/cypress.config.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,6 @@ describe('lib migrator', () => {
).toStrictEqual([
'build',
'lint',
'e2e',
'myCustomBuild',
'myCustomTest',
'myCustomLint',
Expand All @@ -1306,7 +1305,7 @@ describe('lib migrator', () => {
const { targetDefaults } = readNxJson(tree);
expect(
Object.keys(targetDefaults).filter((f) => targetDefaults[f].cache)
).toStrictEqual(['build', 'lint', 'e2e', 'myCustomTest']);
).toStrictEqual(['build', 'lint', 'myCustomTest']);
});
});
});
10 changes: 10 additions & 0 deletions packages/cypress/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
"version": "16.8.0-beta.4",
"description": "Update to Cypress v13. Most noteable change is video recording is off by default. This migration will only update if the workspace is already on Cypress v12. https://docs.cypress.io/guides/references/migration-guide#Migrating-to-Cypress-130",
"implementation": "./src/migrations/update-16-8-0/cypress-13"
},
"add-nx-metadata": {
"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
8 changes: 4 additions & 4 deletions packages/cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@nx/devkit": "file:../devkit",
"@nx/eslint": "file:../eslint",
"@nx/js": "file:../js",
"@phenomnomnominal/tsquery": "~5.0.1",
"detect-port": "^1.5.1",
"semver": "7.5.3",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@nx/eslint": "file:../eslint"
"tslib": "^2.3.0"
},
"peerDependencies": {
"cypress": ">= 3 < 14"
Expand Down
1 change: 1 addition & 0 deletions packages/cypress/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createNodes } from './src/plugins/plugin';
70 changes: 59 additions & 11 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { workspaceRoot } from '@nx/devkit';
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';

interface BaseCypressPreset {
videosFolder: string;
Expand Down Expand Up @@ -65,29 +73,66 @@ export function nxBaseCypressPreset(
* }
* })
*
* @param pathToConfig will be used to construct the output paths for videos and screenshots
*/
export function nxE2EPreset(
pathToConfig: string,
options?: NxCypressE2EPresetOptions
) {
const basePath = options?.cypressDir || 'src';
const baseConfig = {
const baseConfig: any /** Cypress.EndToEndConfigOptions */ = {
...nxBaseCypressPreset(pathToConfig),
fileServerFolder: '.',
supportFile: `${basePath}/support/e2e.ts`,
specPattern: `${basePath}/**/*.cy.{js,jsx,ts,tsx}`,
fixturesFolder: `${basePath}/fixtures`,
env: {
devServerTargets: options?.devServerTargets,
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
);

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

if (options?.bundler === 'vite') {
return {
...baseConfig,
setupNodeEvents(on) {
on('file:preprocessor', vitePreprocessor());
},
};
}
return baseConfig;
}

Expand All @@ -99,4 +144,7 @@ export type NxCypressE2EPresetOptions = {
* default is 'src'
**/
cypressDir?: string;

devServerTargets?: Record<string, string>;
ciDevServerTarget?: string;
};
Loading

1 comment on commit a86d926

@vercel
Copy link

@vercel vercel bot commented on a86d926 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.