Skip to content

Commit 9a51d00

Browse files
committed
Duck type Promise type - Fix #24
1 parent a53bb9c commit 9a51d00

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/run-context.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ RunContext.prototype.toPromise = function () {
126126
}.bind(this));
127127
};
128128

129+
/**
130+
* Promise `.then()` duck typing
131+
* @return {Promise}
132+
*/
133+
RunContext.prototype.then = function () {
134+
var promise = this.toPromise();
135+
return promise.then.apply(promise, arguments);
136+
};
137+
138+
139+
/**
140+
* Promise `.catch()` duck typing
141+
* @return {Promise}
142+
*/
143+
RunContext.prototype.catch = function () {
144+
var promise = this.toPromise();
145+
return promise.catch.apply(promise, arguments);
146+
};
147+
129148
/**
130149
* Clean the provided directory, then change directory into it
131150
* @param {String} dirPath - Directory path (relative to CWD). Prefer passing an absolute

test/run-context.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,40 @@ describe('RunContext', function () {
227227
});
228228
});
229229

230+
describe('#then()', function () {
231+
it('handle success', function () {
232+
return this.ctx.then(function (dir) {
233+
assert.equal(this.ctx.targetDirectory, dir);
234+
}.bind(this));
235+
});
236+
237+
it('handles errors', function () {
238+
var error = new Error();
239+
var Dummy = helpers.createDummyGenerator();
240+
var execSpy = sinon.stub().throws(error);
241+
Dummy.prototype.exec = execSpy;
242+
var ctx = new RunContext(Dummy);
243+
244+
return ctx.then(function () {}, function (err) {
245+
assert.equal(err, error);
246+
});
247+
});
248+
});
249+
250+
describe('#catch()', function () {
251+
it('handles errors', function () {
252+
var error = new Error();
253+
var Dummy = helpers.createDummyGenerator();
254+
var execSpy = sinon.stub().throws(error);
255+
Dummy.prototype.exec = execSpy;
256+
var ctx = new RunContext(Dummy);
257+
258+
return ctx.catch(function (err) {
259+
assert.equal(err, error);
260+
});
261+
});
262+
});
263+
230264
describe('#inDir()', function () {
231265
beforeEach(function () {
232266
process.chdir(__dirname);

0 commit comments

Comments
 (0)