diff --git a/e2e/projects/fixtures/packages/client/rstest.config.ts b/e2e/projects/fixtures/packages/client/rstest.config.ts index 379b67d2e..30351a3c2 100644 --- a/e2e/projects/fixtures/packages/client/rstest.config.ts +++ b/e2e/projects/fixtures/packages/client/rstest.config.ts @@ -4,6 +4,7 @@ import rsbuildConfig from './rsbuild.config'; export default defineProject({ projects: [ { + root: __dirname, ...(rsbuildConfig as RstestConfig), name: 'client-jsdom', testEnvironment: 'jsdom', @@ -11,6 +12,7 @@ export default defineProject({ exclude: ['test/node.test.ts'], }, { + root: __dirname, name: 'client-node', include: ['test/node.test.ts'], }, diff --git a/e2e/projects/index.test.ts b/e2e/projects/index.test.ts index 937a8ab75..7475adc0c 100644 --- a/e2e/projects/index.test.ts +++ b/e2e/projects/index.test.ts @@ -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', diff --git a/examples/node/rstest.config.ts b/examples/node/rstest.config.ts index 9ee3cbabc..af9f402ec 100644 --- a/examples/node/rstest.config.ts +++ b/examples/node/rstest.config.ts @@ -1,3 +1,5 @@ import { defineConfig } from '@rstest/core'; -export default defineConfig({}); +export default defineConfig({ + root: __dirname, +}); diff --git a/packages/core/src/cli/init.ts b/packages/core/src/cli/init.ts index 48ba8314e..0ee61ee34 100644 --- a/packages/core/src/cli/init.ts +++ b/packages/core/src/cli/init.ts @@ -104,19 +104,25 @@ function mergeWithCLIOptions( } async function resolveConfig( - options: CommonOptions & Required>, + 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; + } + return { - config: mergeWithCLIOptions(config, options), + config: mergedConfig, configFilePath: configFilePath ?? undefined, }; } @@ -219,7 +225,7 @@ export async function resolveProjects({ const { config, configFilePath } = await resolveConfig({ ...options, config: isDirectory ? undefined : project, - root: projectRoot, + cwd: projectRoot, }); if (configFilePath) { @@ -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 });