PageRenderTime 51ms CodeModel.GetById 33ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mocha/error.js

https://github.com/mpmedia/bluebird
JavaScript | 131 lines | 116 code | 14 blank | 1 comment | 3 complexity | 54ef48ac3ce41dfbce4202a817722775 MD5 | raw file
  1"use strict";
  2
  3var assert = require("assert");
  4
  5var adapter = require("../../js/debug/bluebird.js");
  6var fulfilled = adapter.fulfilled;
  7var rejected = adapter.rejected;
  8var pending = adapter.pending;
  9var Promise = adapter;
 10
 11describe("Promise.prototype.error", function(){
 12    describe("catches stuff originating from explicit rejections", function() {
 13        specify("using constructor", function(done) {
 14            var e = new Error("sup");
 15            new Promise(function(resolve, reject) {
 16                reject(e);
 17            }).error(function(err){
 18                assert(err === e);
 19                done();
 20            });
 21        });
 22        specify("using Promise.reject", function(done) {
 23            var e = new Error("sup");
 24            Promise.reject(e).error(function(err) {
 25                assert(err === e);
 26                done();
 27            });
 28        });
 29        specify("using deferred", function(done) {
 30            var e = new Error("sup");
 31            var d = Promise.defer();
 32            d.promise.error(function(err) {
 33                assert(err === e);
 34                done();
 35            });
 36            d.reject(e);
 37        });
 38
 39        specify("using callback", function(done) {
 40            var e = new Promise.TypeError("sup");
 41            function callsback(a, b, c, fn) {
 42                fn(e);
 43            }
 44            callsback = Promise.promisify(callsback);
 45
 46            callsback(1, 2, 3).error(function(err) {
 47                assert(err === e);
 48                done();
 49            });
 50        });
 51    });
 52
 53    describe("does not catch stuff originating from thrown errors", function() {
 54        specify("using constructor", function(done) {
 55            var e = new Error("sup");
 56            new Promise(function(resolve, reject) {
 57                throw e;
 58            }).error(function(err) {
 59                assert.fail();
 60            }).caught(function(err){
 61                assert(err === e);
 62                done();
 63            });
 64        });
 65        specify("using thenable", function(done) {
 66            var e = new Error("sup");
 67            var thenable = {
 68                then: function(resolve, reject){
 69                    reject(e);
 70                }
 71            };
 72            Promise.cast(thenable).error(function(err) {
 73                console.error(err);
 74                assert.fail();
 75            }).caught(function(err) {
 76                assert(err === e);
 77                done();
 78            });
 79        });
 80        specify("using callback", function(done) {
 81            var e = new Error("sup");
 82            function callsback(a, b, c, fn) {
 83                throw e;
 84            }
 85            callsback = Promise.promisify(callsback);
 86
 87            callsback(1, 2, 3).error(function(err) {
 88                assert.fail();
 89            }).caught(function(err){
 90                assert(err === e);
 91                done();
 92            });
 93        });
 94    });
 95
 96    specify("gh-54-1", function(done) {
 97        function doThing(arg) {
 98          return new Promise(function (resolve, reject) {
 99            if (typeof arg !== "string") return reject(new Error("invalid thing"));
100          });
101        }
102
103        doThing().error(function(){
104            done();
105        }).caught(function(){
106            assert.fail();
107        });
108
109    });
110
111    specify("gh-54-2", function(done) {
112        function doBuggyThing(arg) {
113          return new Promise(function (resolve, rej) {
114            // arg2 & reject dont exist. this is buggy.
115            if (arg2 && typeof arg2 !== "string") return reject(new Error("invalid thing"));
116          });
117        }
118        var called = false;
119        doBuggyThing().error(function(){
120            called = true;
121        }).caught(function() {
122
123        });
124
125        setTimeout(function(){
126            assert(!called);
127            done();
128        }, 13);
129
130    });
131})