11/* eslint-disable no-console */
22import childProcess from 'child_process' ;
33import os from 'os' ;
4+ import yargs from 'yargs' ;
5+
6+ const args = yargs
7+ . option ( 't' , {
8+ alias : 'testNamePattern' ,
9+ type : 'string' ,
10+ description : 'Filter for a specific test spec\nsee: https://jestjs.io/docs/cli#--testnamepatternregex' ,
11+ } )
12+ . option ( 'watch' , {
13+ type : 'boolean' ,
14+ description : 'Run tests in watch mode\nsee: https://jestjs.io/docs/cli#--watch' ,
15+ } ) . argv ;
416
517// This variable will act as a job queue that is consumed by a number of worker threads. Each item represents a test to run.
618const testPaths = childProcess . execSync ( 'jest --listTests' , { encoding : 'utf8' } ) . trim ( ) . split ( '\n' ) ;
@@ -14,7 +26,17 @@ const workers = os.cpus().map(async (_, i) => {
1426 const testPath = testPaths . pop ( ) ;
1527 console . log ( `(Worker ${ i } ) Running test "${ testPath } "` ) ;
1628 await new Promise < void > ( resolve => {
17- const jestProcess = childProcess . spawn ( 'jest' , [ '--runTestsByPath' , testPath as string , '--forceExit' ] ) ;
29+ const jestArgs = [ '--runTestsByPath' , testPath as string , '--forceExit' ] ;
30+
31+ if ( args . t ) {
32+ jestArgs . push ( '-t' , args . t ) ;
33+ }
34+
35+ if ( args . watch ) {
36+ jestArgs . push ( '--watch' ) ;
37+ }
38+
39+ const jestProcess = childProcess . spawn ( 'jest' , jestArgs ) ;
1840
1941 // We're collecting the output and logging it all at once instead of inheriting stdout and stderr, so that
2042 // test outputs of the individual workers aren't interwoven, in case they print at the same time.
@@ -36,6 +58,7 @@ const workers = os.cpus().map(async (_, i) => {
3658 } ) ;
3759
3860 jestProcess . on ( 'exit' , exitcode => {
61+ output = checkSkippedAllTests ( output , i , testPath ) ;
3962 console . log ( `(Worker ${ i } ) Finished test "${ testPath } "` ) ;
4063 console . log ( output ) ;
4164 if ( exitcode !== 0 ) {
@@ -61,3 +84,20 @@ void Promise.all(workers).then(() => {
6184 process . exit ( 0 ) ;
6285 }
6386} ) ;
87+
88+ /**
89+ * Suppress jest output for test suites where all tests were skipped.
90+ * This only clutters the logs and we can safely print a one-liner instead.
91+ */
92+ function checkSkippedAllTests ( output : string , workerNumber : number , testPath : string | undefined ) : string {
93+ const regex = / T e s t s : \s + ( \d + ) s k i p p e d , ( \d + ) t o t a l / gm;
94+ const matches = regex . exec ( output ) ;
95+ if ( matches ) {
96+ const skipped = Number ( matches [ 1 ] ) ;
97+ const total = Number ( matches [ 2 ] ) ;
98+ if ( ! isNaN ( skipped ) && ! isNaN ( total ) && total === skipped ) {
99+ return `(Worker ${ workerNumber } ) > Skipped all (${ total } tests) in ${ testPath } ` ;
100+ }
101+ }
102+ return output ;
103+ }
0 commit comments