diff --git a/packages/jest-jasmine2/package.json b/packages/jest-jasmine2/package.json index 33e670567007..969536b4192e 100644 --- a/packages/jest-jasmine2/package.json +++ b/packages/jest-jasmine2/package.json @@ -14,7 +14,8 @@ "jest-matcher-utils": "^20.0.3", "jest-matchers": "^20.0.3", "jest-message-util": "^20.0.3", - "jest-snapshot": "^20.0.3" + "jest-snapshot": "^20.0.3", + "p-cancelable": "^0.3.0" }, "devDependencies": { "jest-runtime": "^20.0.4" diff --git a/packages/jest-jasmine2/src/queue_runner.js b/packages/jest-jasmine2/src/queue_runner.js index 978262a85074..d354b8fbd194 100644 --- a/packages/jest-jasmine2/src/queue_runner.js +++ b/packages/jest-jasmine2/src/queue_runner.js @@ -8,6 +8,7 @@ * @flow */ +import PCancelable from 'p-cancelable'; import pTimeout from './p_timeout'; type Options = { @@ -24,20 +25,10 @@ type QueueableFn = { timeout?: () => number, }; -function createCancelToken() { - let cancel; - const promise = new Promise(resolve => { - cancel = resolve; - }); - - return { - cancel, - promise, - }; -} - function queueRunner(options: Options) { - const token = createCancelToken(); + const token = new PCancelable((onCancel, resolve) => { + onCancel(resolve); + }); const mapper = ({fn, timeout}) => { let promise = new Promise(resolve => { @@ -60,7 +51,7 @@ function queueRunner(options: Options) { } }); - promise = Promise.race([promise, token.promise]); + promise = Promise.race([promise, token]); if (!timeout) { return promise; @@ -80,15 +71,15 @@ function queueRunner(options: Options) { ); }; - const returnPromise = options.queueableFns.reduce( + const res = options.queueableFns.reduce( (promise, fn) => promise.then(() => mapper(fn)), Promise.resolve(), ); return { - cancel: token.cancel, - catch: returnPromise.catch.bind(returnPromise), - then: returnPromise.then.bind(returnPromise), + cancel: token.cancel.bind(token), + catch: res.catch.bind(res), + then: res.then.bind(res), }; } diff --git a/yarn.lock b/yarn.lock index e55e9088654a..199e070edfbf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4599,6 +4599,10 @@ osenv@^0.1.4: os-homedir "^1.0.0" os-tmpdir "^1.0.0" +p-cancelable@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-0.3.0.tgz#b9e123800bcebb7ac13a479be195b507b98d30fa" + p-finally@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" @@ -5208,12 +5212,6 @@ resolve@^1.1.3, resolve@^1.1.4, resolve@^1.1.6, resolve@^1.1.7: dependencies: path-parse "^1.0.5" -resolve@^1.3.2: - version "1.3.3" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5" - dependencies: - path-parse "^1.0.5" - resolve@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"