After going through all related issues regarding stubbing readFile/Sync with Sinon /Chai/ Mocha, the test is failing.
There's a basic getFile function that retrieves a file:
function getFile(path) {
const file = fs.readFileSync(path, "utf8)
return file;
}
module.exports = {getFile}
and I want to create a test where getFile should throw an error if fs.readFileSync also throws an error:
it('should throw an error if fs.readFileSync throws an error', () => {
I tried:
it('should throw an error if fs.readFileSync throws an error', () => {
const error = new Error('some error message')
const myStub = sinon.stub(fs, "readFileSync")
myStub.throws(error)
const filePath = "/Project/test.js"
const gFile = index.getFile(filePath)
try {
if(myStub.error === true) {
gFile(error)
} catch (error) {
expect(myStub).to.throw(error)
What I got was:
1 failing Error: some error message at Context. at process.processImmediate
Try putting your error function in the Sinon throw method as below.
myStub.throws(new Error('some error message'));