Skip to content

Commit

Permalink
fix(testing): fix react CT w/ vite and dependant projects
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Apr 21, 2023
1 parent 564ffae commit 1263e71
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/react/plugins/component-testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import type { CypressExecutorOptions } from '@nx/cypress/src/executors/cypress/cypress.impl';
import {
ExecutorContext,
joinPathFragments,
logger,
parseTargetString,
ProjectGraph,
Expand All @@ -19,7 +20,8 @@ import {
getProjectConfigByPath,
} from '@nx/cypress/src/utils/ct-helpers';

import type { Configuration } from 'webpack';
import { existsSync, lstatSync } from 'fs';
import { dirname, join } from 'path';
type ViteDevServer = {
framework: 'react';
bundler: 'vite';
Expand Down Expand Up @@ -61,11 +63,38 @@ export function nxComponentTestingPreset(
chromeWebSecurity: boolean;
} {
if (options?.bundler === 'vite') {
const { searchForWorkspaceRoot } = require('vite');
return {
...nxBaseCypressPreset(pathToConfig),
specPattern: 'src/**/*.cy.{js,jsx,ts,tsx}',
devServer: {
...({ framework: 'react', bundler: 'vite' } as const),
viteConfig: async () => {
const normalizedPath = lstatSync(pathToConfig).isDirectory()
? pathToConfig
: dirname(pathToConfig);
const viteConfigPath = findViteConfig(normalizedPath);
const { mergeConfig, loadConfigFromFile } = (await import(
'vite'
)) as typeof import('vite');
const resolved = await loadConfigFromFile(
{
mode: 'watch',
command: 'serve',
},
viteConfigPath
);
return mergeConfig(resolved.config, {
server: {
fs: {
allow: [
workspaceRoot,
joinPathFragments(workspaceRoot, 'node_modules/vite'),
],
},
},
});
},
},
};
}
Expand Down Expand Up @@ -232,3 +261,12 @@ function buildTargetWebpack(
};
}
}
function findViteConfig(projectRootFullPath: string): string {
const allowsExt = ['js', 'mjs', 'ts', 'cjs', 'mts', 'cts'];

for (const ext of allowsExt) {
if (existsSync(join(projectRootFullPath, `vite.config.${ext}`))) {
return join(projectRootFullPath, `vite.config.${ext}`);
}
}
}

0 comments on commit 1263e71

Please sign in to comment.