writeFileSync stub causing reference error.

125 views
Skip to first unread message

Lukas Wilkeer

unread,
Nov 1, 2019, 6:08:08 PM11/1/19
to nodejs
I'm attempt to stub the fs.writeFileSync method, but it's causing a Reference Error.

In that case, oeverloading the fs method causes troubes as sinon.restore() doesn't work.

My stub.

const writeImageStub = sinon.stub(fs, 'writeFileSync')
      .callsFake((file, data, (err) => {
        expect(file).to.be.a('string')
        expect(getFileExtension(data)).to.equal('jpg')
        expect(new Buffer.alloc(data)).to.be.a('object')

        return '/static/images/image.jpg'
      }))

Reference error: file is not defined.

How to do this, what causing this and exist other method?

PS:
Spying the method causes the write operation to be executed, on this case i have to delete the files written, but how to get the filename as the format is filename.Date.now.extension. Don't exist a way to store as a global variable to do this.

Amresh Yadav

unread,
Nov 1, 2019, 11:25:42 PM11/1/19
to nod...@googlegroups.com
Hi Lukas,

you can try something like this,

const writeImageStub = sinon.stub(fs, 'writeFileSync').callsFake((file, data, _cb)=>{
expect(typeof file).toBe('string');
// expect(getFileExtension(data)).to.equal('jpg')
// expect(new Buffer.alloc(data)).to.be.a('object')
fs.writeFile(file,data,_cb)
});
writeImageStub("abc.txt","Hello world",(err, data)=>{
console.log(err,data);
})

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/3d26db31-2c2c-4c3b-8055-7c8f54628bf6%40googlegroups.com.


--
Regards,
Amresh Yadav

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender immediately.
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager.

Corey Cleary

unread,
Nov 1, 2019, 11:25:59 PM11/1/19
to nodejs
`file` and `data` have no value... so that's why you get the ReferenceError. Understanding why you're trying to stub the filesystem method would help (i.e. - what other code are you trying to test that is causing you to need to stub the fs), but from what I can tell given the context here you can simplify this a lot.

.callsFake takes a function, so you can just do this:

const writeImageStub =
  sinon
    .stub(fs'writeFileSync')
    .callsFake(() => '/static/images/image.jpg')

What you have is more of a mock than a stub, because you're returning pre-configured data AND attempting to verify behavior as part of the fake. I generally avoid mocks.

Also, you shouldn't need to verify/expect things as part of a core module or even 3rd party modules. You should just verify the code you own. Does that help?
Reply all
Reply to author
Forward
0 new messages