Skip to content

Commit 4037edf

Browse files
committed
add tests on edge-cases
1 parent f58814c commit 4037edf

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,59 @@ var assert = require('assert');
66
var Promise = require('./');
77

88
describe('Promise', function () {
9+
it('should throw without new', function () {
10+
assert.throws(function () {
11+
/* eslint-disable new-cap */
12+
var promise = Promise(function () {});
13+
/* eslint-enable new-cap */
14+
assert.ok(promise);
15+
}, /Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function\./);
16+
});
17+
918
it('should throw on invalid resolver type', function () {
1019
assert.throws(function () {
1120
var promise = new Promise('unicorns');
1221
assert.ok(promise);
1322
}, /Promise resolver unicorns is not a function/);
1423
});
24+
25+
it('should reject on exception in resolver', function (done) {
26+
new Promise(function () {
27+
throw new Error('Bang!');
28+
})
29+
.catch(function (err) {
30+
assert.equal(err.message, 'Bang!');
31+
done();
32+
});
33+
});
34+
35+
it('should reject on exception in then', function (done) {
36+
Promise.resolve(1)
37+
.then(function () {
38+
throw new Error('Bang!');
39+
})
40+
.catch(function (err) {
41+
assert.equal(err.message, 'Bang!');
42+
done();
43+
});
44+
});
45+
46+
it('should return Promise from resolve value', function (done) {
47+
Promise.resolve(Promise.resolve(1))
48+
.then(function (value) {
49+
assert.equal(value, 1);
50+
done();
51+
});
52+
});
1553
});
1654

1755
describe('Promise.all', function () {
56+
it('should throw error on invalid argument', function () {
57+
assert.throws(function () {
58+
Promise.all('unicorns');
59+
}, /You must pass an array to Promise.all()./);
60+
});
61+
1862
it('should resolve empty array to empty array', function (done) {
1963
Promise.all([]).then(function (value) {
2064
assert.deepEqual(value, []);
@@ -53,6 +97,12 @@ function delayedResolve() {
5397
}
5498

5599
describe('Promise.race', function () {
100+
it('should throw error on invalid argument', function () {
101+
assert.throws(function () {
102+
Promise.race('unicorns');
103+
}, /You must pass an array to Promise.race()./);
104+
});
105+
56106
it('empty array should be pending', function (done) {
57107
var p = Promise.race([]);
58108
setTimeout(function () {

0 commit comments

Comments
 (0)