How do I stub node.js built-in fs during testing?

前端 未结 11 2110
暖寄归人
暖寄归人 2021-02-05 02:21

I want to stub node.js built-ins like fs so that I don\'t actually make any system level file calls. The only thing I can think to do is to pass in fs

11条回答
  •  醉话见心
    2021-02-05 02:38

    var fs = require('./myStubFs');
    

    Would seem to be a big improvement. Whatever solution you find will probably involve writing your own stub functions. The ideal solution would be lower level so you don't have to touch all the files you want doing this, e.g. perhaps you want 3rd party libraries stubbed out as well.

提交回复
热议问题