react-native-firebase/tests-new/bridge/env/node/index.js

82 lines
1.9 KiB
JavaScript

/* eslint-disable no-param-reassign,global-require */
global.bridge = {};
require('./source-map');
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', {
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.launchApp({ ... })
const detoxOriginalLaunchApp = originalDevice.launchApp.bind(
originalDevice
);
originalDevice.launchApp = async (...args) => {
ready.reset();
await detoxOriginalLaunchApp(...args);
return ready.wait();
};
device = originalDevice;
return originalDevice;
},
});
/* -------------------
* DETOX OVERRIDES
* ------------------- */
// 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(() => {
if (global.after) {
after(() => {
coverage.collect();
});
}
});