Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function runApp(platform, junitFilePath) {
const mochaConfig = {};
// Check if an argument for junit path was requested
if (junitFilePath) {
mochaConfig.reporter = "mocha-junit-reporter";
mochaConfig.reporterOptions = {
mochaFile: junitFilePath,
};
}
const server = new MochaRemoteServer(mochaConfig, {
// Accept connections only from the expected platform, to prevent cross-talk when both emulators are open
id: platform,
});
await server.start();
// Spawn a react-native metro server
const metro = rn.async("start", /*"--verbose", "--reset-cache"*/);
// Kill metro when the process is killed
process.on("exit", (code) => {
metro.kill("SIGHUP");
});
// Close the runner if metro closes unexpectedly
metro.on("close", (code) => {
if (code !== 0) {
console.error(`Metro server unexpectedly closed (code = ${code})`);
process.exit(code);
// Close the runner if metro closes unexpectedly
metro.on("close", (code) => {
if (code !== 0) {
console.error(`Metro server unexpectedly closed (code = ${code})`);
process.exit(code);
}
});
if (platform === "android") {
const devices = android.adb.devices();
const activeDevices = devices.filter(({ state }) => state === "device");
if (activeDevices.length === 0) {
throw new Error("Missing an active device: Attach a device via USB or start an emulator");
} else {
// Ensure the device can access the mocha remote server
android.adb.reverseServerPort(MochaRemoteServer.DEFAULT_CONFIG.port);
}
// Ask React Native to run the android app
rn.sync("run-android", "--no-packager");
} else if (platform === "ios") {
const deviceName = "realm-js-integration-tests";
ensureSimulator(deviceName, 'com.apple.CoreSimulator.SimDeviceType.iPhone-11');
console.log("Simulator is ready 🚀");
// Ask React Native to run the ios app
rn.sync("run-ios", "--no-packager", "--simulator", deviceName);
} else {
throw new Error(`Unexpected platform: '${platform}'`);
}
// Run tests with a 5 minute timeout
return timeout(new Promise((resolve) => {
console.log("Running tests 🏃♂️");
const processType = process.argv[2];
if (processType !== "main" && processType !== "renderer") {
throw Error("You need to call this with a runtime argument specifying the process type");
}
// Check if an argument for junit path was requested
const junitFilePath = process.argv[3];
if (junitFilePath) {
mochaConfig.reporter = "mocha-junit-reporter";
mochaConfig.reporterOptions = {
mochaFile: junitFilePath,
};
}
// Start the mocha remote server
const server = new MochaRemoteServer(mochaConfig, {
id: processType,
// Listing on all interfaces
host: "0.0.0.0",
port: 0,
});
await server.start();
// Spawn the electron process
const appProcess = runElectron(processType, server.getUrl());
console.log(`Started the Electron app (pid = ${appProcess.pid})`);
try {
await new Promise((resolve, reject) => {
// Succeed after the tests has run
server.run(resolve);
// Fail if the electron app closes (before the mocha tests completes)