0

As per suggestion in question enter link description here I mock readFileSync and mocked my outer function now I want to verify if the variables value is set as expected or not

file.js

    const fs1 = require('fs');
    let param;
    module.export = {
         test,
         param
    }

    function test (outputParam) {
     param = fs1.readFileSync(outputParam);
    }

I have stubbed this readFileSync and it returns specified file content as shown in the test below

When I run the test I want to see the variable param has the file content value

test.spec.js

    let expect = require('chai').expect;
    let fs = require("fs");
    let sinon = require("sinon");
    let filejs = require('./file.js');


    it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {

     let someArg ="file.xml";

     sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
        return "this is my file content";
      });

      var mock = sinon.mock(filejs);
      mock.expects('test').withArgs(someArg);
      expect(filejs.param).to.equal('this is my file content');

  })

From file.js, As you see property param gets value from "readFileSync" which is stubbed to return a value

When I run the test

expect(filejs.param).to.equal('this is my file content');

AssertionError: expected undefined to equal 'this is my file content'

2
  • Hi Let me know if anyone hasn't understood my question Commented Feb 19, 2020 at 11:55
  • I have edited my question, made it more simple Commented Feb 19, 2020 at 11:56

1 Answer 1

1

Note the correct spelling of module.exports - not module.export.

In your file.js the variable param is not initialized, so it will get the value undefined and will be exported with that value, too. The exported value does not change if you modify param later. An export only binds a property to a value, not to a variable. And in fact, you don't even need a local variable. To change the value of an export dynamically, at least in Node, you simply reassign the property on module.exports.

const fs1 = require('fs');

module.exports = {
     test
}

function test (outputParam) {
    module.exports.param = fs1.readFileSync(outputParam);
}

As for your test.spec.js, you were already very close to get it to work. After stubbing the method(s) that should not be called under the hub, just call your test function, passing real parameters. No mock is required there.

let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');


it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {

 let someArg ="file.xml";

 sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
    return "this is my file content";
  });

  filejs.test(someArg);

  expect(filejs.param).to.equal('this is my file content');

});

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks GOTO 0, I want to understand how do we differentiate property and variable, in this case for 'param'
I understood properties are something related to a object and variable is like placeholder for value. Please correct me if I am wrong
Glad I could help. There are some good articles all over the web explaining the fundamentals of variables and object properties in JS. I quickly googled out this one which should cover your question. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.