Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Example: Stubbing Node globals with sinon #69

Open
PreethiVuchuru27916 opened this issue Feb 24, 2023 · 1 comment
Open

🚀 Example: Stubbing Node globals with sinon #69

PreethiVuchuru27916 opened this issue Feb 24, 2023 · 1 comment
Labels
area: examples status: accepting prs Please, send a pull request to resolve this! 🙏 type: feature New feature or request

Comments

@PreethiVuchuru27916
Copy link

PreethiVuchuru27916 commented Feb 24, 2023

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 an approach with which I can be able to get not only by using console.log but also by using return statement. Like process.stdout.on works for console.log statements, suggest me some thing that works for a process's method return value.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Example needed for cli testing tool 🚀 Example: Stubbing Node globals with sinon Jul 4, 2024
@JoshuaKGoldberg
Copy link
Member

I'm surprised there isn't already a set of examples with Sinon.JS! +1 to showing how to use that for things like:

sinon.stub(process, 'exit');

@JoshuaKGoldberg JoshuaKGoldberg added type: feature New feature or request status: accepting prs Please, send a pull request to resolve this! 🙏 area: examples labels Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: examples status: accepting prs Please, send a pull request to resolve this! 🙏 type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants