From e95ae43c8f0e40742540844984861a06f26999c6 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 30 Mar 2017 14:43:56 -0700 Subject: tests - unit - fail on unhandled promise rejection --- package.json | 1 + test/helper.js | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9e04bc6f7..70da129d2 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "async": "^1.5.2", "async-q": "^0.3.1", "bip39": "^2.2.0", + "bluebird": "^3.5.0", "browser-passworder": "^2.0.3", "browserify-derequire": "^0.9.4", "clone": "^1.0.2", diff --git a/test/helper.js b/test/helper.js index a01ea1e53..dd22d80b4 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,11 +1,52 @@ +// disallow promises from swallowing errors +enableFailureOnUnhandledPromiseRejection() + +// logging util var log = require('loglevel') log.setDefaultLevel(5) +global.log = log +// +// polyfills +// + +// dom require('jsdom-global')() + +// localStorage window.localStorage = {} -if (!('crypto' in window)) { window.crypto = {} } -window.crypto.getRandomValues = require('polyfill-crypto.getrandomvalues') +// crypto.getRandomValues +if (!window.crypto) window.crypto = {} +if (!window.crypto.getRandomValues) window.crypto.getRandomValues = require('polyfill-crypto.getrandomvalues') -window.log = log -global.log = log + + +function enableFailureOnUnhandledPromiseRejection() { + // overwrite node's promise with the stricter Bluebird promise + global.Promise = require('bluebird') + + // rethrow unhandledRejections + if (typeof process !== 'undefined') { + process.on('unhandledRejection', function (reason) { + throw reason; + }); + } else if (typeof window !== 'undefined') { + // 2016-02-01: No browsers support this natively, however bluebird, when.js, + // and probably other libraries do. + if (typeof window.addEventListener === 'function') { + window.addEventListener('unhandledrejection', function (evt) { + throw evt.detail.reason; + }); + } else { + var oldOHR = window.onunhandledrejection; + window.onunhandledrejection = function (evt) { + if (typeof oldOHR === 'function') oldOHR.apply(this, arguments); + throw evt.detail.reason; + }; + } + } else if (typeof console !== 'undefined' && + typeof (console.error || console.log) === 'function') { + (console.error || console.log)('Unhandled rejections will be ignored!'); + } +} \ No newline at end of file -- cgit v1.2.3 From 8ae37ae80de38728c8c878ab24249450a570e97a Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 30 Mar 2017 14:49:39 -0700 Subject: tests - helper - add note --- test/helper.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/helper.js b/test/helper.js index dd22d80b4..aaac7580e 100644 --- a/test/helper.js +++ b/test/helper.js @@ -26,27 +26,29 @@ function enableFailureOnUnhandledPromiseRejection() { // overwrite node's promise with the stricter Bluebird promise global.Promise = require('bluebird') + // modified from https://github.com/mochajs/mocha/issues/1926#issuecomment-180842722 + // rethrow unhandledRejections if (typeof process !== 'undefined') { process.on('unhandledRejection', function (reason) { - throw reason; - }); + throw reason + }) } else if (typeof window !== 'undefined') { // 2016-02-01: No browsers support this natively, however bluebird, when.js, // and probably other libraries do. if (typeof window.addEventListener === 'function') { window.addEventListener('unhandledrejection', function (evt) { - throw evt.detail.reason; - }); + throw evt.detail.reason + }) } else { - var oldOHR = window.onunhandledrejection; + var oldOHR = window.onunhandledrejection window.onunhandledrejection = function (evt) { - if (typeof oldOHR === 'function') oldOHR.apply(this, arguments); - throw evt.detail.reason; - }; + if (typeof oldOHR === 'function') oldOHR.apply(this, arguments) + throw evt.detail.reason + } } } else if (typeof console !== 'undefined' && typeof (console.error || console.log) === 'function') { - (console.error || console.log)('Unhandled rejections will be ignored!'); + (console.error || console.log)('Unhandled rejections will be ignored!') } } \ No newline at end of file -- cgit v1.2.3 From 1079c20c8329610f124713a1b1dae2dfe9c99ba0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 30 Mar 2017 14:50:01 -0700 Subject: meta - package.json - rename npm test scripts --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 70da129d2..7dfe21f24 100644 --- a/package.json +++ b/package.json @@ -5,20 +5,20 @@ "private": true, "scripts": { "start": "npm run dev", - "lint": "gulp lint", - "buildCiUnits": "node test/integration/index.js", "dev": "gulp dev --debug", "disc": "gulp disc --debug", "dist": "gulp dist --disableLiveReload", - "test": "npm run lint && npm run fastTest && npm run ci", - "fastTest": "METAMASK_ENV=test mocha --require test/helper.js --recursive \"test/unit/**/*.js\"", + "test": "npm run lint && npm run test-unit && npm run test-integration", + "test-unit": "METAMASK_ENV=test mocha --require test/helper.js --recursive \"test/unit/**/*.js\"", + "test-integration": "npm run buildMock && npm run buildCiUnits && testem ci -P 2", + "lint": "gulp lint", + "buildCiUnits": "node test/integration/index.js", "watch": "mocha watch --recursive \"test/unit/**/*.js\"", "genStates": "node development/genStates.js", "ui": "npm run genStates && beefy ui-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "mock": "beefy mock-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "buildMock": "npm run genStates && browserify ./mock-dev.js -o ./development/bundle.js", "testem": "npm run buildMock && testem", - "ci": "npm run buildMock && npm run buildCiUnits && testem ci -P 2", "announce": "node development/announcer.js", "generateNotice": "node notices/notice-generator.js", "deleteNotice": "node notices/notice-delete.js" -- cgit v1.2.3 From 47ea5452414ef4c126ff6c6ccb40615f852f8bed Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 30 Mar 2017 15:43:01 -0700 Subject: tests - add missing done --- test/unit/keyring-controller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js index aae4cdfd6..efd0a3546 100644 --- a/test/unit/keyring-controller-test.js +++ b/test/unit/keyring-controller-test.js @@ -135,7 +135,7 @@ describe('KeyringController', function() { }) describe('#getAccounts', function() { - it('returns the result of getAccounts for each keyring', function() { + it('returns the result of getAccounts for each keyring', function(done) { keyringController.keyrings = [ { getAccounts() { return Promise.resolve([1,2,3]) } }, { getAccounts() { return Promise.resolve([4,5,6]) } }, -- cgit v1.2.3