Skip to content
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
2 changes: 2 additions & 0 deletions e2e/projects/fixtures/packages/client/rstest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import rsbuildConfig from './rsbuild.config';
export default defineProject({
projects: [
{
root: __dirname,
...(rsbuildConfig as RstestConfig),
name: 'client-jsdom',
testEnvironment: 'jsdom',
setupFiles: ['./test/setup.ts'],
exclude: ['test/node.test.ts'],
},
{
root: __dirname,
name: 'client-node',
include: ['test/node.test.ts'],
},
Expand Down
27 changes: 27 additions & 0 deletions e2e/projects/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ describe('test projects', () => {
}, 15_000);
});

it('should run project correctly with specified config root', async () => {
const { cli, expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: ['run', '--globals', '-c', 'packages/client/rstest.config.ts'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
},
},
});

await expectExecSuccess();
const logs = cli.stdout.split('\n').filter(Boolean);

// test log print
// should only run client project
expect(
logs.find((log) => log.includes('packages/node/test/index.test.ts')),
).toBeFalsy();
expect(
logs.find((log) => log.includes('packages/client/test/App.test.tsx')),
).toBeTruthy();
expect(
logs.find((log) => log.includes('packages/client/test/node.test.ts')),
).toBeTruthy();
});

it('should run projects fail when project not found', async () => {
const { expectExecFailed, expectStderrLog } = await runRstestCli({
command: 'rstest',
Expand Down
4 changes: 3 additions & 1 deletion examples/node/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({});
export default defineConfig({
root: __dirname,
});
16 changes: 11 additions & 5 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,25 @@ function mergeWithCLIOptions(
}

async function resolveConfig(
options: CommonOptions & Required<Pick<CommonOptions, 'root'>>,
options: CommonOptions & { cwd: string },
): Promise<{
config: RstestConfig;
configFilePath?: string;
}> {
const { content: config, filePath: configFilePath } = await loadConfig({
cwd: options.root,
cwd: options.cwd,
path: options.config,
configLoader: options.configLoader,
});

const mergedConfig = mergeWithCLIOptions(config, options);

if (!mergedConfig.root) {
mergedConfig.root = options.cwd;
}
Comment on lines +118 to +122
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The mergeWithCLIOptions function includes 'root' in its keys array (line 52), which causes it to override config.root with options.root when the --root CLI flag is provided. This defeats the purpose of this PR, which is to respect the config file's root setting.

The issue: when options.root is provided via CLI (e.g., --root packages/client), it's still a relative string that gets copied into config.root, potentially overriding an absolute path like __dirname from the config. Later, in RstestCore, this relative path would be resolved using the wrong base, resulting in incorrect paths like /monorepo/packages/client/packages/client.

The solution: Remove 'root' from the keys array in mergeWithCLIOptions (line 52). This way:

  • If the config has a root, it's preserved
  • If the config doesn't have a root, the fallback at lines 120-122 sets it to options.cwd
  • The CLI --root only affects where to look for the config file (via options.cwd), not the final config.root

This aligns with the PR's intent to respect the config file's root setting while allowing --root to control where configs are loaded from.

Copilot uses AI. Check for mistakes.

return {
config: mergeWithCLIOptions(config, options),
config: mergedConfig,
configFilePath: configFilePath ?? undefined,
};
}
Expand Down Expand Up @@ -219,7 +225,7 @@ export async function resolveProjects({
const { config, configFilePath } = await resolveConfig({
...options,
config: isDirectory ? undefined : project,
root: projectRoot,
cwd: projectRoot,
});

if (configFilePath) {
Expand Down Expand Up @@ -290,7 +296,7 @@ export async function initCli(options: CommonOptions): Promise<{

const { config, configFilePath } = await resolveConfig({
...options,
root,
cwd: options.root ? getAbsolutePath(cwd, options.root) : cwd,
});

const projects = await resolveProjects({ config, root, options });
Expand Down
Loading