Skip to content

Commit

Permalink
fix: exit after async handler done (#2313)
Browse files Browse the repository at this point in the history
  • Loading branch information
PengBoUESTC committed Apr 28, 2023
1 parent 8343c66 commit e326cde
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/yargs-factory.ts
Expand Up @@ -2162,8 +2162,10 @@ export class YargsInstance {
if (helpOptSet) {
if (this.#exitProcess) setBlocking(true);
skipValidation = true;
this.showHelp('log');
this.exit(0);
this.showHelp(message => {
this.#logger.log(message);
this.exit(0);
});
} else if (versionOptSet) {
if (this.#exitProcess) setBlocking(true);
skipValidation = true;
Expand Down
28 changes: 28 additions & 0 deletions test/usage.cjs
Expand Up @@ -7,6 +7,7 @@ const chalk = require('chalk');
const yargs = require('../index.cjs');
const expect = require('chai').expect;
const {YError} = require('../build/index.cjs');
const assert = require('assert');

const should = require('chai').should();

Expand Down Expand Up @@ -4877,6 +4878,33 @@ describe('usage tests', () => {
help.split('\n').should.deep.equal(expected);
});
});

it('help is displayed before exit is called with async default command', async () => {
// https://github.com/yargs/yargs/issues/2312
const _exit = process.exit;
const _log = console.log;
let callCount = 0;
let logCall = 0;
let exitCall = 0;
process.exit = () => {
exitCall = ++callCount;
};
console.log = () => {
logCall = ++callCount;
};
await yargs(['--help'])
.command('$0', 'a test command', async yargs => {
await wait();
})
.parseAsync();
// The async help is dangling, so wait for it to fire!
await wait();
assert.ok(exitCall > 0, 'exit never called'); // sanity check
assert.ok(logCall > 0, 'log never called'); // sanity check
assert.ok(exitCall > logCall, 'exit called before help displayed');
console.log = _log;
process.exit = _exit;
});
});

// Refs: https://github.com/yargs/yargs/issues/1820
Expand Down

0 comments on commit e326cde

Please sign in to comment.