import { Command , flags } from '@oclif/command' ; import merge = require ( 'deepmerge' ) ; import { existsSync } from 'fs' ; import { resolve } from 'path' ; import { jestConfig } from '../../config/jestConfig' ; import { messages } from '../../messages/test_unit' ; import { log , welcome } from '../../utils/logger' ; const spawn = require ( 'child_process' ) . spawn ; export default class Test extends Command { static description = messages . description ; static examples = messages . help . examples ; static flags = { help : flags . help ( { char : 'h' } ) , coverage : flags . boolean ( { char : 'c' , description : messages . flags . coverage } ) , debug : flags . boolean ( { char : 'd' , description : messages . flags . debug } ) , watch : flags . boolean ( { char : 'w' , description : messages . flags . watch } ) , runInBand : flags . boolean ( { char : 'r' , description : messages . flags . runInBand } ) , passthrough : flags . string ( { char : 'p' , multiple : true , description : messages . flags . passthrough } ) } ; async run ( ) { const { flags } = this . parse ( Test ) ; console . clear ( ) ; welcome ( ) ; let jestFinalConfig = jestConfig ; if ( ! existsSync ( 'jest.config.js' ) && ! existsSync ( 'jest.config.json' ) ) { log ( messages . logs . default_configuration ) ; } else { log ( messages . logs . custom_configuration ) ; if ( existsSync ( 'jest.config.js' ) ) { const jestCustomConfigJs = require ( resolve ( process . cwd ( ) , 'jest.config.js' ) ) ; jestFinalConfig = merge ( jestFinalConfig , jestCustomConfigJs ) ; } if ( existsSync ( 'jest.config.json' ) ) { const jestCustomConfigJson = require ( resolve ( process . cwd ( ) , 'jest.config.json' ) ) ; jestFinalConfig = { ... jestFinalConfig , ... jestCustomConfigJson } ; } } log ( messages . logs . starting_jest ) ; if ( flags . debug ) { const jestExecutable = process . platform === 'win32' ? './node_modules/jest/bin/jest.js' : './node_modules/.bin/jest' ; const debugArguments = [ '--inspect-brk' , jestExecutable , '--runInBand' ] ; if ( flags . passthrough ) { flags . passthrough . forEach ( ( arg ) => debugArguments . push ( arg ) ) ; } const jestSpawn = spawn ( 'node' , debugArguments ) ; jestSpawn . on ( 'error' , ( err ) => { log ( { message : `${ err }` , emoji : 'sos' } ) ; } ) ; jestSpawn . stderr . on ( 'data' , ( data ) => { log ( { message : `${ data }` , emoji : 'rainbow' } ) ; } ) ; } else { const jestArguments = [ ] ; if ( flags . watch ) { jestArguments . push ( '--watchAll' ) ; } if ( flags . coverage ) { jestArguments . push ( '--coverage' ) ; } if ( flags . runInBand ) { jestArguments . push ( '--runInBand' ) ; } if ( flags . passthrough ) { flags . passthrough . forEach ( ( arg ) => jestArguments . push ( arg ) ) ; } const jest = require ( 'jest' ) ; jest . run ( [ '--config' , JSON . stringify ( jestFinalConfig ) ] . concat ( jestArguments ) ) ; } } }