rewire icon indicating copy to clipboard operation
rewire copied to clipboard

not able to unset mock inside same test (mocha)

Open thisisbalajibabu opened this issue 10 years ago • 0 comments

Hi

I have the use case where I would want to test the code based on the boundary conditions from the result of the external module call. For eg: var myModule = rewire("../lib/myModule.js"); describe("test for rewire", function(){

it('should be able to revert', function(){ var fsMock = { readFile: function (path, encoding, cb) { cb(null, "Success!"); } }; var revert = myModule.set("fs", fsMock); myModule.readSomethingFromFileSystem(function (err, data) { console.log(data); // = Success! }); revert(); });

it('should be able to set a new mock', function(){ var fsMock = { readFile: function (path, encoding, cb) { cb({somevalue:"value"}, "Success!"); } }; var revert1= myModule.set("fs", fsMock); myModule.readSomethingFromFileSystem(function (err, data) { console.log(data); // = Success! }); revert1(); });

});

When I try to write test cases for above kind of scenario, wherein the cb might take null in one test case and some value in the m other. So when read file is called, the mock is expected to perform accordingly. But what i observe is that once i have fsmock set to fs with a behaviour, I am not able to override the same. the revert() doesn't seem to work. I even tried using rewire separately using different variables for each test case, but still no luck.

thisisbalajibabu avatar Mar 16 '16 16:03 thisisbalajibabu