Skip to content

Commit

Permalink
feat(testing): cypress using vite
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Dec 1, 2022
1 parent 59a6a9e commit 455c5cb
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/generated/packages/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@
"type": "boolean",
"default": false,
"hidden": true
},
"bundler": {
"description": "The Cypress builder to use.",
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which Cypress builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
2 changes: 2 additions & 0 deletions 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,6 +197,7 @@
},
"bundler": {
"description": "The Storybook builder to use.",
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
Expand Down
15 changes: 15 additions & 0 deletions packages/cypress/src/generators/cypress-project/cypress-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
toJS,
Tree,
updateJson,
workspaceRoot,
} from '@nrwl/devkit';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
Expand All @@ -36,6 +37,8 @@ import { cypressInitGenerator } from '../init/init';
// app
import { Schema } from './schema';

import { getViteConfigFileFullPath, normalizeConfigFilePath } from '@nrwl/vite';

export interface CypressProjectSchema extends Schema {
projectName: string;
projectRoot: string;
Expand All @@ -48,6 +51,16 @@ function createFiles(tree: Tree, options: CypressProjectSchema) {
const cypressFiles =
cypressVersion && cypressVersion < 10 ? 'v9-and-under' : 'v10-and-after';

// I understand that this may seem like an overkill,
// but since we're giving the user the option to set their own vite config
// file, we need to make sure that we're referencing the correct file.
// We cannot assume the file name
const projectViteConfigFile = getViteConfigFileFullPath(
tree,
options.projectName,
options.projectRoot
);

generateFiles(
tree,
join(__dirname, './files', cypressFiles),
Expand All @@ -62,6 +75,8 @@ function createFiles(tree: Tree, options: CypressProjectSchema) {
tree,
options.projectRoot
),
bundler: options.bundler,
projectViteConfigFile: projectViteConfigFile,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,18 @@ import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';

export default defineConfig({
<% if (bundler !== 'vite'){ %>
e2e: nxE2EPreset(__dirname)
});
<% } %>
<% if (bundler === 'vite'){ %>
e2e: {
...nxE2EPreset(__dirname),
setupNodeEvents(on) {
on(
'file:preprocessor',
vitePreprocessor(path.resolve(__dirname, '<%= offsetFromRoot %><%= projectViteConfigFile %>'))
);
},
}
<% } %>
});
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';
}
7 changes: 7 additions & 0 deletions packages/cypress/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
"type": "boolean",
"default": false,
"hidden": true
},
"bundler": {
"description": "The Cypress builder to use.",
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which Cypress builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"bundler": {
"description": "The Storybook builder to use.",
"type": "string",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
Expand Down
1 change: 1 addition & 0 deletions packages/storybook/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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
2 changes: 2 additions & 0 deletions packages/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './src/utils/versions';
export * from './src/utils/generator-utils';
export * from './src/utils/options-utils';
export { viteConfigurationGenerator } from './src/generators/configuration/configuration';
export { vitestGenerator } from './src/generators/vitest/vitest-generator';
23 changes: 23 additions & 0 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
TargetConfiguration,
Tree,
updateProjectConfiguration,
workspaceRoot,
writeJson,
} from '@nrwl/devkit';
import { ViteBuildExecutorOptions } from '../executors/build/schema';
import { ViteDevServerExecutorOptions } from '../executors/dev-server/schema';
import { VitestExecutorOptions } from '../executors/test/schema';
import { Schema } from '../generators/configuration/schema';
import { normalizeConfigFilePath } from './options-utils';

/**
* This function is used to find the build and serve targets for
Expand Down Expand Up @@ -459,3 +461,24 @@ ${options.includeVitest ? '/// <reference types="vitest" />' : ''}

tree.write(viteConfigPath, viteConfigContent);
}

export function getViteConfigFileFullPath(
tree: Tree,
projectName?: string,
projectRoot?: string
): string | undefined {
if (projectName) {
const projectConfig = readProjectConfiguration(tree, projectName);
const serveTarget = findExistingTargets(projectConfig.targets).serveTarget;
const projectViteConfigFileName =
projectConfig.targets[serveTarget].options.configFile;
return normalizeConfigFilePath(
projectViteConfigFileName,
workspaceRoot,
projectConfig.root
);
} else if (projectRoot) {
return normalizeConfigFilePath(undefined, workspaceRoot, projectRoot);
}
return undefined;
}

0 comments on commit 455c5cb

Please sign in to comment.