`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? On Friday, November 1, 2019 at 5:08:08 PM UTC-5, Lukas Wilkeer wrote: > > 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. > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7de6daa4-288d-4192-a4f5-f22249623a47%40googlegroups.com.