Skip to content

Commit a389697

Browse files
committed
refactor type definitions tests runner
1 parent 137997c commit a389697

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

tests/type-definitions/runner.mjs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
import os from 'node:os';
1+
import { cpus } from 'node:os';
22
import { fs } from 'zx';
33

44
const { mkdir, writeJson } = fs;
5-
const TMP_DIR = './tmp/';
5+
66
const ALL_TESTS = process.env.ALL_TYPE_DEFINITIONS_TESTS === '1';
7+
const NUM_CPUS = cpus().length;
8+
const TMP_DIR = './tmp/';
79

810
const TARGETS = [
911
'esnext',
1012
'es2022',
1113
'es6',
1214
];
13-
const TYPE_SCRIPT_VERSIONS = [
15+
16+
const TYPE_SCRIPT_VERSIONS = ALL_TESTS ? [
1417
'5.9',
1518
'5.8',
1619
'5.7',
1720
'5.6',
18-
// '5.5', fails with node types: Named property 'next' of types 'AsyncIterator<T, TReturn, TNext>' and 'AsyncIteratorObject<T, TReturn, TNext>' are not identical.
21+
// '5.5', // fails with node types: Named property 'next' of types 'AsyncIterator<T, TReturn, TNext>' and 'AsyncIteratorObject<T, TReturn, TNext>' are not identical.
1922
// '5.4',
2023
// '5.3',
2124
// '5.2',
25+
] : [
26+
'5.9',
27+
'5.6',
2228
];
23-
const ENVS = [
29+
30+
const ENVS = ALL_TESTS ? [
2431
null,
2532
// '@types/node@25', // fails
2633
'@types/node@24',
@@ -30,15 +37,22 @@ const ENVS = [
3037
'@types/node@16',
3138
// '@types/node@15', // fails
3239
// '@types/bun@latest', // conflicts with DOM types (TextDecorator, SharedArrayBuffer...)
40+
// '@types/deno@latest', // fails
41+
] : [
42+
null,
43+
'@types/node@24',
3344
];
45+
3446
const TYPES = [
3547
'global',
3648
'pure',
3749
];
50+
3851
const LIBS = [
3952
'dom',
4053
null,
4154
];
55+
4256
const TARGET_RULES = {
4357
es6: ['**/*es2018*test.ts'],
4458
};
@@ -57,13 +71,10 @@ function getEnvPath(env) {
5771

5872
async function runLimited(tasks, limit) {
5973
let i = 0;
60-
async function worker() {
61-
while (i < tasks.length) {
62-
const idx = i++;
63-
await runTask(tasks[idx]);
64-
}
65-
}
66-
await Promise.all(Array.from({ length: limit }, worker));
74+
75+
await Promise.all(Array(limit).fill().map(async () => {
76+
while (i < tasks.length) await runTask(tasks[i++]);
77+
}));
6778
}
6879

6980
async function runTask({ cwd, ts, config, args = [] }) {
@@ -81,8 +92,7 @@ async function runTask({ cwd, ts, config, args = [] }) {
8192
}
8293
}
8394

84-
function buildTasks(types, targets, typeScriptVersions, envs, libs) {
85-
const tasks = [];
95+
function * buildTasks(types, targets, typeScriptVersions, envs, libs) {
8696
for (const type of types) {
8797
for (const target of targets) {
8898
for (const ts of typeScriptVersions) {
@@ -107,13 +117,12 @@ function buildTasks(types, targets, typeScriptVersions, envs, libs) {
107117
const envLibName = env ? `,${ env.substring(0, env.lastIndexOf('@')) }` : '';
108118
task.args.push('--types', `@core-js/types${ typesSuffix }${ envLibName }`);
109119
}
110-
tasks.push(task);
120+
yield task;
111121
}
112122
}
113123
}
114124
}
115125
}
116-
return tasks;
117126
}
118127

119128
async function clearTmpDir() {
@@ -152,7 +161,7 @@ async function prepareEnvironment(environments, coreJsTypes) {
152161
}
153162
}
154163

155-
let tasks = [
164+
const tasks = [
156165
{ ts: '5.9', config: 'tools/tsconfig.json' },
157166
{ ts: '5.9', config: 'templates/tsconfig.json' },
158167
{ ts: '5.9', config: 'templates/tsconfig.require.json' },
@@ -164,19 +173,13 @@ let tasks = [
164173
{ ts: '5.9', config: 'entries/global-symlinks/tsconfig.json' },
165174
{ ts: '5.9', config: 'entries/pure-symlinks/tsconfig.json' },
166175
{ ts: '5.9', config: 'entries/configurator/tsconfig.json' },
176+
...buildTasks(TYPES, TARGETS, TYPE_SCRIPT_VERSIONS, ENVS, LIBS),
167177
];
168178

169-
let envs;
170-
if (ALL_TESTS) {
171-
envs = ENVS;
172-
tasks = [...tasks, ...buildTasks(TYPES, TARGETS, TYPE_SCRIPT_VERSIONS, envs, LIBS)];
173-
} else {
174-
envs = [null, '@types/node@24'];
175-
tasks = [...tasks, ...buildTasks(TYPES, ['esnext', 'es2022', 'es6'], ['5.9', '5.6'], envs, ['dom', null])];
176-
}
177-
const numCPUs = os.cpus().length;
178-
await prepareEnvironment(envs, TYPES);
179-
await runLimited(tasks, Math.max(numCPUs - 1, 1));
179+
await prepareEnvironment(ENVS, TYPES);
180+
await runLimited(tasks, Math.max(NUM_CPUS - 1, 1));
180181
await clearTmpDir();
182+
181183
echo(`Tested: ${ chalk.green(tested) }, Failed: ${ chalk.red(failed) }`);
184+
182185
if (failed) throw new Error('Some tests have failed');

0 commit comments

Comments
 (0)