From 9db610c0a6825fb69aad2a39ba7d97f3ea024602 Mon Sep 17 00:00:00 2001 From: Shuvalov Anton Date: Mon, 3 Aug 2015 00:10:57 +0300 Subject: [PATCH] re #8 add ability to set custom timeout for requests --- index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ef07555..5d3dc30 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,12 @@ mock.post = defineRoute.bind(null, 'POST'); mock.put = defineRoute.bind(null, 'PUT'); mock.del = defineRoute.bind(null, 'DELETE'); +/** + * Request timeout + * @type {number|function} + */ +mock.timeout = 0; + /** * List of registred callbacks */ @@ -44,7 +50,7 @@ function mock(superagent) { if (current) { setTimeout(function() { cb && cb(null, current()); - }, 0); + }, value(mock.timeout)); } else { oldEnd.call(this, cb); } @@ -125,4 +131,12 @@ Route.prototype.match = function(method, url, body) { }; }; - +/** + * Exec function and return value, or just return arg + * @param {fn|any} val Value or fn to exec + */ +function value(val) { + return 'function' === typeof val + ? val() + : val; +}