From 87e822cb53049c2fb038bfd0926c4542e1e8bdf0 Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 26 Mar 2018 20:13:51 +0100 Subject: [PATCH] [tests] make detox + mocha optional peer deps --- tests-new/bridge/env/node/index.js | 121 +++++++++++++----------- tests-new/bridge/env/node/source-map.js | 39 ++++++-- 2 files changed, 94 insertions(+), 66 deletions(-) diff --git a/tests-new/bridge/env/node/index.js b/tests-new/bridge/env/node/index.js index de6a5a4a..ec278211 100644 --- a/tests-new/bridge/env/node/index.js +++ b/tests-new/bridge/env/node/index.js @@ -1,72 +1,81 @@ -/* eslint-disable no-param-reassign */ +/* eslint-disable no-param-reassign,global-require */ global.bridge = {}; require('./source-map'); - -const detox = require('detox'); const ws = require('./ws'); const ready = require('./ready'); const coverage = require('./coverage'); -/* --------------------- - * DEVICE OVERRIDES - * --------------------- */ +let detox; +try { + detox = require('detox'); +} catch (e) { + // ignore +} -let device; -Object.defineProperty(global, 'device', { - get() { - return device; - }, - set(originalDevice) { - // device.reloadReactNative({ ... }) - // todo detoxOriginalReloadReactNative currently broken it seems - // const detoxOriginalReloadReactNative = originalDevice.reloadReactNative.bind(originalDevice); - originalDevice.reloadReactNative = async () => { - ready.reset(); - global.bridge.reload(); - return ready.wait(); - }; +if (detox) { + /* --------------------- + * DEVICE OVERRIDES + * --------------------- */ - // device.launchApp({ ... }) - const detoxOriginalLaunchApp = originalDevice.launchApp.bind( - originalDevice - ); - originalDevice.launchApp = async (...args) => { - ready.reset(); - await detoxOriginalLaunchApp(...args); - return ready.wait(); - }; + let device; + Object.defineProperty(global, 'device', { + get() { + return device; + }, + set(originalDevice) { + // device.reloadReactNative({ ... }) + // todo detoxOriginalReloadReactNative currently broken it seems + // const detoxOriginalReloadReactNative = originalDevice.reloadReactNative.bind(originalDevice); + originalDevice.reloadReactNative = async () => { + ready.reset(); + global.bridge.reload(); + return ready.wait(); + }; - device = originalDevice; - return originalDevice; - }, -}); + // device.launchApp({ ... }) + const detoxOriginalLaunchApp = originalDevice.launchApp.bind( + originalDevice + ); + originalDevice.launchApp = async (...args) => { + ready.reset(); + await detoxOriginalLaunchApp(...args); + return ready.wait(); + }; -/* ------------------- - * DETOX OVERRIDES - * ------------------- */ + device = originalDevice; + return originalDevice; + }, + }); -// detox.init() -const detoxOriginalInit = detox.init.bind(detox); -detox.init = async (...args) => { - ready.reset(); - await detoxOriginalInit(...args); - return ready.wait(); -}; + /* ------------------- + * DETOX OVERRIDES + * ------------------- */ -// detox.cleanup() -const detoxOriginalCleanup = detox.cleanup.bind(detox); -detox.cleanup = async (...args) => { - try { - ws.close(); - } catch (e) { - // do nothing - } - await detoxOriginalCleanup(...args); -}; + // detox.init() + const detoxOriginalInit = detox.init.bind(detox); + detox.init = async (...args) => { + ready.reset(); + await detoxOriginalInit(...args); + return ready.wait(); + }; + + // detox.cleanup() + const detoxOriginalCleanup = detox.cleanup.bind(detox); + detox.cleanup = async (...args) => { + try { + ws.close(); + } catch (e) { + // do nothing + } + await detoxOriginalCleanup(...args); + }; +} // setup after hook to ensure final context coverage is captured process.nextTick(() => { - after(() => { - coverage.collect(); - }); + if (global.after) { + after(() => { + coverage.collect(); + }); + } }); diff --git a/tests-new/bridge/env/node/source-map.js b/tests-new/bridge/env/node/source-map.js index 20361866..565691c1 100644 --- a/tests-new/bridge/env/node/source-map.js +++ b/tests-new/bridge/env/node/source-map.js @@ -1,12 +1,16 @@ -/* eslint-disable no-param-reassign */ -const Mocha = require('mocha'); -const ErrorStack = require('error-stack-parser'); -const { SourceMapConsumer } = require('source-map'); +/* eslint-disable no-param-reassign,global-require */ +let Mocha; +try { + Mocha = require('mocha'); +} catch (e) { + // ignore +} let bundleFileName = null; -const Runner = Mocha.Runner; let sourceMapConsumer = null; -const originalFail = Runner.prototype.fail; + +const ErrorStack = require('error-stack-parser'); +const { SourceMapConsumer } = require('source-map'); /** * Convert an error frame into a source mapped string @@ -23,8 +27,13 @@ function frameToStr(parsed) { parsed.columnNumber})`; } -// override mocha fail so we can replace stack traces -Runner.prototype.fail = function fail(test, error) { +/** + * Convert an errors stack frames to their original source mapped positions + * + * @param error + * @return {*} + */ +function sourceMappedError(error) { const original = error.stack.split('\n'); const parsed = ErrorStack.parse(error); @@ -37,10 +46,20 @@ Runner.prototype.fail = function fail(test, error) { } error.stack = newStack.join('\n'); - return originalFail.call(this, test, error); -}; + return error; +} + +if (Mocha) { + // override mocha fail so we can replace stack traces + const Runner = Mocha.Runner; + const originalFail = Runner.prototype.fail; + Runner.prototype.fail = function fail(test, error) { + return originalFail.call(this, test, sourceMappedError(error)); + }; +} module.exports = { + sourceMappedError, /** * Build a source map consumer from source map bundle contents * @param str