Automatic restore for spies & mocks for mocha
Sinon Mocha is an extension library for mocha/sinon that will automatically clean up "wrapped" methods.
As of version 0.0.1 browser support is not tested but is planned in a future version.
var sinon = require'sinon'//You don't need to use expect for this to work//This is just to provide a working exampleexpect = require'expect.js';//Enhance sinon//You can also pass a mocha as the second argument.require'sinon-mocha'enhancesinon;describe"MyClass"var MyClass = require'my-class'subject;//before (all) hooks are *NOT* automatically cleaned up for you.beforesinonstubMyClassprototype 'ajax'//...;;//You must `.restore` the method yourself.afterMyClassprototypeajaxrestore;;beforeEachsubject = ;sinonspysubject 'spiedMethod';//Stubs also worksinonstubsubject 'stubbedMethod';//And stubs with functionalitysinonstubsubject 'stubbedMethodWFunc'console.log' I get called ! ' ;;;describe".spiedMethod"it"will call spy"subjectspiedMethod;expectsubjectspiedMethodcalledtobetrue;;;// Etc...;
In short sinon's wrapMethod function is overwritten to capture its results in the
beforeEach of every test.
Then in the
afterEach those methods are
To gain access to mocha there is a hack that will search through
require.cache and pick out its
and require it to ensure we modify the actual mocha being used. I ran into some issue that lead me down this path.
require('mocha') I would end up with a different instance then was being used inside the test context.
Mocha.Runner.runSuite is patched to add a beforeEach and afterEach that invokes the sinon magic.
Jasmine spoiled me.
MIT (see LICENSE)