Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): support vite configOverrides for cypress #26554

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NX_PLUGIN_OPTIONS } from '../src/utils/constants';
import { spawn } from 'child_process';
import { request as httpRequest } from 'http';
import { request as httpsRequest } from 'https';
import type { InlineConfig } from 'vite';

// Importing the cypress type here causes the angular and next unit
// tests to fail when transpiling, it seems like the cypress types are
Expand Down Expand Up @@ -133,7 +134,7 @@ export function nxE2EPreset(
config.env?.webServerCommand ?? webServerCommands?.default;

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

if (!options?.webServerCommands) {
Expand Down Expand Up @@ -265,4 +266,9 @@ export type NxCypressE2EPresetOptions = {
* Configures how the web server command is started and monitored.
*/
webServerConfig?: WebServerConfig;

/**
* Configure override inside the vite config
*/
viteConfigOverrides?: InlineConfig;
};
6 changes: 4 additions & 2 deletions packages/cypress/src/plugins/preprocessor-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const cache = new Map<string, string>();
* This preprocessor shouldn't be used directly.
* Instead, use the nxE2EPreset(__filename, { bundler: 'vite' }) function instead.
*/
function vitePreprocessor(userConfigPath?: string): CypressPreprocessor {
function vitePreprocessor(
configOverrides: InlineConfig = {}
Copy link
Member

Choose a reason for hiding this comment

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

Usages of vitePreprocessor('/path/to/vite.config.ts') will break right? Do we need to overload the function or deprecate something?

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, it's an internal API. :)

): CypressPreprocessor {
return async (file) => {
const { outputPath, filePath, shouldWatch } = file;

Expand Down Expand Up @@ -79,8 +81,8 @@ function vitePreprocessor(userConfigPath?: string): CypressPreprocessor {
>);

const watcher = (await build({
configFile: userConfigPath,
...defaultConfig,
...configOverrides,
})) as BuildResult;

return new Promise((resolve, reject) => {
Expand Down