Skip to content

Commit 5b8b393

Browse files
committed
Type tests runner refactoring
1 parent ae2c718 commit 5b8b393

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

tests/type-definitions/runner.mjs

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ const LIBS = [
3737
'dom',
3838
// null, // fails on web types
3939
];
40-
const EXCLUDE_RULES = {
40+
const TARGET_RULES = {
4141
es6: ['**/*es2018*test.ts'],
42+
};
43+
44+
const LIB_RULES = {
4245
dom: ['**/*dom*test.ts'],
4346
};
4447

@@ -50,58 +53,70 @@ function getEnvPath(env) {
5053
return path.join(TMP_DIR, env.replaceAll('/', '-').replaceAll('@', ''));
5154
}
5255

53-
async function runTestsOnEnv({ typeScriptVersion, target, type, env, lib }) {
56+
async function runLimited(tasks, limit) {
57+
let i = 0;
58+
async function worker() {
59+
while (i < tasks.length) {
60+
const idx = i++;
61+
await runTask(tasks[idx]);
62+
}
63+
}
64+
await Promise.all(Array.from({ length: limit }, worker));
65+
}
66+
67+
async function runTask(config) {
5468
$.verbose = false;
55-
const envLibName = env ? env.substring(0, env.lastIndexOf('@')) : '';
56-
let tsConfigPostfix = EXCLUDE_RULES[target] ? `.${ target }` : '';
57-
tsConfigPostfix = lib && EXCLUDE_RULES[lib] ? `${ tsConfigPostfix }.${ lib }` : tsConfigPostfix;
58-
const command = `npx -p typescript@${ typeScriptVersion }${
59-
env ? ` -p ${ env }` : '' } tsc -p ${ type }/tsconfig${ tsConfigPostfix }.json --target ${ target } --lib ${ target }${ lib ? `,${ lib }` : '' }${
60-
env ? ` --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }` : '' }`;
61-
echo(`$ ${ command }`);
69+
const command = `$ ${ config.cmd } ${ config.args.join(' ') }`;
6270
try {
6371
tested++;
64-
if (env && lib) {
65-
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }${ tsConfigPostfix }.json --target ${ target } --lib ${ target },${ lib } --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }`.quiet();
66-
} else if (env) {
67-
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }${ tsConfigPostfix }.json --target ${ target } --lib ${ target } --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }`.quiet();
68-
} else if (lib) {
69-
await $`npx -p typescript@${ typeScriptVersion } tsc -p ${ type }/tsconfig${ tsConfigPostfix }.json --target ${ target } --lib ${ target },${ lib }`.quiet();
72+
echo(command);
73+
if (config.cwd) {
74+
await $({ cwd: config.cwd })`${ config.cmd } ${ config.args }`.quiet();
7075
} else {
71-
await $`npx -p typescript@${ typeScriptVersion } tsc -p ${ type }/tsconfig${ tsConfigPostfix }.json --target ${ target } --lib ${ target }`.quiet();
76+
await $`${ config.cmd } ${ config.args }`.quiet();
7277
}
73-
echo(chalk.green(`$ ${ command }`));
78+
echo(chalk.green(command));
7479
} catch (error) {
7580
failed++;
76-
echo(`$ ${ chalk.red(command) }\n ${ error }`);
81+
echo(chalk.red(`${ command }\n ${ error }`));
7782
}
7883
}
7984

