0

I have a code as below src/example.js:

var argv = require('yargs')
.usage('service 1.\nUsage: $0 -s [host] -n [port]')
.alias('s', 'host') 
.alias('n', 'port')
.describe('host', 'Hostname to be used')
.describe('port', 'Port on which service will run')
.argv;  //--->> Can this line even be stubbed??


function a(){
    console.log(argv.host.toString());
    console.log(argv.port.toString())
}


module.exports = {a}

The unit test case for above, that I'm trying to write:

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
var expect = require('chai').expect;

describe("Add service", function() {
    let roo;
    let argv;
    beforeEach(function() {
         var argv = {}
         argv.host = 'xx.xx.xx.xx';
         argv.port = '7677'
         roo = proxyquire('../src/example.js', { 'yargs' : { argv: argv}} );

    })

    it("Response should be logged", function() {
         roo.a()
    })

})

When I run the above I get the below error:

Add service
1) "before each" hook for "Response should be logged"



  0 passing (8ms)
  1 failing

  1) Add service "before each" hook for "Response should be logged":
   TypeError: Cannot read property 'toString' of undefined
    at a (src/example.js:14:31)

What am I doing wrong here??

EDIT: So apparently if I write my src/example.js as below:

var argv = require("yargs")
                .option('host', {
                       alias: 's',
                       demand: false,
                       describe: 'Hostname to be used',
                       default: "xx.xx.xx.xx"
                }) 
                .option('port', {
                       alias: 'n',
                       demand: true,
                       describe: "sme port",
                       default: "7677"
                })
                .help()
                .alias('help', 'h')
                .argv


function a(done){
   console.log(argv.machineHost.toString());
   console.log(argv.servicePort.toString())
   done(argv.machineHost)
}

module.exports = {a}

And write the test case as below:

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
var expect = require('chai').expect;

describe("Add service", function() {
    let roo;
    let argv;
    beforeEach(function() {
         roo = proxyquire('../src/example.js', {} );

    })

    it("Response should be logged", function() {
         roo.a(function(resp){
            chai.expect(resp).to.equal("xx.xx.xx.xx")
         })
    })

})

It works! Go figure! This is probably because I'm setting a default value in the yargs. But I don't know how else to get this to work without doing the above changes!

user1452759
  • 8,810
  • 15
  • 42
  • 58

1 Answers1

0

It means that the 'yargs' object doesn't have host or port attributes.

Try:

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
var expect = require('chai').expect;

describe("Add service", function() {
    let roo;

    beforeEach(function() {
      roo = proxyquire('../src/example.js', { 'yargs' :  {
       host: 'xx.xx.xx.xx',
       port: '7677',
      }});
     })

    it("Response should be logged", function() {
         roo.a()
    })
})
Jezpoz
  • 179
  • 1
  • 7