javascript - Testing promises and sync functions that throw errors -
i'm trying build , test function @ same time. testing makes sense , love in theory, when comes down it pain in behind.
i have function takes string , throws errors when goes awry if goes it's going return original text argument , therefore truthy value, if not should caught promise it's either in or promise.
this test / want (which doesn't work).
var main = require("./index.js") var promise = require("bluebird") var mocha = require("mocha") var chai = require("chai") var chaipromise = require("chai-as-promised") chai.use(chaipromise) var shouldthrow = [ "random", // invalid non-flag "--random", // invalid flag "--random string", //invalid flag "--wallpaper", // invalid flag w/ match "--notify", // invalid flag w/ match "wallpaper", // valid non-flag missing option(s) image "wallpaper image.jpg" // invalid flag value "wallpaper http://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621", // invalid flag value "wallpaper //cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag value "wallpaper http://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag value "wallpaper https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag value "wallpaper https://cdn.example.com/s/files/1/0031/5352/files/holstee_logo_2.png?4803", // invalid flag value "wallpaper https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue", // invalid flag value "wallpaper https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().subtract(1, "month").format("yyyy-mm-dd-hh-mm"), // invalid flag value "wallpaper https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().add(1, "month").format("yy-mm-dd-hh"), // invalid flag value "wallpaper --image http://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621", // invalid flag value not https "wallpaper --image //cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag no protocol "wallpaper --image http://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag value not https "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/favicon.ico?12375621748379006621", // invalid flag value not valid image "wallpaper --image https://cdn.example.com/s/files/1/0031/5352/files/holstee_logo_2.png?4803", // invalid flag image not found "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue", // invalid subflag queue missing value "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().subtract(1, "month").format("yyyy-mm-dd-hh-mm"), // invalid subflag queue date value past "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().add(1, "month").format("yy-mm-dd-hh"), // invalid subflag queue date value format "--wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621", //no action non-flag "--wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().add(1, "month").format("yyyy-mm-dd-hh-mm"), //no action non-flag "notify", // valid non-flag missing option(s) message, open 'notify --message "hello world"', // valid flag missing params open 'notify --open "https://www.holstee.com"', // valid flag missing params message 'notify --message "hello world" --open "http://www.holstee.com"', // invalid subflag value `open` should https 'notify --message "hello world" --open "https://www.holstee.com" --queue', // invalid subflag queue missing value 'notify --message "hello world" --open "https://www.holstee.com" --queue '+moment().subtract(1, "month").format("yyyy-mm-dd-hh-mm"), // invalid subflag queue date value past 'notify --message "hello world" --open "https://www.holstee.com" --queue '+moment().add(1, "month").format("yy-mm-dd-hh"), // invalid subflag queue date value format '--notify --message "hello world" --open "https://www.holstee.com"', //no action non-flag '--notify --message "hello world" --open "https://www.holstee.com --queue "'+moment().add(1, "month").format("yyyy-mm-dd-hh-mm"), //no action non-flag ] var shouldnotthrow = [ 'notify --message "hello world" --open "https://www.holstee.com"', 'notify --message "hello world" --open "https://www.holstee.com --queue "'+moment().add(1, "month").format("yyyy-mm-dd-hh-mm"), "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621", "wallpaper --image https://cdn.shopify.com/s/files/1/0031/5352/t/28/assets/holstee-calendar-2015-03-flow-desktop.jpg?12375621748379006621 --queue "+moment().add(1, "month").format("yyyy-mm-dd-hh-mm"), ] describe('process text', function(){ return promise.map(shouldthrow, function(option){ it('throw error', function(){ return main.processtext(option).should.throw() }) }) return promise.map(shouldnotthrow, function(option){ it('throw error', function(){ return main.processtext(option).should.not.throw() }) }) }) here's snapshot of non-working* function i'm trying test.
main.processtext = function(text){ var args = minimist(text.split(" ")) var actions = _.keys(actionsflags) var flags = _.chain(_.map(actionsflags, _.keys)).flatten().uniq().value() var extraunparsed = _.extra(actions, args._) var providedflags = _.chain(args).keys().without("_").value() var extraparsed = _.extra(flags, providedflags) var validactions = _.intersection(actions, args._) var requiredflags = _.mapobject(actionsflags, function(flags){ return _.filterobject(flags, function(flag){ return flag }) }) if(extraunparsed.length) throw new error("invalid unparsed argument(s): "+extraunparsed.join(", ")) if(extraparsed.length) throw new error("invalid parsed argument(s): "+extraparsed.join(", ")) if(validactions.length > 1) throw new error("too many actions: "+validactions.join(", ")) if(validactions.length == 0) throw new error("no action: "+actions.join(", ")) _.each(actions, function(action){ var missingflags = _.missing(_.keys(requiredflags[action]), providedflags) var extraflags = _.extra(_.keys(requiredflags[action]), providedflags) if(_.contains(args._, action)){ if(missingflags.length) throw new error(util.format("missing required flags %s: %s", action, missingflags.join(", "))) if(extraflags.length) throw new error(util.format("extra flags %s: %s", action, extraflags.join(", "))) } }) return text } note not promise , doesn't return promises yet. 1 of validation features want check if url responds in 200 status code, that's gonna request promise. if update function of function contents need nested within promise.resolve(false).then()? perhaps promise shouldn't in block of code , async validation operations should exist somewhere else?
i don't know i'm doing , i'm little frustrated. i'm of course looking golden bullet or whatever make sense of this.
ideally use on how test kind of function. if make promise later on still want tests work.
here's example code of mean sync functions , promises.
function syncfunction(value){ if(!value) throw new error("missing value") return value } function asyncfunction(url){ return requestpromise(url) } // both of these throw errors same way caught promise can use `.catch` (in bluebird). promise.resolve(false).then(function(){ return syncfunction() }) promise.resolve(false).then(function(){ return asyncfunction("http://404.com") }) i want reflect way test errors , whether should or should not throw error in test.
i left promises out of it, it's sync function , i'm testing this.
describe('process text', function(){ _.each(shouldthrow, function(option){ it('throw error ('+option+')', function(){ expect(function(){ main.textvalidation(option) }).to.throw() }) }) _.each(shouldnotthrow, function(option){ it('not throw error ('+option+')', function(){ expect(function(){ main.textvalidation(option) }).to.not.throw() }) }) })
highly recommend npm install chai-as-promised
return dosomethingasync().should.eventually.equal("foo");
Comments
Post a Comment