import * as Path from 'path' ; import * as Fs from 'fs-extra' ; import { get } from 'lodash' ; import { unpacked , loc , posix } from '@lib/helpers/paths' ; import { ParsedRepository } from '@lib/frameworks/repository' ; import { FrameworkOptions , FrameworkDefaults , Framework , FrameworkReloadOutcome } from '@lib/frameworks/framework' ; import { ISuite } from '@lib/frameworks/suite' ; export class Jest extends Framework { static readonly defaults : FrameworkDefaults = { all : { name : 'Jest' , type : 'jest' , command : 'yarn test' , path : '' , proprietary : { } } } ; param repository public static async spawnForDirectory ( repository ) { if ( repository . files . includes ( 'package.json' ) ) { const pkg = await Fs . readJson ( Path . join ( repository . path , 'package.json' ) , { throws : false } ) || { } ; try { const scripts = get ( pkg , 'scripts' ) ; for ( const script in scripts ) { if ( scripts [ script ] . search ( /(?<![^\/\\\s])\bjest\b(\.js)?(?!\.)/i ) > - 1 ) { return this . hydrate ( { command : `yarn ${ script }` } ) ; } } } catch ( Error ) { } if ( get ( pkg , 'jest' ) ) { return this . hydrate ( ) ; } } return false ; } /**
     * Prepare this framework for running.
     */ protected async assemble ( ) { super . assemble ( ) ; if ( this . runsInRemote ) { const reporter = process . env . NODE_ENV === 'development' ? Path . resolve ( __dirname , loc ( '../../reporters/jest' ) ) : unpacked ( Path . join ( __static , loc ( './reporters/jest' ) ) ) ; await Fs . copy ( reporter , this . injectPath ( ) ) ; } } /**
     * Reload this framework's suites and tests.
     */ protected reload ( ) { return new Promise ( ( resolve , reject ) => { this . spawn ( [ '--listTests' , '--forceExit' ] ) . on ( 'success' , ( { lines } ) => { try { lines . sort ( ) ; lines . filter ( ( file ) => this . fileInPath ( file ) ) . map ( ( file ) => this . makeSuite ( this . hydrateSuiteResult ( { file , testsLoaded : false } ) ) ) ; resolve ( 'success' ) ; } catch ( error ) { this . stop ( ) ; reject ( 'The Jest package returned unexpected results.' ) ; } } ) . on ( 'killed' , ( ) => { resolve ( 'killed' ) ; } ) . on ( 'error' , error => { reject ( error ) ; } ) ; } ) ; } /**
     * The command arguments for running this framework.
     */ protected runArgs ( ) { const args = [ '--verbose=false' , '--forceExit' , '--expand' , '--colors' , '--reporters' , this . runsInRemote ? Path . join ( this . getRemotePath ( ) , loc ( '.lode/jest/index.js' ) ) : process . env . NODE_ENV === 'development' ? Path . resolve ( __dirname , loc ( '../../reporters/jest/index.js' ) ) : unpacked ( Path . join ( __static , loc ( './reporters/jest/index.js' ) ) ) ] ; if ( __DEV__ ) { args . push ( '--useStderr' ) ; } return args ; } param suites param selectTests protected runSelectiveArgs ( suites , selectTests ) { const args = [ ] ; suites . forEach ( ( suite ) => { args . push ( posix ( suite . getRelativePath ( ) ) ) ; } ) ; return args . concat ( this . runArgs ( ) ) ; } /**
     * Provide setup instructions for using Lode with Jest.
     */ public static instructions ( ) { return '' ; } param error protected troubleshoot ( error ) { if ( error instanceof Error ) { error = error . toString ( ) ; } if ( error . includes ( 'Error: Could not resolve a module for a custom reporter.' ) ) { return 'If your Jest tests run in a remote machine, make sure to toggle that in your framework settings and set the absolute path to the repository inside the remote machine.' ; } return super . troubleshoot ( error ) ; } }