Skip to content

Commit

Permalink
feat(testing): support vite configOverrides for cypress (#26554)
Browse files Browse the repository at this point in the history
We'd like to use shared cypress commands, but that's currently not
working because vite cannot resolve the import.
By allowing to pass options to the vite config we have the possibility
to add an alias or a plugin to fix this.

This adds support for an **optional** `viteConfigOverrides` object.

## Current Behavior
![Screenshot 2024-06-14 at 15 19
21](https://github.com/nrwl/nx/assets/951290/78f5037f-c839-4227-a3ea-4de4142006cf)

cypress.config.ts:
```ts
import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';

export default defineConfig({
  e2e: {
    ...nxE2EPreset(__filename, { cypressDir: 'cypress', bundler: 'vite' }),
  },
});
```

## Expected Behavior
![Screenshot 2024-06-14 at 15 23
56](https://github.com/nrwl/nx/assets/951290/385cde67-91b9-462e-9bfa-0c784d3947b7)

cypress.config.ts:
```ts
import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';

const viteConfigOverrides = {
  resolve: {
    alias: {
      '@cypress-shared-commands': '../../../shared-cypress-commands',
    },
  },
};

export default defineConfig({
  e2e: {
    ...nxE2EPreset(__filename, { cypressDir: 'cypress', bundler: 'vite', viteConfigOverrides }),
  },
});
```
## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
TimvdEijnden authored Jul 22, 2024
1 parent 224305c commit 764ecee
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 764ecee

Please sign in to comment.