diff --git a/detox/README.md b/detox/README.md index 28152e40aa..1705af73cc 100644 --- a/detox/README.md +++ b/detox/README.md @@ -208,6 +208,12 @@ You can choose to build your project in any of these ways... detox test --configuration yourConfiguration ``` +### Faster test runs with app reuse +By default the app is rmoved, reinstalled and launched before each run. +Starting fresh is critical in CI but in dev you might be able to save time between test runs and reuse the app that was previously installed in the simulator. To do so use the `reuse` flag and run your tests like this:`detox test --reuse`. +This is especially useful with React Native DEV mode when making Javascript code changes that are getting picked up by the packager (and thus no reinstall is needed). This can save up to 7 seconds per run! +You should not use this option if you made native code changes or if your app relies on local ("disk") storage. + ## See it in Action diff --git a/detox/local-cli/detox-test.js b/detox/local-cli/detox-test.js index bc00f6d1ff..e02d9b28b2 100644 --- a/detox/local-cli/detox-test.js +++ b/detox/local-cli/detox-test.js @@ -9,6 +9,7 @@ program .option('-l, --loglevel [value]', 'info, debug, verbose, silly, wss') .option('-c, --configuration [device configuration]', 'Select a device configuration from your defined configurations,' + 'if not supplied, and there\'s only one configuration, detox will default to it') + .option('-r, --reuse', 'Reuse existing installed app (do not delete and re-install) for a faster run.', false) .option('-u, --cleanup', 'shutdown simulator when test is over, useful for CI scripts, to make sure detox exists cleanly with no residue', false) .parse(process.argv); @@ -18,11 +19,12 @@ const testFolder = config.specs || 'e2e'; const loglevel = program.loglevel ? `--loglevel ${program.loglevel}` : ''; const configuration = program.configuration ? `--configuration ${program.configuration}` : ''; const cleanup = program.cleanup ? `--cleanup` : ''; +const reuse = program.reuse ? `--reuse` : ''; let command; switch (program.runner) { case 'mocha': - command = `node_modules/.bin/${program.runner} ${testFolder} --opts ${testFolder}/${program.runnerConfig} ${configuration} ${loglevel} ${cleanup}`; + command = `node_modules/.bin/${program.runner} ${testFolder} --opts ${testFolder}/${program.runnerConfig} ${configuration} ${loglevel} ${cleanup} ${reuse}`; break; default: throw new Error(`${program.runner} is not supported in detox cli tools. You can still run your tests with the runner's own cli tool`); diff --git a/detox/src/devices/Simulator.js b/detox/src/devices/Simulator.js index 35af70da59..4d1b2f7aef 100644 --- a/detox/src/devices/Simulator.js +++ b/detox/src/devices/Simulator.js @@ -6,6 +6,7 @@ const _ = require('lodash'); const IosNoneDevice = require('./IosNoneDevice'); const FBsimctl = require('./Fbsimctl'); const configuration = require('../configuration'); +const argparse = require('../utils/argparse'); class Simulator extends IosNoneDevice { @@ -51,8 +52,8 @@ class Simulator extends IosNoneDevice { this._simulatorUdid = await this._fbsimctl.list(this._deviceConfig.name); this._bundleId = await this._getBundleIdFromApp(this._deviceConfig.binaryPath); await this._fbsimctl.boot(this._simulatorUdid); - await this.relaunchApp({delete: true}); - } + await this.relaunchApp({delete: !argparse.getArgValue('reuse')}); +} async relaunchApp(params = {}, bundleId) { if (params.url && params.userNotification) { diff --git a/detox/src/devices/Simulator.test.js b/detox/src/devices/Simulator.test.js index 1656f3dbab..aa8d4a9f2a 100644 --- a/detox/src/devices/Simulator.test.js +++ b/detox/src/devices/Simulator.test.js @@ -9,6 +9,7 @@ describe('Simulator', () => { let cpp; let Simulator; let simulator; + let argparse; let Client; let client; @@ -23,6 +24,9 @@ describe('Simulator', () => { jest.mock('./Fbsimctl'); jest.mock('../client/Client'); + jest.mock('../utils/argparse'); + argparse = require('../utils/argparse'); + Client = require('../client/Client'); Simulator = require('./Simulator'); @@ -98,6 +102,21 @@ describe('Simulator', () => { ["-detoxServer", "ws://localhost:8099", "-detoxSessionId", "test"]); }); + + it(`relaunchApp() without delete when reuse is enabled should not uninstall and install`, async() => { + simulator = validSimulator(); + argparse.getArgValue.mockReturnValue(true); + fs.existsSync.mockReturnValue(true); + + await simulator.relaunchApp(); + + expect(simulator._fbsimctl.uninstall).not.toHaveBeenCalled(); + expect(simulator._fbsimctl.install).not.toHaveBeenCalled(); + expect(simulator._fbsimctl.launch).toHaveBeenCalledWith(simulator._simulatorUdid, + simulator._bundleId, + ["-detoxServer", "ws://localhost:8099", "-detoxSessionId", "test"]); + }); + it(`relaunchApp() with url should send the url as a param in launchParams`, async() => { simulator = validSimulator(); await simulator.relaunchApp({url: `scheme://some.url`});