Skip to content

Instantly share code, notes, and snippets.

@hokaccha
Last active December 16, 2015 09:19
Show Gist options
  • Save hokaccha/5412476 to your computer and use it in GitHub Desktop.
Save hokaccha/5412476 to your computer and use it in GitHub Desktop.
mocha.sinon = {
test: function(fn) {
var sandbox = sinon.sandbox.create();
if (fn.length >= 2) {
return function(done) {
var origOnError = window.onerror;
window.onerror = function() {
sandbox.restore();
origOnError.apply(this, arguments);
};
try {
fn.call(this, sandbox, function(err) {
sandbox.verifyAndRestore();
done(err);
});
} catch (e) {
sandbox.restore();
throw e;
}
};
}
else {
return function() {
var exception;
try {
fn.call(this, sandbox);
}
catch (e) {
exception = e;
}
if (exception) {
sandbox.restore();
throw exception;
}
else {
sandbox.verifyAndRestore();
}
};
}
}
};
describe('Foo', function() {
beforeEach(function() {
this.foo = 'bar';
});
it('foo', mocha.sinon.test(function(sandbox, done) {
expect(this.foo).to.be('bar');
sandbox.stub(window, 'confirm').returns(true);
setTimeout(function() {
expect(window.confirm()).to.ok();
done();
}, 1000);
}));
it('bar', mocha.sinon.test(function(sandbox) {
// confirm実行される(stubがリストアされてる)
expect(window.confirm()).to.ok();
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment