Skip to content

Commit

Permalink
test: allow to run tests in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Oct 24, 2024
1 parent 417cf78 commit 7418d61
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const pc = require('picocolors');
const { globSync } = require('tinyglobby');
const utils = require('./utils.js');
const { spawn } = require('child_process');
const { cpus } = require('os');
const host = 'node' + utils.getNodeMajorVersion();
let target = process.argv[2] || 'host';
if (target === 'host') target = host;
Expand All @@ -18,7 +19,9 @@ if (target === 'host') target = host;

const flavor = process.env.FLAVOR || process.argv[3] || 'all';

const isCI = process.env.CI === 'true';
// const isCI = process.env.CI === 'true';

const maxConcurrency = flavor === 'only-npm' ? 1 : Math.max(cpus().length, 4);

console.log('');
console.log('*************************************');
Expand Down Expand Up @@ -151,20 +154,13 @@ function runTest(file) {
});
}

const clearLastLine = () => {
if (isCI) return;
process.stdout.moveCursor(0, -1); // up one line
process.stdout.clearLine(1); // from cursor to end
};

async function run() {
let done = 0;
let ok = 0;
let failed = [];
const start = Date.now();

function addLog(log, isError = false) {
clearLastLine();
if (isError) {
console.error(log);
} else {
Expand All @@ -176,9 +172,6 @@ async function run() {
file = path.resolve(file);
const startTest = Date.now();
try {
if (!isCI) {
console.log(pc.gray(`⏳ ${file} - ${done}/${files.length}`));
}
await runTest(file);
ok++;
addLog(
Expand All @@ -203,8 +196,8 @@ async function run() {
done++;
});

for (let i = 0; i < promises.length; i++) {
await promises[i]();
for (let i = 0; i < promises.length; i += maxConcurrency) {
await Promise.all(promises.slice(i, i + maxConcurrency).map((p) => p()));
}

const end = Date.now();
Expand Down Expand Up @@ -236,6 +229,8 @@ function cleanup() {
for (const process of activeProcesses) {
process.kill();
}

process.exit(1);
}

process.on('SIGINT', cleanup);
Expand Down

0 comments on commit 7418d61

Please sign in to comment.