File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed
Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -2162,8 +2162,10 @@ export class YargsInstance {
21622162 if ( helpOptSet ) {
21632163 if ( this . #exitProcess) setBlocking ( true ) ;
21642164 skipValidation = true ;
2165- this . showHelp ( 'log' ) ;
2166- this . exit ( 0 ) ;
2165+ this . showHelp ( message => {
2166+ this . #logger. log ( message ) ;
2167+ this . exit ( 0 ) ;
2168+ } ) ;
21672169 } else if ( versionOptSet ) {
21682170 if ( this . #exitProcess) setBlocking ( true ) ;
21692171 skipValidation = true ;
Original file line number Diff line number Diff line change @@ -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
You can’t perform that action at this time.
0 commit comments