[tests] make detox + mocha optional peer deps

This commit is contained in:
Salakar 2018-03-26 20:13:51 +01:00
parent e19e87b0aa
commit 87e822cb53
2 changed files with 94 additions and 66 deletions

View File

@ -1,18 +1,24 @@
/* 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');
/* ---------------------
let detox;
try {
detox = require('detox');
} catch (e) {
// ignore
}
if (detox) {
/* ---------------------
* DEVICE OVERRIDES
* --------------------- */
let device;
Object.defineProperty(global, 'device', {
let device;
Object.defineProperty(global, 'device', {
get() {
return device;
},
@ -39,34 +45,37 @@ Object.defineProperty(global, 'device', {
device = originalDevice;
return originalDevice;
},
});
});
/* -------------------
/* -------------------
* DETOX OVERRIDES
* ------------------- */
// detox.init()
const detoxOriginalInit = detox.init.bind(detox);
detox.init = async (...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) => {
// 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(() => {
if (global.after) {
after(() => {
coverage.collect();
});
}
});

View File

@ -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