Skip to content

Commit

Permalink
feat(vite): cypress vite generators
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Dec 13, 2022
1 parent b3ff11f commit a888d85
Show file tree
Hide file tree
Showing 32 changed files with 663 additions and 94 deletions.
13 changes: 13 additions & 0 deletions docs/generated/packages/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
},
"bundler": {
"description": "The Cypress bundler to use.",
"type": "string",
"enum": ["vite", "webpack", "none"],
"x-prompt": "Which Cypress bundler do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/react.json
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,12 @@
"type": "boolean",
"description": "Skip formatting files",
"default": false
},
"bundler": {
"description": "The bundler to use for Cypress Component Testing.",
"type": "string",
"enum": ["vite", "webpack"],
"hidden": true
}
},
"required": ["project"],
Expand Down
6 changes: 5 additions & 1 deletion docs/generated/packages/storybook.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
},
"bundler": {
"description": "The bundler to use.",
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which bundler do you want to use?",
"default": "webpack"
Expand Down Expand Up @@ -196,7 +197,10 @@
},
"bundler": {
"description": "The Storybook builder to use.",
"enum": ["vite", "webpack"]
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ 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 { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: nxE2EPreset(__dirname)
});"
`;

exports[`e2e migrator cypress with project root at "" cypress version >=10 should keep paths in the e2e config when they differ from the nx preset defaults 1`] = `
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
Expand Down Expand Up @@ -41,11 +50,11 @@ exports[`e2e migrator cypress with project root at "" cypress version >=10 shoul
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: {...nxE2EPreset(__dirname),
baseUrl: 'http://localhost:4200'
},
});"
export default defineConfig({
e2e: {...nxE2EPreset(__dirname),
baseUrl: 'http://localhost:4200'
},
});"
`;

exports[`e2e migrator cypress with project root at "" cypress version >=10 should update paths in the config 1`] = `
Expand Down Expand Up @@ -79,6 +88,15 @@ 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 { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: nxE2EPreset(__dirname)
});"
`;

exports[`e2e migrator cypress with project root at "projects/app1" cypress version >=10 should keep paths in the e2e config when they differ from the nx preset defaults 1`] = `
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
Expand Down Expand Up @@ -107,11 +125,11 @@ exports[`e2e migrator cypress with project root at "projects/app1" cypress versi
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: {...nxE2EPreset(__dirname),
baseUrl: 'http://localhost:4200'
},
});"
export default defineConfig({
e2e: {...nxE2EPreset(__dirname),
baseUrl: 'http://localhost:4200'
},
});"
`;

exports[`e2e migrator cypress with project root at "projects/app1" cypress version >=10 should update paths in the config 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,8 @@ describe('e2e migrator', () => {
'apps/app1-e2e/cypress.config.ts',
'utf-8'
);
expect(cypressConfig).toBe(`import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';

export default defineConfig({
e2e: nxE2EPreset(__dirname)
});
`);
expect(cypressConfig).toMatchSnapshot();
});