80-
async function runLimited(configs, limit) {
81-
let i = 0;
82-
async function worker() {
83-
while (i < configs.length) {
84-
const idx = i++;
85-
await runTestsOnEnv(configs[idx]);
86-
}
87-
}
88-
await Promise.all(Array.from({ length: limit }, worker));
89-
}
90-
91-
function buildTasksConfigs(types, targets, typeScriptVersions, envs, libs) {
92-
const taskConfigs = [];
85+
function buildTasks(types, targets, typeScriptVersions, envs, libs) {
86+
const tasks = [];
9387
for (const type of types) {
9488
for (const target of targets) {
9589
for (const typeScriptVersion of typeScriptVersions) {
9690
for (const env of envs) {
9791
for (const lib of libs) {
98-
taskConfigs.push({ env, lib, target, type, typeScriptVersion });
92+
let tsConfigPostfix = TARGET_RULES[target] ? `.${ target }` : '';
93+
tsConfigPostfix += lib && LIB_RULES[lib] ? `.${ lib }` : '';
94+
const libsStr = lib ? `${ target },${ lib }` : target;
95+
const tsConfigPath = env ? `./tsconfig.${ type }${ tsConfigPostfix }.json` : `${ type }/tsconfig${ tsConfigPostfix }.json`;
96+
const taskConfig = {
97+
cmd: 'npx',
98+
cwd: getEnvPath(env),
99+
args: [
100+
'-p', `typescript@${ typeScriptVersion }`,
101+
'tsc',
102+
'-p', tsConfigPath,
103+
'--target', target,
104+
'--lib', `${ libsStr }`,
105+
],
106+
};
107+
// eslint-disable-next-line max-depth -- it's needed here
108+
if (type) {
109+
const typesSuffix = type === 'pure' ? '/pure' : '';
110+
const envLibName = env ? `,${ env.substring(0, env.lastIndexOf('@')) }` : '';
111+
taskConfig.args.push('--types', `@core-js/types${ typesSuffix }${ envLibName }`);
112+
}
113+
tasks.push(taskConfig);
99114
}
100115
}
101116
}
102117
}
103118
}
104-
return taskConfigs;
119+
return tasks;
105120
}
106121

107122
async function clearTmpDir() {
@@ -120,7 +135,7 @@ async function prepareEnvironment(environments, coreJsTypes) {
120135
await writeJson(path.join(tmpEnvDir, `tsconfig.${ type }.json`), {
121136
extends: '../../tsconfig.json',
122137
include: [`../../${ type }/**/*.ts`],
123-
exclude: [`../../${ type }/**/${ EXCLUDE_RULES.dom }`],
138+
exclude: [`../../${ type }/**/${ LIB_RULES.dom }`],
124139
});
125140
await writeJson(path.join(tmpEnvDir, `tsconfig.${ type }.dom.json`), {
126141
extends: '../../tsconfig.json',
@@ -129,32 +144,35 @@ async function prepareEnvironment(environments, coreJsTypes) {
129144
await writeJson(path.join(tmpEnvDir, `tsconfig.${ type }.es6.json`), {
130145
extends: '../../tsconfig.json',
131146
include: [`../../${ type }/**/*.ts`],
132-
exclude: [`../../${ type }/**/${ EXCLUDE_RULES.es6 }`, `../../${ type }/${ EXCLUDE_RULES.dom }`],
147+
exclude: [`../../${ type }/**/${ TARGET_RULES.es6 }`, `../../${ type }/${ LIB_RULES.dom }`],
133148
});
134149
await writeJson(path.join(tmpEnvDir, `tsconfig.${ type }.es6.dom.json`), {
135150
extends: '../../tsconfig.json',
136151
include: [`../../${ type }/**/*.ts`],
137-
exclude: [`../../${ type }/**/${ EXCLUDE_RULES.es6 }`],
152+
exclude: [`../../${ type }/**/${ TARGET_RULES.es6 }`],
138153
});
139154
}
140155
}
141156
}
142157

143-
await $`npx -p typescript@5.9 tsc`;
144-
await $`npx -p typescript@5.9 tsc -p templates/tsconfig.json`;
145-
await $`npx -p typescript@5.9 -p @types/node@24 tsc -p templates/tsconfig.require.json`;
158+
let tasks = [];
159+
tasks.push(
160+
{ cmd: 'npx', args: ['-p', 'typescript@5.9', 'tsc'] },
161+
{ cmd: 'npx', args: ['-p', 'typescript@5.9', 'tsc', '-p', 'templates/tsconfig.json'] },
162+
{ cmd: 'npx', args: ['-p', 'typescript@5.9', '-p', '@types/node@24', 'tsc', '-p', 'templates/tsconfig.require.json'] },
163+
);
146164

147-
let taskConfigs, envs;
165+
let envs;
148166
if (ALL_TESTS) {
149167
envs = ENVS;
150-
taskConfigs = buildTasksConfigs(TYPES, TARGETS, TYPE_SCRIPT_VERSIONS, ENVS, LIBS);
168+
tasks = [...tasks, ...buildTasks(TYPES, TARGETS, TYPE_SCRIPT_VERSIONS, envs, LIBS)];
151169
} else {
152-
envs = [null];
153-
taskConfigs = buildTasksConfigs(TYPES, ['esnext', 'es2022', 'es6'], ['5.9', '5.6'], envs, ['dom', null]);
170+
envs = [null, '@types/node@24'];
171+
tasks = [...tasks, ...buildTasks(TYPES, ['esnext', 'es2022', 'es6'], ['5.9', '5.6'], envs, ['dom', null])];
154172
}
155173
const numCPUs = os.cpus().length;
156174
await prepareEnvironment(envs, TYPES);
157-
await runLimited(taskConfigs, Math.max(numCPUs - 1, 1));
175+
await runLimited(tasks, Math.max(numCPUs - 1, 1));
158176
await clearTmpDir();
159177
echo(`Tested: ${ chalk.green(tested) }, Failed: ${ chalk.red(failed) }`);
160178
if (failed) throw new Error('Some tests have failed');

0 commit comments

Comments
 (0)