diff --git a/cavy.js b/cavy.js index da9fc72..5da1a4d 100755 --- a/cavy.js +++ b/cavy.js @@ -34,7 +34,8 @@ function test(cmd) { const outputAsXml = cmd.xml; const dev = cmd.dev; const bootTimeout = cmd.bootTimeout; - runTests(commandName, entryFile, skipbuild, dev, outputAsXml, bootTimeout, args); + const allTestRunTimeout = cmd.allTestRunTimeout; + runTests(commandName, entryFile, skipbuild, dev, outputAsXml, bootTimeout, allTestRunTimeout, args); } // Stop quitting unless we want to @@ -63,6 +64,11 @@ program 'Set how long the CLI should wait for the RN app to boot ' + '(is ignored if used with --skipbuild, defaults to 2 minutes, requires Cavy 4.0.0)' ) + .option( + '-t, --allTestRunTimeout ', + 'Set how long the CLI should wait for the RN app for finishing tests ' + + '(defaults to 2 minutes, requires Cavy 4.0.0)' + ) .option('--xml', 'Write out test results to cavy_results.xml (requires Cavy 3.3.0)') .allowUnknownOption() .action(cmd => test(cmd)); @@ -81,6 +87,12 @@ program 'Set how long the CLI should wait for the RN app to boot ' + '(is ignored if used with --skipbuild, defaults to 2 minutes, requires Cavy 4.0.0)' ) + .option( + '-t, --allTestRunTimeout ', + 'Set how long the CLI should wait for the RN app for finishing tests ' + + '(defaults to 2 minutes, requires Cavy 4.0.0)' + ) + .option('--xml', 'Write out test results to cavy_results.xml (requires Cavy 3.3.0)') .allowUnknownOption() .action(cmd => test(cmd)); diff --git a/server.js b/server.js index 64faf22..f0e5dc2 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,8 @@ const server = http.createServer(); // Setup local variables for server server.locals = { appBooted: false, - testCount: 0 + testCount: 0, + testFinished: false }; // Initialize a WebSocket Server instance @@ -27,9 +28,14 @@ wss.on('connection', socket => { case 'singleResult': logTestResult(json.data); break; - case 'testingComplete': + case 'testingComplete':{ + server.locals.testFinished = true; finishTesting(json.data); break; + } + default: { + console.log(json.event); + } } }); diff --git a/src/runTests.js b/src/runTests.js index 69ef62c..5d4f71b 100644 --- a/src/runTests.js +++ b/src/runTests.js @@ -7,6 +7,7 @@ const { spawn, execFileSync } = require('child_process'); // Default boot timeout in minutes const BOOT_TIMEOUT = 2; +const ALL_TESTS_RUN_TIMEOUT = 2; let switched = false; @@ -57,7 +58,7 @@ function getAdbPath() { } // Start test server, listening for test results to be posted. -function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) { +function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout }) { server.locals.dev = dev; server.locals.outputAsXml = outputAsXml; server.listen(8082, () => { @@ -73,6 +74,7 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) { } else { // bootTimeout defaults to two minutes const timeout = bootTimeout || BOOT_TIMEOUT; + const testsTimeout = allTestRunTimeout || ALL_TESTS_RUN_TIMEOUT setTimeout(() => { if (!server.locals.appBooted) { console.log(`No response from Cavy within ${timeout} minutes.`); @@ -81,6 +83,16 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) { } // Convert bootTimeout to milliseconds }, minsToMillisecs(timeout)); + setTimeout(() => { + if (!server.locals.testFinished) { + console.log(`No response from Cavy within ${allTestRunTimeout} minutes.`); + console.log('Terminating processes.'); + process.exit(1); + } else { + console.log("Finished") + } + // Convert testsTimeout to milliseconds + }, minsToMillisecs(testsTimeout)); } }); } @@ -93,7 +105,7 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) { // outputAsXml: whether to write and save the results to XML file // bootTimeout: how long the CLI should wait for the RN app to boot. // args: any extra arguments the user would usually to pass to `react native run...` -function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args) { +function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, allTestRunTimeout, args) { // Assume entry file is 'index.js' if user doesn't supply one. const entryFile = file || 'index.js'; @@ -134,7 +146,7 @@ function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args) }); if (skipbuild) { - runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }); + runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout}); } else { // Build the app, start the test server and wait for results. console.log(`cavy: Running \`npx react-native ${command}\`...`); @@ -151,7 +163,7 @@ function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args) if (code) { return process.exit(code); } - runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }); + runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout }); }); } }