From 96ad1523cb6ab93a1d0061a91a3aa23eb735e769 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 22 May 2020 21:50:31 +0800 Subject: [PATCH] Fix problems with third-party Promise (#427) Co-authored-by: Sindre Sorhus --- lib/promise.js | 3 ++- test/override-promise.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/override-promise.js diff --git a/lib/promise.js b/lib/promise.js index f85c43a3da..bd9d52333d 100644 --- a/lib/promise.js +++ b/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` diff --git a/test/override-promise.js b/test/override-promise.js new file mode 100644 index 0000000000..30c2465c55 --- /dev/null +++ b/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'); +});