Last active
August 29, 2015 14:22
-
-
Save thehig/4c36ca8870d4d71e5ce3 to your computer and use it in GitHub Desktop.
sinon-chai in browser error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Mocha Tests</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.css" /> | |
</head> | |
<body> | |
<div id="mocha"></div> | |
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/2.3.0/chai.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js"></script> | |
<script> | |
(function (sinonChai) { | |
"use strict"; | |
// Module systems magic dance. | |
/* istanbul ignore else */ | |
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") { | |
// NodeJS | |
module.exports = sinonChai; | |
} else if (typeof define === "function" && define.amd) { | |
// AMD | |
define(function () { | |
return sinonChai; | |
}); | |
} else { | |
// Other environment (usually <script> tag): plug in to global chai instance directly. | |
chai.use(sinonChai); | |
} | |
}(function sinonChai(chai, utils) { | |
"use strict"; | |
var slice = Array.prototype.slice; | |
function isSpy(putativeSpy) { | |
return typeof putativeSpy === "function" && | |
typeof putativeSpy.getCall === "function" && | |
typeof putativeSpy.calledWithExactly === "function"; | |
} | |
function timesInWords(count) { | |
return count === 1 ? "once" : | |
count === 2 ? "twice" : | |
count === 3 ? "thrice" : | |
(count || 0) + " times"; | |
} | |
function isCall(putativeCall) { | |
return putativeCall && isSpy(putativeCall.proxy); | |
} | |
function assertCanWorkWith(assertion) { | |
if (!isSpy(assertion._obj) && !isCall(assertion._obj)) { | |
throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!"); | |
} | |
} | |
function getMessages(spy, action, nonNegatedSuffix, always, args) { | |
var verbPhrase = always ? "always have " : "have "; | |
nonNegatedSuffix = nonNegatedSuffix || ""; | |
if (isSpy(spy.proxy)) { | |
spy = spy.proxy; | |
} | |
function printfArray(array) { | |
return spy.printf.apply(spy, array); | |
} | |
return { | |
affirmative: function () { | |
return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args)); | |
}, | |
negative: function () { | |
return printfArray(["expected %n to not " + verbPhrase + action].concat(args)); | |
} | |
}; | |
} | |
function sinonProperty(name, action, nonNegatedSuffix) { | |
utils.addProperty(chai.Assertion.prototype, name, function () { | |
assertCanWorkWith(this); | |
var messages = getMessages(this._obj, action, nonNegatedSuffix, false); | |
this.assert(this._obj[name], messages.affirmative, messages.negative); | |
}); | |
} | |
function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) { | |
utils.addMethod(chai.Assertion.prototype, name, function (arg) { | |
assertCanWorkWith(this); | |
var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]); | |
this.assert(this._obj[name] === arg, messages.affirmative, messages.negative); | |
}); | |
} | |
function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) { | |
return function () { | |
assertCanWorkWith(this); | |
var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1); | |
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function"; | |
var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName; | |
var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments)); | |
this.assert(this._obj[sinonMethod].apply(this._obj, arguments), messages.affirmative, messages.negative); | |
}; | |
} | |
function sinonMethodAsProperty(name, action, nonNegatedSuffix) { | |
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix); | |
utils.addProperty(chai.Assertion.prototype, name, handler); | |
} | |
function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) { | |
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix); | |
utils.addMethod(chai.Assertion.prototype, chaiName, handler); | |
} | |
function sinonMethod(name, action, nonNegatedSuffix) { | |
exceptionalSinonMethod(name, name, action, nonNegatedSuffix); | |
} | |
utils.addProperty(chai.Assertion.prototype, "always", function () { | |
utils.flag(this, "always", true); | |
}); | |
sinonProperty("called", "been called", " at least once, but it was never called"); | |
sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", ", but it was called %c%C"); | |
sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C"); | |
sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C"); | |
sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C"); | |
sinonMethodAsProperty("calledWithNew", "been called with new"); | |
sinonMethod("calledBefore", "been called before %1"); | |
sinonMethod("calledAfter", "been called after %1"); | |
sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead"); | |
sinonMethod("calledWith", "been called with arguments %*", "%C"); | |
sinonMethod("calledWithExactly", "been called with exact arguments %*", "%C"); | |
sinonMethod("calledWithMatch", "been called with arguments matching %*", "%C"); | |
sinonMethod("returned", "returned %1"); | |
exceptionalSinonMethod("thrown", "threw", "thrown %1"); | |
})); | |
</script> | |
<script> | |
mocha.setup('bdd'); | |
</script> | |
<script> | |
function sinonTest(parameter, callback) { | |
callback(parameter); | |
} | |
var expect = chai.expect; | |
describe("sinonTest", function () { | |
var param; | |
beforeEach(function() { | |
param = "Hello World"; | |
spy = sinon.spy(); | |
}); | |
it("should have a sinonTest function that returns the parameter to the callback function", function(done) { | |
sinonTest(param, function(testOutput) { | |
expect(testOutput).to.equal(param); | |
done(); | |
}); | |
}); | |
it("should call the callback once", function() { | |
sinonTest(param, spy); | |
expect(spy).to.have.been.calledOnce; | |
}); | |
it("should call the callback with the correct parameter (chai expect)", function () { | |
sinonTest(param, spy); | |
expect(spy.args[0][0]).to.equal(param); | |
}); | |
it("should call the callback with the correct parameter (sinon-chai expect)", function () { | |
sinonTest(param, spy); | |
expect(spy).to.have.been.calledWith(param); | |
}); | |
describe("Intentionally failing tests", function() { | |
it("should fail the test with an appropriate message (chai)", function () { | |
sinonTest(param, spy); | |
expect(spy.args[0][0]).to.not.equal(param); | |
}); | |
it("should fail the test with an appropriate message (sinon-chai)", function () { | |
sinonTest(param, spy); | |
expect(spy).not.to.have.been.calledWith(param); | |
}); | |
}); | |
}); | |
</script> | |
<script> | |
//mocha.checkLeaks(); | |
mocha.globals(['jQuery']); | |
mocha.run(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment