From 5813fd0bf8350a97d529e5e608e7620b2b65fd0c Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Sun, 16 Apr 2017 16:50:03 -0400 Subject: [PATCH] Fix flaky tests (#3724) * adds continuation to silence rejected promises * Wrap json parsing --- spec/AuthenticationAdapters.spec.js | 8 ++++++-- src/Adapters/Auth/facebook.js | 6 +++++- src/Adapters/Auth/github.js | 6 +++++- src/Adapters/Auth/google.js | 6 +++++- src/Adapters/Auth/janrainengage.js | 9 +++++++-- src/Adapters/Auth/linkedin.js | 6 +++++- src/Adapters/Auth/meetup.js | 6 +++++- src/Adapters/Auth/qq.js | 6 +++++- src/Adapters/Auth/spotify.js | 6 +++++- src/Adapters/Auth/vkontakte.js | 6 +++++- src/Adapters/Auth/wechat.js | 6 +++++- src/Adapters/Auth/weibo.js | 6 +++++- 12 files changed, 63 insertions(+), 14 deletions(-) diff --git a/spec/AuthenticationAdapters.spec.js b/spec/AuthenticationAdapters.spec.js index 501b64b3..526fa5c8 100644 --- a/spec/AuthenticationAdapters.spec.js +++ b/spec/AuthenticationAdapters.spec.js @@ -10,8 +10,12 @@ describe('AuthenticationProviers', function() { var provider = require("../src/Adapters/Auth/" + providerName); jequal(typeof provider.validateAuthData, "function"); jequal(typeof provider.validateAppId, "function"); - jequal(provider.validateAuthData({}, {}).constructor, Promise.prototype.constructor); - jequal(provider.validateAppId("app", "key", {}).constructor, Promise.prototype.constructor); + const authDataPromise = provider.validateAuthData({}, {}); + const validateAppIdPromise = provider.validateAppId("app", "key", {}); + jequal(authDataPromise.constructor, Promise.prototype.constructor); + jequal(validateAppIdPromise.constructor, Promise.prototype.constructor); + authDataPromise.then(()=>{}, ()=>{}); + validateAppIdPromise.then(()=>{}, ()=>{}); done(); }); }); diff --git a/src/Adapters/Auth/facebook.js b/src/Adapters/Auth/facebook.js index 84f89566..ab846e43 100644 --- a/src/Adapters/Auth/facebook.js +++ b/src/Adapters/Auth/facebook.js @@ -43,7 +43,11 @@ function graphRequest(path) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/github.js b/src/Adapters/Auth/github.js index e6e2c05e..146fbdc6 100644 --- a/src/Adapters/Auth/github.js +++ b/src/Adapters/Auth/github.js @@ -36,7 +36,11 @@ function request(path, access_token) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/google.js b/src/Adapters/Auth/google.js index 4699c837..7cc41492 100644 --- a/src/Adapters/Auth/google.js +++ b/src/Adapters/Auth/google.js @@ -55,7 +55,11 @@ function request(path) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/janrainengage.js b/src/Adapters/Auth/janrainengage.js index 4df5701a..7de682e7 100644 --- a/src/Adapters/Auth/janrainengage.js +++ b/src/Adapters/Auth/janrainengage.js @@ -41,7 +41,7 @@ function request(api_key, auth_token) { } }; - return new Promise(function (resolve) { + return new Promise(function (resolve, reject) { // Create the post request. var post_req = https.request(post_options, function (res) { var data = ''; @@ -52,7 +52,12 @@ function request(api_key, auth_token) { }); // Once we have all the data, we can parse it and return the data we want. res.on('end', function () { - resolve(JSON.parse(data)); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } + resolve(data); }); }); diff --git a/src/Adapters/Auth/linkedin.js b/src/Adapters/Auth/linkedin.js index 63c6cd97..de5fc66c 100644 --- a/src/Adapters/Auth/linkedin.js +++ b/src/Adapters/Auth/linkedin.js @@ -42,7 +42,11 @@ function request(path, access_token, is_mobile_sdk) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/meetup.js b/src/Adapters/Auth/meetup.js index e5c7e2c9..bb14dc54 100644 --- a/src/Adapters/Auth/meetup.js +++ b/src/Adapters/Auth/meetup.js @@ -35,7 +35,11 @@ function request(path, access_token) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/qq.js b/src/Adapters/Auth/qq.js index 376a5e65..6f4dfdc0 100644 --- a/src/Adapters/Auth/qq.js +++ b/src/Adapters/Auth/qq.js @@ -32,7 +32,11 @@ function graphRequest(path) { throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.'); } data = data.substring(starPos + 1,endPos - 1); - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function () { diff --git a/src/Adapters/Auth/spotify.js b/src/Adapters/Auth/spotify.js index 7c39ed65..701422c5 100644 --- a/src/Adapters/Auth/spotify.js +++ b/src/Adapters/Auth/spotify.js @@ -49,7 +49,11 @@ function request(path, access_token) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function() { diff --git a/src/Adapters/Auth/vkontakte.js b/src/Adapters/Auth/vkontakte.js index 0fdf28f2..4e57b259 100644 --- a/src/Adapters/Auth/vkontakte.js +++ b/src/Adapters/Auth/vkontakte.js @@ -47,7 +47,11 @@ function request(host, path) { data += chunk; }); res.on('end', function () { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function () { diff --git a/src/Adapters/Auth/wechat.js b/src/Adapters/Auth/wechat.js index 77cd7cfa..9432b7c5 100644 --- a/src/Adapters/Auth/wechat.js +++ b/src/Adapters/Auth/wechat.js @@ -26,7 +26,11 @@ function graphRequest(path) { data += chunk; }); res.on('end', function () { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); }).on('error', function () { diff --git a/src/Adapters/Auth/weibo.js b/src/Adapters/Auth/weibo.js index 8daf11de..64efada2 100644 --- a/src/Adapters/Auth/weibo.js +++ b/src/Adapters/Auth/weibo.js @@ -39,7 +39,11 @@ function graphRequest(access_token) { data += chunk; }); res.on('end', function () { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + return reject(e); + } resolve(data); }); res.on('error', function () {