Skip to content

Commit

Permalink
Fix problems with third-party Promise (#427)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed May 22, 2020
1 parent e222526 commit 96ad152
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/promise.js
@@ -1,8 +1,9 @@
'use strict';

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

// The return value is a mixin of `childProcess` and `Promise`
Expand Down
20 changes: 20 additions & 0 deletions test/override-promise.js
@@ -0,0 +1,20 @@
import path from 'path';
import test from 'ava';

process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;

// Can't use `test.before`, because `ava` needs `Promise`.
// Can't use `import(…)` either, because `execa` is not an ES Module.
const nativePromise = Promise;
global.Promise = class BrokenPromise {
then() {
throw new Error('error');
}
};
const execa = require('..');
global.Promise = nativePromise;

test('should work with third-party Promise', async t => {
const {stdout} = await execa('noop', ['foo']);
t.is(stdout, 'foo');
});

0 comments on commit 96ad152

Please sign in to comment.