@@ -1426,36 +1426,69 @@ describe('Command', () => {
14261426 } )
14271427 } )
14281428
1429- // addresses https://github.com/yargs/yargs/issues/510
1430- it ( 'fails when the promise returned by the command handler rejects' , ( done ) => {
1431- const error = new Error ( )
1432- yargs ( 'foo' )
1433- . command ( 'foo' , 'foo command' , noop , ( yargs ) => Promise . reject ( error ) )
1434- . fail ( ( msg , err ) => {
1435- expect ( msg ) . to . equal ( null )
1436- expect ( err ) . to . equal ( error )
1437- done ( )
1429+ describe ( 'async' , ( ) => {
1430+ // addresses https://github.com/yargs/yargs/issues/510
1431+ it ( 'fails when the promise returned by the command handler rejects' , ( done ) => {
1432+ const error = new Error ( )
1433+ yargs ( 'foo' )
1434+ . command ( 'foo' , 'foo command' , noop , ( yargs ) => Promise . reject ( error ) )
1435+ . fail ( ( msg , err ) => {
1436+ expect ( msg ) . to . equal ( null )
1437+ expect ( err ) . to . equal ( error )
1438+ done ( )
1439+ } )
1440+ . parse ( )
1441+ } )
1442+
1443+ it ( 'succeeds when the promise returned by the command handler resolves' , ( done ) => {
1444+ const handler = new Promise ( ( resolve , reject ) => {
1445+ setTimeout ( ( ) => {
1446+ return resolve ( true )
1447+ } , 5 )
14381448 } )
1439- . parse ( )
1440- } )
1449+ const parsed = yargs ( 'foo hello' )
1450+ . command ( 'foo <pos>' , 'foo command' , ( ) => { } , ( yargs ) => handler )
1451+ . fail ( ( msg , err ) => {
1452+ return done ( Error ( 'should not have been called' ) )
1453+ } )
1454+ . parse ( )
14411455
1442- it ( 'succeeds when the promise returned by the command handler resolves' , ( done ) => {
1443- const handler = new Promise ( ( resolve , reject ) => {
1444- setTimeout ( ( ) => {
1445- return resolve ( true )
1446- } , 5 )
1447- } )
1448- const parsed = yargs ( 'foo hello' )
1449- . command ( 'foo <pos>' , 'foo command' , ( ) => { } , ( yargs ) => handler )
1450- . fail ( ( msg , err ) => {
1451- return done ( Error ( 'should not have been called' ) )
1456+ handler . then ( called => {
1457+ called . should . equal ( true )
1458+ parsed . pos . should . equal ( 'hello' )
1459+ return done ( )
14521460 } )
1453- . parse ( )
1461+ } )
14541462
1455- handler . then ( called => {
1456- called . should . equal ( true )
1457- parsed . pos . should . equal ( 'hello' )
1458- return done ( )
1463+ // see: https://github.com/yargs/yargs/issues/1144
1464+ it ( 'displays error and appropriate help message when handler fails' , ( done ) => {
1465+ const y = yargs ( 'foo' )
1466+ . command ( 'foo' , 'foo command' , ( yargs ) => {
1467+ yargs . option ( 'bar' , {
1468+ describe : 'bar option'
1469+ } )
1470+ } , ( argv ) => {
1471+ return Promise . reject ( Error ( 'foo error' ) )
1472+ } )
1473+ . exitProcess ( false )
1474+ // the bug reported in #1144 only happens when
1475+ // usage.help() is called, this does not occur when
1476+ // console output is suppressed. tldr; we capture
1477+ // the log output:
1478+ let errorLog = ''
1479+ y . _getLoggerInstance ( ) . error = ( out ) => {
1480+ if ( out ) errorLog += `${ out } \n`
1481+ }
1482+ y . parse ( )
1483+ // the promise returned by the handler rejects immediately,
1484+ // so we can wait for a small number of ms:
1485+ setTimeout ( ( ) => {
1486+ // ensure the appropriate help is displayed:
1487+ errorLog . should . include ( 'bar option' )
1488+ // ensure error was displayed:
1489+ errorLog . should . include ( 'foo error' )
1490+ return done ( )
1491+ } , 5 )
14591492 } )
14601493 } )
14611494
0 commit comments