Skip to content

Commit

Permalink
Merge 02b57ee into 3a276a8
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 20, 2019
2 parents 3a276a8 + 02b57ee commit 7359bab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
30 changes: 14 additions & 16 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
'use strict';
const mergePromiseProperty = (spawned, promise, property) => {
// Starting the main `promise` is deferred to avoid consuming streams
const value = typeof promise === 'function' ?
(...args) => promise()[property](...args) :
promise[property].bind(promise);

Object.defineProperty(spawned, property, {
value,
writable: true,
enumerable: false,
configurable: true
});
};

const descriptors = ['then', 'catch', 'finally'].map(property => [
property,
Reflect.getOwnPropertyDescriptor(Promise.prototype, property)
]);

// The return value is a mixin of `childProcess` and `Promise`
const mergePromise = (spawned, promise) => {
mergePromiseProperty(spawned, promise, 'then');
mergePromiseProperty(spawned, promise, 'catch');
mergePromiseProperty(spawned, promise, 'finally');
for (const [property, descriptor] of descriptors) {
// Starting the main `promise` is deferred to avoid consuming streams
const value = typeof promise === 'function' ?
(...args) => Reflect.apply(descriptor.value, promise(), args) :
descriptor.value.bind(promise);

Reflect.defineProperty(spawned, property, {...descriptor, value});
}

return spawned;
};

Expand Down
65 changes: 30 additions & 35 deletions test/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,39 @@ test('promise methods are not enumerable', t => {
// eslint-disable-next-line promise/prefer-await-to-then
t.false(descriptors.then.enumerable);
t.false(descriptors.catch.enumerable);
// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
t.false(descriptors.finally.enumerable);
}
t.false(descriptors.finally.enumerable);
});

// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
test('finally function is executed on success', async t => {
let isCalled = false;
const {stdout} = await execa('noop', ['foo']).finally(() => {
isCalled = true;
});
t.is(isCalled, true);
t.is(stdout, 'foo');
test('finally function is executed on success', async t => {
let isCalled = false;
const {stdout} = await execa('noop', ['foo']).finally(() => {
isCalled = true;
});

test('finally function is executed on failure', async t => {
let isError = false;
const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
isError = true;
}));
t.is(isError, true);
t.is(typeof stdout, 'string');
t.is(typeof stderr, 'string');
});
t.is(isCalled, true);
t.is(stdout, 'foo');
});

test('throw in finally function bubbles up on success', async t => {
const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});
test('finally function is executed on failure', async t => {
let isError = false;
const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
isError = true;
}));
t.is(isError, true);
t.is(typeof stdout, 'string');
t.is(typeof stderr, 'string');
});

test('throw in finally bubbles up on error', async t => {
const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});
}
test('throw in finally function bubbles up on success', async t => {
const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});

test('throw in finally bubbles up on error', async t => {
const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});

0 comments on commit 7359bab

Please sign in to comment.