Reputation: 26122
Are there any worthwhile alternatives for Sinon.js?
Thanks.
Upvotes: 6
Views: 5068
Reputation: 11
I just started a new project called candy-wrapper that may be an alternative to Sinon in some instances: https://www.npmjs.com/package/candy-wrapper
Here are some examples of how to use it, I would love feedback if anyone has any insights as to how to make it better:
var Wrapper = require("candy-wrapper");
// a simple test object
var myDrone = {
name: "DJI",
fly: function(direction) {
return true;
}
}
new Wrapper(myDrone, "name");
new Wrapper(myDrone, "fly");
myDrone.fly("north");
myDrone.fly("west");
// evaluating previous calls through the 'historyList' and 'Filters'
myDrone.fly.historyList.filterFirst().expectCallArgs("north"); // true
myDrone.fly.historyList.filterSecond().expectCallArgs("east"); // false
myDrone.fly.expectReportAllFailtures(); // throws an error about the "east" expecation failing
// modifying behavior using 'Triggers'
myDrone.fly.triggerOnCallArgs("east").actionReturn(false); // will return 'false' when called with "east"
myDrone.fly("east"); // false
myDrone.fly("west"); // true (the default return value)
// working with properties
myDrone.name.triggerOnSet().actionThrowException(new Error("do not set the name"));
myDrone.name = "Bob"; // throws Error: "do not set the name"
var ret = myDrone.name; // ret = "DJI"
Upvotes: 0
Reputation: 27889
There is also library called testdouble.js. Which is a kind of more object-oriented than sinon.js.
Also, this article from testdouble guys explain the differences between sinon.js and testdouble.js.
var td = require('testdouble');
var fetch = td.function();
td.when(fetch(42)).thenReturn('Jane User');
fetch(42); // -> 'Jane User'
Upvotes: 2