UNPKG

5.22 kBJavaScriptView Raw
1var chai = require('chai');
2var Promise = require('bluebird');
3var async = require('../src/async/index');
4var await = require('../src/await/index');
5var expect = chai.expect;
6//TODO: tests for long stack traces across async calls?
7function runTestsFor(variant, acceptsCallback) {
8 if (acceptsCallback === void 0) { acceptsCallback = false; }
9 var name = 'async' + (variant ? ('.' + variant) : '');
10 var func = async;
11 if (variant)
12 variant.split('.').forEach(function (prop) { return func = func[prop]; });
13 var arity = function (fn) { return fn.length + (acceptsCallback ? 1 : 0); };
14 describe('The ' + name + '(...) function', function () {
15 //it('throws if not passed a single function', () => {
16 // expect(() => func.call(func, 1)).to.throw(Error);
17 // expect(() => func.call(func, 'sss')).to.throw(Error);
18 // expect(() => func.call(func, ()=>{}, true)).to.throw(Error);
19 // expect(() => func.call(func, ()=>{}, ()=>{})).to.throw(Error);
20 //});
21 it('synchronously returns a function', function () {
22 var foo = func(function () { });
23 expect(foo).to.be.a('function');
24 });
25 it('returns a function whose arity matches that of its definition', function () {
26 var defns = [
27 function (a, b, c) { },
28 function () { },
29 function (a, b, c, d, e, f, g, h) { },
30 function (x) { }
31 ];
32 for (var i = 0; i < defns.length; ++i) {
33 var foo = func(defns[i]);
34 expect(foo.length).to.equal(arity(defns[i]));
35 }
36 });
37 });
38}
39runTestsFor(null);
40runTestsFor('cps', true);
41runTestsFor('thunk');
42runTestsFor('iterable');
43describe('A suspendable function returned by async(...)', function () {
44 it('synchronously returns a promise', function () {
45 var foo = async(function () { });
46 var syncResult = foo();
47 expect(syncResult).instanceOf(Promise);
48 });
49 it('begins executing synchronously and completes asynchronously', function (done) {
50 var x = 5;
51 var foo = async(function () { x = 7; });
52 foo()
53 .then(function () { return expect(x).to.equal(9); })
54 .then(function () { return done(); })
55 .catch(done);
56 expect(x).to.equal(7);
57 x = 9;
58 });
59 it("preserves the 'this' context of the call", function (done) {
60 var foo = { bar: async(function () { return this; }) }, baz = { x: 7 };
61 foo.bar()
62 .then(function (result) { return expect(result).to.equal(foo); })
63 .then(function () { return foo.bar.call(baz); })
64 .then(function (result) { return expect(result).to.equal(baz); })
65 .then(function () { return done(); })
66 .catch(done);
67 });
68 it('eventually resolves with its definition\'s returned value', function (done) {
69 var foo = async(function () { return 'blah'; });
70 foo()
71 .then(function (result) { return expect(result).to.equal('blah'); })
72 .then(function () { return done(); })
73 .catch(done);
74 });
75 it('eventually rejects with its definition\'s thrown value', function (done) {
76 var act, exp = new Error('Expected thrown value to match rejection value');
77 var foo = async(function () { throw exp; return 'blah'; });
78 foo()
79 .catch(function (err) { return act = err; })
80 .then(function () {
81 if (!act)
82 done(new Error("Expected function to throw"));
83 else if (act !== exp)
84 done(exp);
85 else
86 done();
87 });
88 });
89 it('works with await', function (done) {
90 var foo = async(function () { return await(Promise.delay(20).then(function () { return 'blah'; })); });
91 foo()
92 .then(function (result) { return expect(result).to.equal('blah'); })
93 .then(function () { return done(); })
94 .catch(done);
95 });
96 it('triggers the global unhandledException event if a rejection goes unhandled', function (done) {
97 var foo = async(function () { throw new Error('nobody handled me'); });
98 foo(); // NB: no .catch()
99 var didTrigger = false;
100 process.on('unhandledRejection', function (err) {
101 didTrigger = true;
102 done();
103 });
104 setTimeout(function () {
105 if (!didTrigger)
106 done(new Error('unhandledRejection event not triggered'));
107 }, 20);
108 });
109 //it('emits progress with each yielded value', done => {
110 // var foo = async(() => { yield_(111); yield_(222); yield_(333); return 444; });
111 // var yields = [];
112 // (<Promise<any>> foo())
113 // .progressed(value => yields.push(value))
114 // .then(result => expect(result).to.equal(444))
115 // .then(() => expect(yields).to.deep.equal([111, 222, 333]))
116 // .then(() => done())
117 // .catch(done);
118 //});
119});
120//# sourceMappingURL=async.js.map
\No newline at end of file