-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrstestConfig.ts
More file actions
106 lines (91 loc) · 2.69 KB
/
Copy pathrstestConfig.ts
File metadata and controls
106 lines (91 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { ConfigParams } from '@rsbuild/core';
import type { RstestConfig, RstestConfigExport } from '@rstest/core';
import { loadRstackConfig, type Configs } from './config.ts';
const resolveAutomaticExtends = async (
configs: Configs,
params: ConfigParams,
): Promise<RstestConfig['extends'] | undefined> => {
// 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(
/* rspackChunkName: 'adapterRsbuild' */
'@rstest/adapter-rsbuild'
);
const config =
typeof appConfig === 'function' ? await appConfig(params) : appConfig;
return withRsbuildConfig({
config,
});
}
const libConfig = configs.lib;
if (libConfig) {
const { withRslibConfig } = await import(
/* rspackChunkName: 'adapterRslib' */
'@rstest/adapter-rslib'
);
const config =
typeof libConfig === 'function' ? await libConfig(params) : libConfig;
return withRslibConfig({
config,
});
}
return undefined;
};
const injectExtends = <T extends RstestConfig>(
config: T,
automaticExtends: RstestConfig['extends'],
): T => {
if (!automaticExtends || 'extends' in config) {
return config;
}
return {
...config,
extends: automaticExtends,
};
};
const extendsConfig = async (
configs: Configs,
testConfig: RstestConfig,
params: ConfigParams,
) => {
if ('extends' in testConfig) {
return testConfig;
}
if (testConfig.projects === undefined) {
const automaticExtends = await resolveAutomaticExtends(configs, params);
return injectExtends(testConfig, automaticExtends);
}
const shouldInjectProject = testConfig.projects.some(
(project) => typeof project !== 'string' && !('extends' in project),
);
if (!shouldInjectProject) {
return testConfig;
}
const automaticExtends = await resolveAutomaticExtends(configs, params);
return {
...testConfig,
projects: testConfig.projects.map((project) =>
typeof project === 'string'
? project
: injectExtends(project, automaticExtends),
),
};
};
const resolveRstestConfig = async (configs: Configs) => {
const testConfig = configs.test;
if (!testConfig) {
return {};
}
if (typeof testConfig === 'function') {
return testConfig();
}
return testConfig;
};
const loadRstestConfig = (async (params: ConfigParams) => {
const { configs } = await loadRstackConfig();
const testConfig = await resolveRstestConfig(configs);
return extendsConfig(configs, testConfig, params);
}) as RstestConfigExport;
export default loadRstestConfig;