From c935991bbc08cec0c623b42dfc722b7829a5147b Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 10 Jul 2026 13:00:51 +0800 Subject: [PATCH] fix: prefer app config for Rstest --- .../skills/rstack-cli-best-practices/SKILL.md | 2 +- packages/rstack/src/config.ts | 4 ++-- packages/rstack/src/rstestConfig.ts | 20 ++++++++++++----- .../test/config/define-app-lib/fixture.ts | 7 ++++++ .../test/config/define-app-lib/index.test.ts | 5 +++++ .../config/define-app-lib/rstack.config.ts | 22 +++++++++++++++++++ 6 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 packages/rstack/test/config/define-app-lib/fixture.ts create mode 100644 packages/rstack/test/config/define-app-lib/index.test.ts create mode 100644 packages/rstack/test/config/define-app-lib/rstack.config.ts diff --git a/.agents/skills/rstack-cli-best-practices/SKILL.md b/.agents/skills/rstack-cli-best-practices/SKILL.md index cb51213..2e8f491 100644 --- a/.agents/skills/rstack-cli-best-practices/SKILL.md +++ b/.agents/skills/rstack-cli-best-practices/SKILL.md @@ -26,7 +26,7 @@ Use `rs -h` for top-level help, and `rs -h` for command help where sup Key behavior: -- `rs test` extends `define.app` through `@rstest/adapter-rsbuild` and `define.lib` through `@rstest/adapter-rslib` unless `define.test` already sets `extends`. +- Unless `define.test` already sets `extends`, `rs test` extends `define.app` through `@rstest/adapter-rsbuild` or falls back to `define.lib` through `@rstest/adapter-rslib`. The app config takes precedence when both are defined. - `rs doc` requires the optional `@rspress/core` dependency. ## rstack.config.ts diff --git a/packages/rstack/src/config.ts b/packages/rstack/src/config.ts index 718378a..1a57dc8 100644 --- a/packages/rstack/src/config.ts +++ b/packages/rstack/src/config.ts @@ -68,8 +68,8 @@ type Define = { * * This config is used by the `rs test` command. * - * If `define.app` is also used, Rstest automatically extends the app config unless - * `extends` is set explicitly. + * Unless `extends` is set explicitly, Rstest automatically extends `define.app` or + * falls back to `define.lib`. The app config takes precedence when both are defined. */ test: (config: RstestConfigExport) => void; /** diff --git a/packages/rstack/src/rstestConfig.ts b/packages/rstack/src/rstestConfig.ts index 0025bb4..e375063 100644 --- a/packages/rstack/src/rstestConfig.ts +++ b/packages/rstack/src/rstestConfig.ts @@ -7,6 +7,8 @@ const extendsConfig = async (configs: Configs, testConfig: RstestConfig, params: return testConfig; } + // Prefer the app when both app and lib are defined. Merging both adapters can + // introduce conflicting runtime, resolve, and source transform settings. const appConfig = configs.app; if (appConfig) { const { withRsbuildConfig } = await import( @@ -15,9 +17,12 @@ const extendsConfig = async (configs: Configs, testConfig: RstestConfig, params: ); const config = typeof appConfig === 'function' ? await appConfig(params) : appConfig; - testConfig.extends = withRsbuildConfig({ - config, - }); + return { + ...testConfig, + extends: withRsbuildConfig({ + config, + }), + }; } const libConfig = configs.lib; @@ -28,9 +33,12 @@ const extendsConfig = async (configs: Configs, testConfig: RstestConfig, params: ); const config = typeof libConfig === 'function' ? await libConfig(params) : libConfig; - testConfig.extends = withRslibConfig({ - config, - }); + return { + ...testConfig, + extends: withRslibConfig({ + config, + }), + }; } return testConfig; diff --git a/packages/rstack/test/config/define-app-lib/fixture.ts b/packages/rstack/test/config/define-app-lib/fixture.ts new file mode 100644 index 0000000..1873311 --- /dev/null +++ b/packages/rstack/test/config/define-app-lib/fixture.ts @@ -0,0 +1,7 @@ +import { expect, test } from 'rstack/test'; + +declare const RSTACK_INHERITED_CONFIG: string; + +test('inherits the app config', () => { + expect(RSTACK_INHERITED_CONFIG).toBe('app'); +}); diff --git a/packages/rstack/test/config/define-app-lib/index.test.ts b/packages/rstack/test/config/define-app-lib/index.test.ts new file mode 100644 index 0000000..99f6a8a --- /dev/null +++ b/packages/rstack/test/config/define-app-lib/index.test.ts @@ -0,0 +1,5 @@ +import { test } from '#test-helpers'; + +test('should prefer define.app when app and lib are both defined', ({ execCli }) => { + execCli('test'); +}); diff --git a/packages/rstack/test/config/define-app-lib/rstack.config.ts b/packages/rstack/test/config/define-app-lib/rstack.config.ts new file mode 100644 index 0000000..9b48491 --- /dev/null +++ b/packages/rstack/test/config/define-app-lib/rstack.config.ts @@ -0,0 +1,22 @@ +import { define } from 'rstack'; + +define.app({ + source: { + define: { + RSTACK_INHERITED_CONFIG: JSON.stringify('app'), + }, + }, +}); + +define.lib({ + lib: [{}], + source: { + define: { + RSTACK_INHERITED_CONFIG: JSON.stringify('lib'), + }, + }, +}); + +define.test({ + include: ['./fixture.ts'], +});