tj / commander

The complete solution for Ruby command-line executables
http://visionmedia.github.com/commander
MIT License
1.09k stars 64 forks source link

Unable to build a automatic test tool that tests the commander Js's command action with a return value. #104

Closed PreethiVuchuru27916 closed 1 year ago

PreethiVuchuru27916 commented 1 year ago

I have a file fele.js that specifies all commands that my app needs.

Command Used - Just says hello world when hello is entered.

const program = new commander.Command();
const helloCommand = program.command('hello');
helloCommand
    .action(async(options) => {
       console.log("Hello World"); //Works well as 
        return "Hello World";//But this does not work. 
    })

Now I am using Mocha to test the command. PFB Code


const assert = require('assert');
const { spawn } = require('child_process');
const path = require('path');

describe('Hello Command', function() {
  it('should output a greeting message', function(done) {
    const expectedOutput = 'Hello World\n';
    const cliPath = path.join(__dirname,'fele.js'); 
    const process = spawn('node', [cliPath, 'hello']);
    let actual = '';
    process.stdout.on('data', data => {
      actual += data.toString();
    });
    process.on('close', () => {
      assert.strictEqual(output, expectedOutput);
      done();
    });
  });
});```

Please do suggest me what can I do so that I get the return value from the command's action be available to the test file. 

I understand that the result can be stored in another variable and exported but I want to get a better solution. Please assist me if there is any other way.
ggilder commented 1 year ago

Wrong repo - I think you want to post this issue here: https://github.com/tj/commander.js/