it('should update e2e config with the nx preset', async () => {
Expand All @@ -856,11 +851,11 @@ export default defineConfig({
joinPathFragments(root, 'cypress.config.ts'),
`import { defineConfig } from 'cypress';
export default defineConfig({
e2e: {
baseUrl: 'http://localhost:4200'
},
});`
export default defineConfig({
e2e: {
baseUrl: 'http://localhost:4200'
},
});`
);
const project = addProject('app1', {
root,
Expand Down
1 change: 1 addition & 0 deletions packages/cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ts-loader": "^9.3.1",
"tsconfig-paths-webpack-plugin": "3.5.2",
"tslib": "^2.3.0",
"vite": "^3.2.3",
"webpack": "^4 || ^5",
"webpack-node-externals": "^3.0.0"
},
Expand Down
23 changes: 20 additions & 3 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { workspaceRoot } from '@nrwl/devkit';
import { dirname, join, relative } from 'path';
import { lstatSync } from 'fs';

import vitePreprocessor from '../src/plugins/preprocessor-vite';

interface BaseCypressPreset {
videosFolder: string;
screenshotsFolder: string;
Expand All @@ -15,8 +17,10 @@ export interface NxComponentTestingOptions {
* this is only when customized away from the default value of `component-test`
* @example 'component-test'
*/
ctTargetName: string;
ctTargetName?: string;
bundler?: 'vite' | 'webpack';
}

export function nxBaseCypressPreset(pathToConfig: string): BaseCypressPreset {
// prevent from placing path outside the root of the workspace
// if they pass in a file or directory
Expand Down Expand Up @@ -58,12 +62,25 @@ export function nxBaseCypressPreset(pathToConfig: string): BaseCypressPreset {
*
* @param pathToConfig will be used to construct the output paths for videos and screenshots
*/
export function nxE2EPreset(pathToConfig: string) {
return {
export function nxE2EPreset(
pathToConfig: string,
options?: { bundler?: string }
) {
const baseConfig = {
...nxBaseCypressPreset(pathToConfig),
fileServerFolder: '.',
supportFile: 'src/support/e2e.ts',
specPattern: 'src/**/*.cy.{js,jsx,ts,tsx}',
fixturesFolder: 'src/fixtures',
};

if (options?.bundler === 'vite') {
return {
...baseConfig,
setupNodeEvents(on) {
on('file:preprocessor', vitePreprocessor());
},
};
}
return baseConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,26 @@ Object {
}
`;
exports[`Cypress Project > v10 for bundler:vite should pass the bundler info to nxE2EPreset in \`cypress.config.ts\` 1`] = `
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: nxE2EPreset(__dirname,
{
bundler: 'vite'
}
)
});"
`;
exports[`Cypress Project > v10 nested should set right path names in \`cypress.config.ts\` 1`] = `
"import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: nxE2EPreset(__dirname)
});
"
});"
`;
exports[`Cypress Project > v10 nested should set right path names in \`tsconfig.e2e.json\` 1`] = `
Expand Down Expand Up @@ -243,8 +255,7 @@ import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: nxE2EPreset(__dirname)
});
"
});"
`;
exports[`Cypress Project > v10 should set right path names in \`tsconfig.e2e.json\` 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ describe('Cypress Project', () => {
expect(tsConfig.extends).toBe('../../tsconfig.json');
});

describe('for bundler:vite', () => {
it('should pass the bundler info to nxE2EPreset in `cypress.config.ts`', async () => {
await cypressProjectGenerator(tree, {
...defaultOptions,
name: 'my-app-e2e',
project: 'my-app',
bundler: 'vite',
});
const cypressConfig = tree.read(
'apps/my-app-e2e/cypress.config.ts',
'utf-8'
);
expect(cypressConfig).toMatchSnapshot();
});
});

describe('nested', () => {
it('should set right path names in `cypress.config.ts`', async () => {
await cypressProjectGenerator(tree, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
extractLayoutDirectory,
formatFiles,
generateFiles,
getProjects,
getWorkspaceLayout,
joinPathFragments,
logger,
Expand All @@ -16,7 +17,6 @@ import {
toJS,
Tree,
updateJson,
getProjects,
} from '@nrwl/devkit';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
Expand All @@ -32,6 +32,7 @@ import { filePathPrefix } from '../../utils/project-name';
import {
cypressVersion,
eslintPluginCypressVersion,
viteVersion,
} from '../../utils/versions';
import { cypressInitGenerator } from '../init/init';
// app
Expand Down Expand Up @@ -63,6 +64,7 @@ function createFiles(tree: Tree, options: CypressProjectSchema) {
tree,
options.projectRoot
),
bundler: options.bundler,
}
);

Expand Down Expand Up @@ -271,6 +273,19 @@ export async function cypressProjectGenerator(host: Tree, schema: Schema) {
if (!cypressVersion) {
tasks.push(cypressInitGenerator(host, options));
}

if (schema.bundler === 'vite') {
tasks.push(
addDependenciesToPackageJson(
host,
{},
{
vite: viteVersion,
}
)
);
}

createFiles(host, options);
addProject(host, options);
const installTask = await addLinter(host, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';

export default defineConfig({
e2e: nxE2EPreset(__dirname)
});
e2e: nxE2EPreset(__dirname<% if (bundler === 'vite'){ %>,
{
bundler: 'vite'
}
<% } %>)
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface Schema {
standaloneConfig?: boolean;
skipPackageJson?: boolean;
rootProject?: boolean;
bundler?: 'webpack' | 'vite' | 'none';
}
13 changes: 13 additions & 0 deletions packages/cypress/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
},
"bundler": {
"description": "The Cypress bundler to use.",
"type": "string",
"enum": ["vite", "webpack", "none"],
"x-prompt": "Which Cypress bundler do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
Loading

0 comments on commit a888d85

Please sign in to comment.