@@ -7,6 +7,7 @@ const chalk = require('chalk');
77const yargs = require ( '../index.cjs' ) ;
88const expect = require ( 'chai' ) . expect ;
99const { YError} = require ( '../build/index.cjs' ) ;
10+ const assert = require ( 'assert' ) ;
1011
1112const should = require ( 'chai' ) . should ( ) ;
1213
@@ -4877,6 +4878,33 @@ describe('usage tests', () => {
48774878 help . split ( '\n' ) . should . deep . equal ( expected ) ;
48784879 } ) ;
48794880 } ) ;
4881+
4882+ it ( 'help is displayed before exit is called with async default command' , async ( ) => {
4883+ // https://github.com/yargs/yargs/issues/2312
4884+ const _exit = process . exit ;
4885+ const _log = console . log ;
4886+ let callCount = 0 ;
4887+ let logCall = 0 ;
4888+ let exitCall = 0 ;
4889+ process . exit = ( ) => {
4890+ exitCall = ++ callCount ;
4891+ } ;
4892+ console . log = ( ) => {
4893+ logCall = ++ callCount ;
4894+ } ;
4895+ await yargs ( [ '--help' ] )
4896+ . command ( '$0' , 'a test command' , async yargs => {
4897+ await wait ( ) ;
4898+ } )
4899+ . parseAsync ( ) ;
4900+ // The async help is dangling, so wait for it to fire!
4901+ await wait ( ) ;
4902+ assert . ok ( exitCall > 0 , 'exit never called' ) ; // sanity check
4903+ assert . ok ( logCall > 0 , 'log never called' ) ; // sanity check
4904+ assert . ok ( exitCall > logCall , 'exit called before help displayed' ) ;
4905+ console . log = _log ;
4906+ process . exit = _exit ;
4907+ } ) ;
48804908 } ) ;
48814909
48824910 // Refs: https://github.com/yargs/yargs/issues/1820
0 commit comments