Skip to content

Commit 77e8e67

Browse files
committed
refactor(config): merge test config layers with Rstest
1 parent bd374a6 commit 77e8e67

2 files changed

Lines changed: 51 additions & 11 deletions

File tree

‎packages/rstack/src/rstestConfig.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { ConfigParams } from '@rsbuild/core';
2-
import type { RstestConfig, RstestConfigExport } from '@rstest/core';
2+
import {
3+
type RstestConfig,
4+
type RstestConfigExport,
5+
mergeRstestConfig,
6+
} from '@rstest/core';
37
import { loadRstackConfig, type Configs } from './config.ts';
8+
import { resolveConfigLayers } from './configLayers.ts';
49

510
const resolveAutomaticExtends = async (
611
configs: Configs,
@@ -86,20 +91,18 @@ const extendsConfig = async (
8691
};
8792
};
8893

89-
const resolveRstestConfig = async (configs: Configs) => {
90-
const testConfig = configs.test;
91-
if (!testConfig) {
92-
return {};
93-
}
94-
if (typeof testConfig === 'function') {
95-
return testConfig();
96-
}
97-
return testConfig;
94+
export const resolveRstestConfig = async (
95+
layers: readonly Configs[],
96+
): Promise<RstestConfig> => {
97+
const configs = await resolveConfigLayers(layers, 'test');
98+
return configs.length > 1
99+
? mergeRstestConfig(...configs)
100+
: (configs[0] ?? {});
98101
};
99102

100103
const loadRstestConfig = (async (params: ConfigParams) => {
101104
const { configs } = await loadRstackConfig();
102-
const testConfig = await resolveRstestConfig(configs);
105+
const testConfig = await resolveRstestConfig([configs]);
103106
return extendsConfig(configs, testConfig, params);
104107
}) as RstestConfigExport;
105108

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createRequire } from 'node:module';
2+
import { expect, test } from 'rstack/test';
3+
4+
// Bypass Rstest's replacement of @rstest/core with its test runtime APIs.
5+
const { resolveRstestConfig } = createRequire(import.meta.url)(
6+
'../../dist/rstestConfig.js',
7+
) as typeof import('../../src/rstestConfig.ts');
8+
9+
test('merges test layers using native Rstest rules', async () => {
10+
const config = await resolveRstestConfig([
11+
{
12+
test: {
13+
extends: { testTimeout: 5000 },
14+
retry: 1,
15+
setupFiles: ['./base.ts'],
16+
reporters: ['default'],
17+
projects: ['./base.config.ts'],
18+
},
19+
},
20+
{
21+
test: {
22+
retry: 2,
23+
setupFiles: ['./project.ts'],
24+
reporters: ['dot'],
25+
projects: ['./project.config.ts'],
26+
},
27+
},
28+
]);
29+
30+
expect(config).toMatchObject({
31+
extends: { testTimeout: 5000 },
32+
retry: 2,
33+
setupFiles: ['./base.ts', './project.ts'],
34+
reporters: ['dot'],
35+
projects: ['./base.config.ts', './project.config.ts'],
36+
});
37+
});

0 commit comments

Comments
 (0)