file description import { Config } from '@jest/types' ; import yargs from 'yargs/yargs' ; import { runCLI } from 'jest' ; import chalk from 'chalk' ; import { getJestUtils , patchConfig } from './config' ; import { TestConfig } from './types' ; import { debug } from './utils' ; import { createLifeCycle } from './plugin' ; type Agrv = Omit < Config . Argv , '_' | '$0' > ; const buildArgv = async ( rawArgv , config ) : Promise < Config . Argv > => { const argv = await yargs ( rawArgv ) . argv ; const result = { $0 : argv . $0 , _ : argv . _ . slice ( 1 ) , } ; Object . keys ( argv ) . forEach ( key => { if ( key . includes ( '-' ) || key === '_' ) { return ; } result [ key ] = argv [ key ] ; } ) ; if ( config ) { result . config = JSON . stringify ( config ) ; } return result ; } ; const readResultsAndExit = ( result , globalConfig ) => { const code = ! result || result . success ? 0 : globalConfig . testFailureExitCode ; process . on ( 'exit' , ( ) => { if ( typeof code === 'number' && code !== 0 ) { process . exitCode = code ; } } ) ; if ( globalConfig . forceExit ) { if ( ! globalConfig . detectOpenHandles ) { console . warn ( `${ chalk . bold ( 'Force exiting Jest: ' ) }Have you considered using \`--detectOpenHandles\` to detect ` + `async operations that kept running after all tests finished?` ) ; } process . exit ( code ) ; } else if ( ! globalConfig . detectOpenHandles ) { setTimeout ( ( ) => { console . warn ( chalk . yellow . bold ( 'Jest did not exit one second after the test run has completed.\n\n' ) + chalk . yellow ( 'This usually means that there are asynchronous operations that ' + "weren't stopped in your tests. Consider running Jest with " + '`--detectOpenHandles` to troubleshoot this issue.' ) ) ; } , 1000 ) . unref ( ) ; } } ; /**
 * Node API: excute jest
 */ export async function runJest ( config , pwd = process . cwd ( ) ) { try { const argvConfig = await buildArgv ( process . argv . slice ( 2 ) , config ) ; const { results , globalConfig } = await runCLI ( argvConfig , [ pwd ] ) ; readResultsAndExit ( results , globalConfig ) ; } catch ( e ) { console . error ( chalk . red ( e ?. stack || e ) ) ; process . exit ( 1 ) ; } } /**
 * Node API: run test
 */ export async function runTest ( config , pwd = process . cwd ( ) ) { process . env . NODE_ENV = 'test' ; const lifeCycle = createLifeCycle ( config . plugins ) ; const jestUtils = getJestUtils ( config ) ; await patchConfig ( jestUtils ) ; await lifeCycle . jestConfig ( jestUtils , { onLast : input => input as any , } ) ; const finalConfig = jestUtils . jestConfig ; debug ( 'Jest config:' , finalConfig ) ; await runJest ( finalConfig , pwd ) ; }