@@ -11,7 +11,7 @@ const JEST_PATH = 'jest';
1111// return the result of the spawned proccess:
1212// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
1313// 'envPairs', 'options', 'args', 'file' ]
14- export default function runJest ( dir : string , args : string [ ] ) {
14+ export default function runJest ( dir : string , args : string [ ] , env = { } ) : Result {
1515 const isRelative = dir [ 0 ] !== '/' ;
1616
1717 if ( isRelative ) {
@@ -30,10 +30,28 @@ export default function runJest(dir: string, args: string[]) {
3030
3131 const result = spawnSync ( JEST_PATH , args || [ ] , {
3232 cwd : dir ,
33+ env : { ...process . env , ...env } , // Add both process.env which is the standard and custom env variables
3334 } ) ;
3435
35- result . stdout = result . stdout && result . stdout . toString ( ) ;
36- result . stderr = result . stderr && result . stderr . toString ( ) ;
36+ // Call to string on byte arrays and strip ansi color codes for more accurate string comparison.
37+ result . stdout = result . stdout && stripAnsiColors ( result . stdout . toString ( ) ) ;
38+ result . stderr = result . stderr && stripAnsiColors ( result . stderr . toString ( ) ) ;
39+ result . output = result . output && stripAnsiColors ( result . output . toString ( ) ) ;
3740
3841 return result ;
3942}
43+
44+ // from https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
45+ function stripAnsiColors ( stringToStrip : String ) : String {
46+ return stringToStrip . replace (
47+ / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g,
48+ '' ,
49+ ) ;
50+ }
51+
52+ export interface Result {
53+ stdout : string ;
54+ stderr : string ;
55+ status : number ;
56+ output : string ;
57+ }
0 commit comments