sinon-chai
Advanced tools
Comparing version 1.0.0 to 1.1.0
/*jshint | ||
curly: true, eqeqeq: true, immed: true, latedef: true, newcap: true, noarg: true, nonew: true, trailing: true, | ||
undef: true, white: true, es5: true, globalstrict: true, node: true */ | ||
"use strict"; | ||
undef: true, white: true, es5: true, strict: true, node: true */ | ||
/*global define: false */ | ||
module.exports = function (chai) { | ||
(function (sinonChai) { | ||
"use strict"; | ||
// Module systems magic dance. | ||
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): attach to global. | ||
var global = (false || eval)("this"); | ||
global.sinonChai = sinonChai; | ||
} | ||
}(function sinonChai(chai) { | ||
"use strict"; | ||
var slice = Function.prototype.call.bind(Array.prototype.slice); | ||
function flagger(word) { | ||
@@ -34,32 +56,33 @@ Object.defineProperty(chai.Assertion.prototype, word, { | ||
function sinonProperty(name, action) { | ||
function getMessages(spy, action, nonNegatedSuffix, always, args) { | ||
var verbPhrase = always ? "always have " : "have "; | ||
nonNegatedSuffix = nonNegatedSuffix || ""; | ||
var printfArray = Function.prototype.apply.bind(spy.printf, spy); | ||
return { | ||
affirmative: printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args)), | ||
negative: printfArray(["expected %n to not " + verbPhrase + action].concat(args)) | ||
}; | ||
} | ||
function sinonProperty(name, action, nonNegatedSuffix) { | ||
property(name, function () { | ||
this.assert(this.obj[name], | ||
"expected " + this.obj.displayName + " to have been " + action, | ||
"expected " + this.obj.displayName + " to not have been " + action); | ||
var messages = getMessages(this.obj, action, nonNegatedSuffix, false); | ||
this.assert(this.obj[name], messages.affirmative, messages.negative); | ||
}); | ||
} | ||
function stringify(x) { | ||
return x && x.displayName ? x.displayName : chai.inspect(x); | ||
} | ||
function exceptionalSinonMethod(chaiName, sinonName, action) { | ||
function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) { | ||
method(chaiName, function () { | ||
var argsString = Array.prototype.slice.call(arguments).map(stringify).join(", "); | ||
var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1); | ||
var shouldBeAlways = this._always && typeof this.obj[alwaysSinonMethod] === "function"; | ||
var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName; | ||
var verbPhrase = shouldBeAlways ? "always have " : "have "; | ||
this.assert(this.obj[sinonMethod].apply(this.obj, arguments), | ||
"expected " + this.obj.displayName + " to " + verbPhrase + action + " " + argsString, | ||
"expected " + this.obj.displayName + " to not " + verbPhrase + action + " " + argsString); | ||
var messages = getMessages(this.obj, action, nonNegatedSuffix, shouldBeAlways, slice(arguments)); | ||
this.assert(this.obj[sinonMethod].apply(this.obj, arguments), messages.affirmative, messages.negative); | ||
}); | ||
} | ||
function sinonMethod(name, action) { | ||
exceptionalSinonMethod(name, name, action); | ||
function sinonMethod(name, action, nonNegatedSuffix) { | ||
exceptionalSinonMethod(name, name, action, nonNegatedSuffix); | ||
} | ||
@@ -69,13 +92,13 @@ | ||
sinonProperty("called", "been called"); | ||
sinonProperty("calledOnce", "been called exactly once"); | ||
sinonProperty("calledTwice", "been called exactly twice"); | ||
sinonProperty("calledThrice", "been called exactly thrice"); | ||
sinonMethod("calledBefore", "been called before"); | ||
sinonMethod("calledAfter", "been called after"); | ||
sinonMethod("calledOn", "been called on"); | ||
sinonMethod("calledWith", "been called with"); | ||
sinonMethod("calledWithExactly", "been called with exactly"); | ||
sinonMethod("returned", "returned"); | ||
exceptionalSinonMethod("thrown", "threw", "thrown"); | ||
}; | ||
sinonProperty("called", "been called", " at least once, but it was never called"); | ||
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"); | ||
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("returned", "returned %1"); | ||
exceptionalSinonMethod("thrown", "threw", "thrown %1"); | ||
})); |
{ | ||
"name": "sinon-chai", | ||
"description": "Extends chai with assertions for the Sinon.JS mocking framework.", | ||
"description": "Extends Chai with assertions for the Sinon.JS mocking framework.", | ||
"keywords": [ | ||
@@ -12,3 +12,3 @@ "sinon", | ||
], | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenicdenicola.com)", | ||
@@ -27,4 +27,4 @@ "repository": { | ||
"scripts": { | ||
"test": "node ./node_modules/mocha/bin/mocha", | ||
"lint": "node ./node_modules/jshint/bin/hint ./lib" | ||
"test": "mocha", | ||
"lint": "jshint ./lib --show-non-errors" | ||
}, | ||
@@ -36,9 +36,9 @@ "engines": { | ||
"chai": ">=0.3.3", | ||
"sinon": "~1.3.1" | ||
"sinon": "1" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "~1.2.0", | ||
"mocha": "~0.12.0", | ||
"jshint": "~0.5.7" | ||
"coffee-script": "1", | ||
"mocha": "*", | ||
"jshint": "*" | ||
} | ||
} |
@@ -1,4 +0,5 @@ | ||
# Sinon.JS Assertions for Chai | ||
Sinon.JS Assertions for Chai | ||
============================ | ||
**sinon-chai** provides a set of custom assertions for using the [Sinon.JS][sinon] spy, stub, and mocking framework | ||
**Sinon–Chai** provides a set of custom assertions for using the [Sinon.JS][sinon] spy, stub, and mocking framework | ||
with the [Chai][chai] assertion library. You get all the benefits of Chai with all the powerful tools of Sinon.JS. | ||
@@ -28,3 +29,3 @@ | ||
All of your favorite Sinon.JS assertions made their way into sinon-chai. We show the `should` syntax here; the `expect` | ||
All of your favorite Sinon.JS assertions made their way into Sinon–Chai. We show the `should` syntax here; the `expect` | ||
equivalent is also available. | ||
@@ -35,3 +36,3 @@ | ||
<th>Sinon.JS property/method</th> | ||
<th>sinon-chai assertion</th> | ||
<th>Sinon–Chai assertion</th> | ||
</tr> | ||
@@ -111,2 +112,4 @@ <tr> | ||
### Node | ||
Do an `npm install sinon-chai` to get up and running. Then: | ||
@@ -122,5 +125,29 @@ | ||
You can of course put this code in a common test fixture file; for an example using [Mocha][mocha], see | ||
[the sinon-chai tests themselves][fixturedemo]. | ||
[the Sinon–Chai tests themselves][fixturedemo]. | ||
### AMD | ||
Sinon–Chai supports being used as an [AMD][amd] module, registering itself anonymously (just like Chai). So, assuming | ||
you have configured your loader to map the Chai and Sinon–Chai files to the respective module IDs `"chai"` and | ||
`"sinon-chai"`, you can use them as follows: | ||
```javascript | ||
define(function (require, exports, module) { | ||
var chai = require("chai"); | ||
var sinonChai = require("sinon-chai"); | ||
chai.use(sinonChai); | ||
}); | ||
``` | ||
### `<script>` tag | ||
If you include Sinon–Chai directly with a `<script>` tag, it creates a `window.sinonChai` global (again, just like | ||
Chai). Then your setup code becomes: | ||
```javascript | ||
window.chai.use(window.sinonChai); | ||
``` | ||
[sinon]: http://sinonjs.org/ | ||
@@ -131,1 +158,2 @@ [chai]: http://chaijs.com/ | ||
[spymethods]: http://sinonjs.org/docs/#spies-api | ||
[amd]: https://github.com/amdjs/amdjs-api/wiki/AMD |
@@ -7,3 +7,9 @@ global.sinon = require("sinon"); | ||
global.swallow = function (thrower) { | ||
try { | ||
thrower(); | ||
} catch (e) { } | ||
}; | ||
var sinonChai = require("../lib/sinon-chai"); | ||
chai.use(sinonChai); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
41259
14
0
99
155
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedformatio@1.1.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedlolex@1.3.2(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedsamsam@1.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsinon@1.17.7(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removedsinon@1.3.4(transitive)
Updatedsinon@1