Skip to content

Commit 1dcbc24

Browse files
authored
feat(test): load project configs through Rstest PROJECT_LOADER (#600)
1 parent 0043c0e commit 1dcbc24

17 files changed

Lines changed: 158 additions & 34 deletions

File tree

‎packages/rstack/rstack.config.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { define } from 'rstack';
44

55
define.test(() => {
66
return {
7-
// Temporary projects may contain files that match Rstest's test glob.
8-
exclude: ['**/test-temp-*/**'],
7+
// Temporary projects and fixture projects may contain files that match
8+
// Rstest's test glob.
9+
exclude: [
10+
'**/test-temp-*/**',
11+
// Project-loader fixtures run the CLI with their own test globs.
12+
'**/tests/config/project-loader/packages/**',
13+
],
914
extends: withRslibConfig(),
1015
testTimeout: 30_000,
1116
source: {

‎packages/rstack/src/config.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export type LoadRstackConfigOptions = {
8484
/**
8585
* The path to the Rstack config file, can be a relative or absolute path.
8686
* A relative path is resolved from `cwd`.
87-
* If `configFilePath` is not provided, the config path set by the CLI is used.
87+
* If `configFilePath` is omitted, the config path set by the CLI is used.
8888
* If neither path is provided, the function will search for the config file in `cwd`.
89+
* Pass `null` to search `cwd` and ignore the CLI config path.
8990
*/
90-
configFilePath?: string;
91+
configFilePath?: string | null;
9192
/**
9293
* The directory the config file is searched in and relative config paths are resolved from.
9394
* Defaults to the current working directory.
@@ -269,7 +270,8 @@ export const loadRstackConfig = async ({
269270
cwd,
270271
}: LoadRstackConfigOptions = {}): Promise<LoadedRstackConfig> => {
271272
const state = getConfigState();
272-
const configPath = configFilePath ?? state.configPath;
273+
const configPath =
274+
configFilePath === null ? undefined : (configFilePath ?? state.configPath);
273275
const session: ConfigSession = {
274276
configs: {},
275277
active: true,

‎packages/rstack/src/rstestConfig.ts‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import { normalize } from 'node:path';
12
import type { ConfigParams } from '@rsbuild/core';
23
import {
34
type RstestConfig,
45
type RstestConfigExport,
56
mergeRstestConfig,
67
} from '@rstest/core';
8+
import { type LoadProjectConfig, PROJECT_LOADER } from '@rstest/core/api';
79
import { loadRstackConfig, type RstackConfigDefinitions } from './config.ts';
810
import { resolveConfigLayers } from './configLayers.ts';
911

1012
const resolveAutomaticExtends = async (
1113
layers: readonly RstackConfigDefinitions[],
1214
params: ConfigParams,
15+
cwd?: string,
1316
): Promise<RstestConfig['extends'] | undefined> => {
1417
// Prefer the app when both app and lib are defined. Merging both adapters can
1518
// introduce conflicting runtime, resolve, and source transform settings.
@@ -34,6 +37,10 @@ const resolveAutomaticExtends = async (
3437

3538
return withRslibConfig({
3639
config: await resolveRslibConfig(layers, params),
40+
// The adapter reads the project's tsconfig for decorator detection. It
41+
// falls back to `process.cwd()`, so external projects need their own
42+
// directory here or they would inherit the root tsconfig.
43+
cwd,
3744
});
3845
}
3946

@@ -58,13 +65,14 @@ const extendsConfig = async (
5865
layers: readonly RstackConfigDefinitions[],
5966
testConfig: RstestConfig,
6067
params: ConfigParams,
68+
cwd?: string,
6169
) => {
6270
if ('extends' in testConfig) {
6371
return testConfig;
6472
}
6573

6674
if (testConfig.projects === undefined) {
67-
const automaticExtends = await resolveAutomaticExtends(layers, params);
75+
const automaticExtends = await resolveAutomaticExtends(layers, params, cwd);
6876
return injectExtends(testConfig, automaticExtends);
6977
}
7078

@@ -75,7 +83,7 @@ const extendsConfig = async (
7583
return testConfig;
7684
}
7785

78-
const automaticExtends = await resolveAutomaticExtends(layers, params);
86+
const automaticExtends = await resolveAutomaticExtends(layers, params, cwd);
7987

8088
return {
8189
...testConfig,
@@ -99,14 +107,45 @@ export const mergeRstestConfigLayers = async (
99107
export const resolveRstestConfig = async (
100108
layers: readonly RstackConfigDefinitions[],
101109
params: ConfigParams,
110+
cwd?: string,
102111
): Promise<RstestConfig> => {
103112
const testConfig = await mergeRstestConfigLayers(layers);
104-
return extendsConfig(layers, testConfig, params);
113+
return extendsConfig(layers, testConfig, params, cwd);
105114
};
106115

116+
// Rstest reads the host loader only from the root config and calls it for every
117+
// directory or file a string `projects` entry expands to. Rstack does not load
118+
// native tool configs anywhere else, so a project without an Rstack config
119+
// becomes an empty project instead of falling back to `rstest.config.*`.
120+
const createProjectLoader =
121+
(params: ConfigParams): LoadProjectConfig =>
122+
async ({ dirPath, filePath }) => {
123+
// `null` searches the project directory with the default config file names
124+
// instead of reusing the `rs --config` path that loads the root config.
125+
const loaded = await loadRstackConfig({
126+
cwd: dirPath,
127+
configFilePath: filePath ?? null,
128+
});
129+
if (loaded.filePath === null) {
130+
return null;
131+
}
132+
133+
return {
134+
content: await resolveRstestConfig([loaded.configs], params, dirPath),
135+
// Rstest dedupes projects by this string. Directory and file entries can
136+
// reach the same file with different path separators, so normalize.
137+
filePath: normalize(loaded.filePath),
138+
dependencies: loaded.dependencies,
139+
};
140+
};
141+
107142
const loadRstestConfig = (async (params: ConfigParams) => {
108143
const { configs } = await loadRstackConfig();
109-
return resolveRstestConfig([configs], params);
144+
const config = await resolveRstestConfig([configs], params);
145+
return {
146+
...config,
147+
[PROJECT_LOADER]: createProjectLoader(params),
148+
};
110149
}) as RstestConfigExport;
111150

112151
export default loadRstestConfig;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from '#test-helpers';
2+
3+
test('should load each project from its own Rstack config', ({ execCli }) => {
4+
execCli('test');
5+
});
6+
7+
test('should load file project entries from their Rstack configs', ({
8+
execCli,
9+
}) => {
10+
execCli('test --config rstack.file-entries.config.ts');
11+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from 'rstack/test';
2+
3+
declare const PROJECT_LABEL: string;
4+
5+
test('should inherit the package app and test config', () => {
6+
expect(PROJECT_LABEL).toBe('pkg-a');
7+
expect(typeof (globalThis as Record<string, unknown>).describe).toBe(
8+
'function',
9+
);
10+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file must not be collected: the package config's `include` only matches
2+
// `entry.ts`, so importing it means the project config was not applied.
3+
throw new Error('pkg-a include was not applied');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { define } from 'rstack';
2+
3+
define.app({
4+
source: {
5+
define: {
6+
PROJECT_LABEL: JSON.stringify('pkg-a'),
7+
},
8+
},
9+
});
10+
11+
define.test({
12+
globals: true,
13+
include: ['./entry.ts'],
14+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'rstack/test';
2+
3+
test('should use the default config of an empty project', () => {
4+
expect(typeof (globalThis as Record<string, unknown>).describe).toBe(
5+
'undefined',
6+
);
7+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Rstack does not fall back to native tool config files. This file must never
2+
// be loaded for a project.
3+
throw new Error('rstest.config.ts must not be loaded by Rstack');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { define } from 'rstack';
2+
3+
define.test({
4+
projects: ['packages/*'],
5+
});

0 commit comments

Comments
 (0)