1
1
/* eslint-disable no-console */
2
2
import childProcess from 'child_process' ;
3
3
import 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 ;
4
16
5
17
// This variable will act as a job queue that is consumed by a number of worker threads. Each item represents a test to run.
6
18
const testPaths = childProcess . execSync ( 'jest --listTests' , { encoding : 'utf8' } ) . trim ( ) . split ( '\n' ) ;
@@ -14,7 +26,17 @@ const workers = os.cpus().map(async (_, i) => {
14
26
const testPath = testPaths . pop ( ) ;
15
27
console . log ( `(Worker ${ i } ) Running test "${ testPath } "` ) ;
16
28
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 ) ;
18
40
19
41
// We're collecting the output and logging it all at once instead of inheriting stdout and stderr, so that
20
42
// 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) => {
36
58
} ) ;
37
59
38
60
jestProcess . on ( 'exit' , exitcode => {
61
+ output = checkSkippedAllTests ( output , i , testPath ) ;
39
62
console . log ( `(Worker ${ i } ) Finished test "${ testPath } "` ) ;
40
63
console . log ( output ) ;
41
64
if ( exitcode !== 0 ) {
@@ -61,3 +84,20 @@ void Promise.all(workers).then(() => {
61
84
process . exit ( 0 ) ;
62
85
}
63
86
} ) ;
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