|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import childProcess from 'child_process'; |
| 3 | +import os from 'os'; |
| 4 | + |
| 5 | +const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n'); |
| 6 | + |
| 7 | +let testsSucceeded = true; |
| 8 | +const testAmount = testPaths.length; |
| 9 | +const fails: string[] = []; |
| 10 | + |
| 11 | +const threads = os.cpus().map(async (_, i) => { |
| 12 | + let testPath = testPaths.pop(); |
| 13 | + while (testPath !== undefined) { |
| 14 | + console.log(`(Worker ${i}) Running test "${testPath}"`); |
| 15 | + await new Promise(resolve => { |
| 16 | + const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']); |
| 17 | + |
| 18 | + let output = ''; |
| 19 | + |
| 20 | + p.stdout.on('data', (data: Buffer) => { |
| 21 | + output = output + data.toString(); |
| 22 | + }); |
| 23 | + |
| 24 | + p.stderr.on('data', (data: Buffer) => { |
| 25 | + output = output + data.toString(); |
| 26 | + }); |
| 27 | + |
| 28 | + p.on('error', error => { |
| 29 | + console.log(`(Worker ${i}) Error in test "${testPath}"`, error); |
| 30 | + console.log(output); |
| 31 | + resolve(); |
| 32 | + }); |
| 33 | + |
| 34 | + p.on('exit', exitcode => { |
| 35 | + console.log(`(Worker ${i}) Finished test "${testPath}"`); |
| 36 | + console.log(output); |
| 37 | + if (exitcode !== 0) { |
| 38 | + fails.push(`FAILED: "${testPath}"`); |
| 39 | + testsSucceeded = false; |
| 40 | + } |
| 41 | + resolve(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + testPath = testPaths.pop(); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +void Promise.all(threads).then(() => { |
| 49 | + console.log('-------------------'); |
| 50 | + console.log(`Successfully ran ${testAmount} tests.`); |
| 51 | + if (!testsSucceeded) { |
| 52 | + console.log('Not all tests succeeded:\n'); |
| 53 | + fails.forEach(fail => { |
| 54 | + console.log(`● ${fail}`); |
| 55 | + }); |
| 56 | + process.exit(1); |
| 57 | + } else { |
| 58 | + console.log('All testcases returned the expected results.'); |
| 59 | + process.exit(0); |
| 60 | + } |
| 61 | +}); |
0 commit comments