Skip to content

Commit

Permalink
feat(testing): support vite configOverrides for cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdEijnden authored and Coly010 committed Jul 22, 2024
1 parent 199ffd1 commit 1bfecfe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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 = {}
): 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

0 comments on commit 1bfecfe

Please sign in to comment.