import { existsSync } from 'fs' ; import spawn from 'cross-spawn' ; import concat from 'concat-stream' ; import os from 'os' ; import { ChildProcess } from 'child_process' ; const PATH = process . env . PATH ; /**
 * Set current directory to ./tests so that all paths can be set as relative to test folder
 */ export function setTestFolder ( ) { const originalCwd = process . cwd ( ) ; before ( ( ) => { process . chdir ( __dirname ) ; } ) ; after ( ( ) => { process . chdir ( originalCwd ) ; } ) ; } /**
 * Unify EOL for snapshots
 */ export function mockEOL ( ) { const originalEOL = os . EOL ; before ( ( ) => { Object . defineProperty ( os , 'EOL' , { value : '\r\n' , } ) ; } ) ; after ( ( ) => { Object . defineProperty ( os , 'EOL' , { value : originalEOL , } ) ; } ) ; } param processPath param args function createProcess ( processPath , args = [ ] ) { if ( ! processPath || ! existsSync ( processPath ) ) { throw new Error ( 'Invalid process path' ) ; } return spawn ( 'node' , [ processPath , ... args ] , { env : { NODE_ENV : 'test' , preventAutoStart : 'false' , PATH , } , stdio : [ null , null , null , 'ipc' ] , } ) ; } param processPath param args export function execute ( processPath , args = [ ] ) { const childProcess = createProcess ( processPath , args ) ; if ( childProcess . stdin === null ) { throw new Error ( 'stdin is null' ) ; } return new Promise ( ( resolve , reject ) => { if ( childProcess . stderr ) { childProcess . stderr . once ( 'data' , ( error ) => { reject ( error . toString ( ) ) ; } ) ; } childProcess . on ( 'error' , reject ) ; if ( childProcess . stdout ) { childProcess . stdout . pipe ( concat ( ( result ) => { resolve ( result . toString ( ) ) ; } ) ) ; } } ) ; }