Skip to content Skip to sidebar Skip to footer

After Stubbing Function Still It Calls The Real Function

I have stubbed the content of file, so I can run through only outer function file.html file.js const fs1 = require('fs');

Solution 1:

You should probably try to stub readFileSync.

const fs = require('fs');

// ...

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

Besides that, I can spot two issues with your code.

  • The real readFileSync will return a Buffer if called without a second parameter, not a string like your stub does.

  • The body onload event only exists inside the DOM. The fs module is only available in Node.js. If you run your code in a browser, you won't be able to use fs.readFileSync. If you run it in Node, your HTML file and the onload event won't be useful.


Post a Comment for "After Stubbing Function Still It Calls The Real Function"