From 1bfecfe5b969d153ec59338ba20bbf9b43d9fddc Mon Sep 17 00:00:00 2001 From: Tim van den Eijnden Date: Fri, 14 Jun 2024 14:32:12 +0200 Subject: [PATCH] feat(testing): support vite configOverrides for cypress --- packages/cypress/plugins/cypress-preset.ts | 8 +++++++- packages/cypress/src/plugins/preprocessor-vite.ts | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/cypress/plugins/cypress-preset.ts b/packages/cypress/plugins/cypress-preset.ts index 09ce7e5f8f87e..31ee3da34f453 100644 --- a/packages/cypress/plugins/cypress-preset.ts +++ b/packages/cypress/plugins/cypress-preset.ts @@ -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 @@ -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) { @@ -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; }; diff --git a/packages/cypress/src/plugins/preprocessor-vite.ts b/packages/cypress/src/plugins/preprocessor-vite.ts index 4cacbfcfc6780..93da61cf35d41 100644 --- a/packages/cypress/src/plugins/preprocessor-vite.ts +++ b/packages/cypress/src/plugins/preprocessor-vite.ts @@ -40,7 +40,9 @@ const cache = new Map(); * 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 = {} +): CypressPreprocessor { return async (file) => { const { outputPath, filePath, shouldWatch } = file; @@ -79,8 +81,8 @@ function vitePreprocessor(userConfigPath?: string): CypressPreprocessor { >); const watcher = (await build({ - configFile: userConfigPath, ...defaultConfig, + ...configOverrides, })) as BuildResult; return new Promise((resolve, reject) => {