candy-wrapper
Advanced tools
Comparing version 0.8.2 to 0.8.3
{ | ||
"name": "candy-wrapper", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "Wrap your functions, methods, and properties to monitor them and modify their behavior.", | ||
@@ -12,3 +12,3 @@ "main": "main.js", | ||
"test:browsers": "grunt default", | ||
"test:node": "istanbul cover _mocha", | ||
"test:node": "istanbul cover _mocha test/nodeTest.js", | ||
"docs:create": "rm -rf \"\" ./docs && jsdoc -c ./.jsdoc-conf.json", | ||
@@ -15,0 +15,0 @@ "docs:publish": "gh-pages --silent --repo https://$GH_TOKEN@github.com/apowers313/candy-wrapper.git --dist docs" |
@@ -1042,2 +1042,4 @@ var assert = assert || chai.assert; | ||
it("property exception works with configSwallowExpectException"); | ||
it("function exception", function() { | ||
@@ -1146,2 +1148,4 @@ var w = new Wrapper(); | ||
it("exception works with configSwallowExpectException"); | ||
it("call custom", function() { | ||
@@ -1148,0 +1152,0 @@ var testFunc = function() { |
@@ -86,4 +86,17 @@ var assert = assert || chai.assert; | ||
it("throws when getInterface gets bad args"); | ||
it("throws when getInterface gets bad args", function() { | ||
var mod = new Module(); | ||
// bad arg | ||
assert.throws(function() { | ||
mod.getInterface(42); | ||
}, TypeError, "getInterface: expected a single argument of type String"); | ||
// no arg | ||
assert.throws(function() { | ||
mod.getInterface(); | ||
}, TypeError, "getInterface: expected a single argument of type String"); | ||
}); | ||
it("can chain on interface", function() { | ||
@@ -119,4 +132,16 @@ var mod = new Module(); | ||
it("throws when getBehavior gets bad args"); | ||
it("throws when getBehavior gets bad args", function() { | ||
var mod = new Module(); | ||
// not a string | ||
assert.throws(function() { | ||
mod.getBehavior(42); | ||
}, TypeError, "getBehavior: expected a single argument of type String"); | ||
// missing arg | ||
assert.throws(function() { | ||
mod.getBehavior(); | ||
}, TypeError, "getBehavior: expected a single argument of type String"); | ||
}); | ||
it("can't define existing behavior", function() { | ||
@@ -215,4 +240,4 @@ var mod = new Module(); | ||
var stub = mod.getStub("getUserSuccess"); | ||
assert.isTrue(Wrapper.isWrapper(stub)); | ||
var ret = stub(); | ||
assert.isTrue(Wrapper.isWrapper(stub.getUser)); | ||
var ret = stub.getUser(); | ||
assert.deepEqual(ret, { | ||
@@ -235,6 +260,6 @@ user: "Adam" | ||
var stub = mod.getStub("getUsers"); | ||
assert.isTrue(Wrapper.isWrapper(stub)); | ||
assert.isTrue(Wrapper.isWrapper(stub.getUser)); | ||
// first call | ||
var ret = stub(); | ||
var ret = stub.getUser(); | ||
assert.deepEqual(ret, { | ||
@@ -245,3 +270,3 @@ user: "Adam" | ||
// second call | ||
ret = stub(); | ||
ret = stub.getUser(); | ||
assert.deepEqual(ret, { | ||
@@ -252,6 +277,330 @@ user: "Adam" | ||
// third call | ||
ret = stub(); | ||
ret = stub.getUser(); | ||
assert.isUndefined(ret); | ||
}); | ||
it("can create a multi-stub", function() { | ||
var mod = new Module(); | ||
// methods | ||
mod.defineMethod("getUser"); | ||
mod.defineMethod("createUser"); | ||
// behaviors | ||
mod.defineBehavior("getUserFail", "getUser") | ||
.throws(new Error("User not found")); | ||
mod.defineBehavior("createUserSuccess", "createUser") | ||
.returns(true); | ||
mod.defineBehavior("getUserSuccess", "getUser") | ||
.returns("Adam"); | ||
mod.defineBehavior("createAccount") | ||
.getUserFail() | ||
.createUserSuccess() | ||
.getUserSuccess(); | ||
// create stub | ||
var stub = mod.getStub("createAccount"); | ||
assert.isTrue(Wrapper.isWrapper(stub.getUser)); | ||
assert.isTrue(Wrapper.isWrapper(stub.createUser)); | ||
var ret; | ||
// first call | ||
assert.throws(function() { | ||
stub.getUser(); | ||
}, Error, "User not found"); | ||
// second call | ||
ret = stub.createUser(); | ||
assert.strictEqual(ret, true); | ||
// third call | ||
ret = stub.getUser(); | ||
assert.strictEqual(ret, "Adam"); | ||
// all done | ||
ret = stub.getUser(); | ||
assert.isUndefined(ret); | ||
ret = stub.createUser(); | ||
assert.isUndefined(ret); | ||
}); | ||
it("can stub arguments", function() { | ||
var mod = new Module(); | ||
// methods | ||
mod.defineMethod("getUser"); | ||
// behaviors | ||
mod.defineBehavior("getUserMissing", "getUser") | ||
.args("Adam") | ||
.returns(false); | ||
// create stub | ||
var stub = mod.getStub("getUserMissing"); | ||
// first call | ||
var ret = stub.getUser("Adam"); | ||
assert.isFalse(ret); | ||
assert.isTrue(Wrapper.isWrapper(stub.getUser)); | ||
var callArgs = stub.getUser.historyList.getAllCallArgs(); | ||
assert.isArray(callArgs); | ||
assert.strictEqual(callArgs.length, 1); | ||
assert.deepEqual(callArgs[0], ["Adam"]); | ||
}); | ||
it("can stub context", function() { | ||
var mod = new Module(); | ||
// methods | ||
mod.defineMethod("getUser"); | ||
// behaviors | ||
mod.defineBehavior("getUserMissing", "getUser") | ||
.context({ | ||
ctx: true, | ||
name: "beer" | ||
}) | ||
.returns(false); | ||
// create stub | ||
var stub = mod.getStub("getUserMissing"); | ||
// first call | ||
var ret = stub.getUser.call({ | ||
ctx: true, | ||
name: "beer" | ||
}); | ||
assert.isFalse(ret); | ||
assert.isTrue(Wrapper.isWrapper(stub.getUser)); | ||
var callContexts = stub.getUser.historyList.getAllCallContexts(); | ||
assert.isArray(callContexts); | ||
assert.strictEqual(callContexts.length, 1); | ||
assert.deepEqual(callContexts[0], { | ||
ctx: true, | ||
name: "beer" | ||
}); | ||
}); | ||
it("can stub exception"); | ||
it("can create property stub"); | ||
}); | ||
describe("defineTest", function() { | ||
it("throws on bad args", function() { | ||
var mod = new Module(); | ||
// doesn't exist | ||
assert.throws(function() { | ||
mod.defineTest("asdfqwer"); | ||
}, TypeError, "defineTest: behavior 'asdfqwer' not defined"); | ||
// bad args | ||
assert.throws(function() { | ||
mod.defineTest(); | ||
}, TypeError, "defineTest: expected argument 'behaviorName' to be of type String"); | ||
assert.throws(function() { | ||
mod.defineTest(42); | ||
}, TypeError, "defineTest: expected argument 'behaviorName' to be of type String"); | ||
mod.defineBehavior("getUserSuccess"); | ||
assert.throws(function() { | ||
mod.defineTest("getUserSuccess", 42); | ||
}, TypeError, "defineTest: expection optional argument 'desc' to be of type String"); | ||
}); | ||
it("defines a test", function() { | ||
var mod = new Module(); | ||
mod.defineBehavior("getUserSuccess"); | ||
mod.defineTest("getUserSuccess"); | ||
assert.strictEqual(mod.testList.length, 1); | ||
assert.strictEqual(mod.testList[0].behaviorName, "getUserSuccess"); | ||
}); | ||
it("uses behavior name as default description", function() { | ||
var mod = new Module(); | ||
mod.defineBehavior("getUserSuccess"); | ||
mod.defineTest("getUserSuccess"); | ||
assert.strictEqual(mod.testList[0].behaviorName, "getUserSuccess"); | ||
assert.strictEqual(mod.testList[0].desc, "getUserSuccess"); | ||
}); | ||
it("set desc explicitly", function() { | ||
var mod = new Module(); | ||
mod.defineBehavior("getUserSuccess"); | ||
mod.defineTest("getUserSuccess", "ensures user success"); | ||
assert.strictEqual(mod.testList[0].behaviorName, "getUserSuccess"); | ||
assert.strictEqual(mod.testList[0].desc, "ensures user success"); | ||
}); | ||
it("can test exception", function() { | ||
var mod = new Module(); | ||
mod.defineMethod("getUser"); | ||
mod.defineBehavior("getUserError") | ||
.getUser() | ||
.throws(new Error("user not found")); | ||
mod.defineTest("getUserError", "throws error on failure"); | ||
// pass | ||
var myMod = { | ||
getUser: function() { | ||
throw new Error("user not found"); | ||
} | ||
}; | ||
mod.runAllTests(myMod, mochaIt); | ||
// fail - no error | ||
myMod.getUser = function() {}; | ||
assert.throws(function() { | ||
mod.runAllTests(myMod, mochaIt); | ||
}, ExpectError, "expectException: expectation failed for: Error: user not found"); | ||
// fail - wrong error | ||
myMod.getUser = function() { | ||
throw new Error("out of memory"); | ||
}; | ||
assert.throws(function() { | ||
mod.runAllTests(myMod, mochaIt); | ||
}, ExpectError, "expectException: expectation failed for: Error: user not found"); | ||
}); | ||
it("can set test args", function() { | ||
var mod = new Module(); | ||
// method | ||
mod.defineMethod("getUser"); | ||
var myMod = { | ||
getUser: function(...args) { | ||
assert.deepEqual(args, [1, undefined, false, "God"]); | ||
} | ||
}; | ||
// behavior | ||
mod.defineBehavior("getUserArgs") | ||
.getUser() | ||
.args(1, undefined, false, "God"); | ||
mod.defineTest("getUserArgs", "tests the getUserArgs"); | ||
// pass | ||
mod.runAllTests(myMod, mochaIt); | ||
}); | ||
it("can set test context", function() { | ||
var mod = new Module(); | ||
// method | ||
mod.defineMethod("getUser"); | ||
var myMod = { | ||
getUser: function() { | ||
assert.deepEqual(this, { | ||
happy: true, | ||
name: "bob", | ||
sumthin: "yup" | ||
}); | ||
} | ||
}; | ||
// behavior | ||
mod.defineBehavior("getUserArgs") | ||
.getUser() | ||
.context({ | ||
happy: true, | ||
name: "bob", | ||
sumthin: "yup" | ||
}); | ||
mod.defineTest("getUserArgs", "tests the getUserArgs"); | ||
// pass | ||
mod.runAllTests(myMod, mochaIt); | ||
}); | ||
it("does set value"); | ||
it("can test get value"); | ||
}); | ||
describe("_testFunctionFactory", function() { | ||
it("creates a function", function() { | ||
var mod = new Module(); | ||
var behav = mod.defineBehavior("testBehavior"); | ||
var fn = mod._testFunctionFactory(behav); | ||
assert.isFunction(fn); | ||
fn(); | ||
}); | ||
}); | ||
function mochaIt(desc, fn) { | ||
// console.log(" " + desc); | ||
fn(); | ||
} | ||
describe("getTestList", function() { | ||
it("returns a test list", function() { | ||
var mod = new Module(); | ||
mod.defineBehavior("getUserSuccess"); | ||
mod.defineTest("getUserSuccess", "successful get user"); | ||
var testList = mod.getTestList(); | ||
assert.isArray(testList); | ||
assert.strictEqual(testList.length, 1); | ||
assert.strictEqual(testList[0].behaviorName, "getUserSuccess"); | ||
}); | ||
}); | ||
describe("test", function() { | ||
it("throws if interace isn't defined", function() { | ||
var mod = new Module(); | ||
mod.defineMethod("getUser"); | ||
mod.defineBehavior("getUserSuccess").getUser().returns("Adam"); | ||
mod.defineTest("getUserSuccess", "successful get user"); | ||
var testList = mod.getTestList(); | ||
assert.throws(function() { | ||
mochaIt(testList[0].desc, testList[0].fn({ | ||
module: "test" | ||
})); | ||
}, Error, "runTest: expected property 'getUser' to exist on module"); | ||
}); | ||
it("throws on bad return value", function() { | ||
var mod = new Module(); | ||
mod.defineMethod("getUser"); | ||
mod.defineBehavior("getUserSuccess").getUser().returns("Adam"); | ||
mod.defineTest("getUserSuccess", "successful get user"); | ||
var testList = mod.getTestList(); | ||
var myMod = { | ||
name: "myMod", | ||
getUser: function() { | ||
return "Bob"; | ||
} | ||
}; | ||
assert.throws(function() { | ||
mochaIt(testList[0].desc, testList[0].fn(myMod)); | ||
}, ExpectError, "expectReturn: expectation failed for: Adam"); | ||
}); | ||
it("passes good expect", function() { | ||
var mod = new Module(); | ||
mod.defineMethod("getUser"); | ||
mod.defineBehavior("getUserSuccess").getUser().returns("Adam"); | ||
mod.defineTest("getUserSuccess", "successful get user"); | ||
var testList = mod.getTestList(); | ||
var myMod = { | ||
getUser: function() { | ||
return "Adam"; | ||
} | ||
}; | ||
mochaIt(testList[0].desc, testList[0].fn(myMod)); | ||
}); | ||
}); | ||
}); |
@@ -569,2 +569,53 @@ var assert = assert || chai.assert; | ||
it("can swallow exception", function() { | ||
var testFunc = function() { | ||
throw new Error("test exception"); | ||
}; | ||
testFunc = new Wrapper(testFunc); | ||
testFunc.triggerAlways().expectException(new Error("test exception")); | ||
// throws the error | ||
assert.throws(function() { | ||
testFunc(); | ||
}, Error, "test exception"); | ||
// swallows the error | ||
testFunc.configSwallowExpectException(true); | ||
assert.doesNotThrow(function() { | ||
testFunc(); | ||
}); | ||
// make sure the exception is still recorded | ||
assert.strictEqual(testFunc.historyList.length, 2); | ||
var errList = testFunc.historyList.getAllExceptions(); | ||
assert.strictEqual(errList[0].name, "Error"); | ||
assert.strictEqual(errList[0].message, "test exception"); | ||
assert.strictEqual(errList[0].name, "Error"); | ||
assert.strictEqual(errList[0].message, "test exception"); | ||
}); | ||
it("doesn't swallow different exception", function() { | ||
var testFunc = function() { | ||
throw new Error("this is not a test"); | ||
}; | ||
testFunc = new Wrapper(testFunc); | ||
assert.isTrue(Wrapper.isWrapper(testFunc)); | ||
testFunc.triggerAlways().expectException(new Error("test exception")); | ||
assert.throws(function() { | ||
testFunc(); | ||
}, Error, "this is not a test"); | ||
testFunc.configSwallowExpectException(true); | ||
assert.throws(function() { | ||
testFunc(); | ||
}, Error, "this is not a test"); | ||
// make sure the exception is still recorded | ||
assert.strictEqual(testFunc.historyList.length, 0); | ||
}); | ||
it("does setval", function() { | ||
@@ -621,4 +672,20 @@ var testObj = { | ||
it("prints diff", function() { | ||
var w = new Wrapper(); | ||
w.triggerAlways() | ||
.expectCallArgs("beer"); | ||
// fail | ||
assert.throws(function() { | ||
w("wine"); | ||
}, ExpectError, new RegExp( | ||
"expectCallArgs: expectation failed for: beer\n" + | ||
" +At \\[0\\]: Expected: 'beer'; Got: 'wine'\n", | ||
"g")); | ||
}) | ||
it("is chainable"); | ||
}); | ||
describe("trigger action", function() { | ||
@@ -983,2 +1050,55 @@ it("can spoof a call return value", function() { | ||
it("can set call arguments", function() { | ||
var testFunc = function(...args) { | ||
assert.deepEqual(args, [1, undefined, false, "God"]); | ||
}; | ||
testFunc = new Wrapper(testFunc); | ||
testFunc.triggerAlways() | ||
.actionCallArgs(1, undefined, false, "God"); | ||
// all pass, args replaced by trigger | ||
testFunc(); | ||
testFunc("foo"); | ||
testFunc(undefined); | ||
testFunc(null); | ||
testFunc([]); | ||
}); | ||
it("can set call call context", function() { | ||
var testFunc = function() { | ||
assert.deepEqual(this, { | ||
happy: true, | ||
name: "bob", | ||
sumthin: "yup" | ||
}); | ||
}; | ||
testFunc = new Wrapper(testFunc); | ||
testFunc.triggerAlways() | ||
.actionCallContext({ | ||
happy: true, | ||
name: "bob", | ||
sumthin: "yup" | ||
}); | ||
// all pass, args replaced by trigger | ||
testFunc.call(); | ||
testFunc.call({}); | ||
testFunc.call({ | ||
sumthin: "nope" | ||
}); | ||
testFunc.call(null); | ||
// trhows on too many args | ||
assert.throws(function() { | ||
testFunc.triggerAlways() | ||
.actionCallContext({ | ||
happy: true, | ||
name: "bob", | ||
sumthin: "yup" | ||
}, 2); | ||
}, TypeError, "actionCallContext: expected a single argument of any type"); | ||
}); | ||
it("can chain actions"); | ||
@@ -989,3 +1109,2 @@ it("actionCallbackAsync"); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
391903
7911
0