Skip to content

Commit

Permalink
Merge pull request #639 from wix/CliAutoPlatfromTestFiltering
Browse files Browse the repository at this point in the history
Automatically filter platform tests by inferring from config
  • Loading branch information
rotemmiz authored Mar 26, 2018
2 parents 792a782 + 14e4988 commit 58e145c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
41 changes: 24 additions & 17 deletions detox/local-cli/detox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const program = require('commander');
const path = require('path');
const cp = require('child_process');

program
.option('-o, --runner-config [config]',
`Test runner config file, defaults to e2e/mocha.opts for mocha and e2e/config.json' for jest`)
Expand All @@ -22,7 +23,7 @@ program
.option('-a, --artifacts-location [path]',
'Artifacts destination path (currently will contain only logs). If the destination already exists, it will be removed first')
.option('-p, --platform [ios/android]',
'Run platform specific tests. Runs tests with invert grep on \':platform:\', '
'[DEPRECATED], platform is deduced automatically. Run platform specific tests. Runs tests with invert grep on \':platform:\', '
+ 'e.g test with substring \':ios:\' in its name will not run when passing \'--platform android\'')
.option('-f, --file [path]',
'Specify test file to run')
Expand All @@ -33,11 +34,28 @@ const config = require(path.join(process.cwd(), 'package.json')).detox;
const testFolder = getConfigFor(['file', 'specs'], 'e2e');
const runner = getConfigFor(['testRunner'], 'mocha');
const runnerConfig = getConfigFor(['runnerConfig'], getDefaultRunnerConfig());
const platform = (config.configurations[program.configuration].type).split('.')[0];

run();


if (typeof program.debugSynchronization === "boolean") {
program.debugSynchronization = 3000;
}

function run() {
switch (runner) {
case 'mocha':
runMocha();
break;
case 'jest':
runJest();
break;
default:
throw new Error(`${runner} is not supported in detox cli tools. You can still run your tests with the runner's own cli tool`);
}
}

function getConfigFor(keys, fallback) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand All @@ -53,37 +71,26 @@ function camelToKebabCase(string) {
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}

switch (runner) {
case 'mocha':
runMocha();
break;
case 'jest':
runJest();
break;
default:
throw new Error(`${runner} is not supported in detox cli tools. You can still run your tests with the runner's own cli tool`);
}

function runMocha() {
const loglevel = program.loglevel ? `--loglevel ${program.loglevel}` : '';
const configuration = program.configuration ? `--configuration ${program.configuration}` : '';
const cleanup = program.cleanup ? `--cleanup` : '';
const reuse = program.reuse ? `--reuse` : '';
const artifactsLocation = program.artifactsLocation ? `--artifacts-location ${program.artifactsLocation}` : '';
const configFile = runnerConfig ? `--opts ${runnerConfig}` : '';
const platform = program.platform ? `--grep ${getPlatformSpecificString(program.platform)} --invert` : '';
const platformString = platform ? `--grep ${getPlatformSpecificString(platform)} --invert` : '';

const debugSynchronization = program.debugSynchronization ? `--debug-synchronization ${program.debugSynchronization}` : '';
const command = `node_modules/.bin/mocha ${testFolder} ${configFile} ${configuration} ${loglevel} ${cleanup} ${reuse} ${debugSynchronization} ${platform} ${artifactsLocation}`;
const command = `node_modules/.bin/mocha ${testFolder} ${configFile} ${configuration} ${loglevel} ${cleanup} ${reuse} ${debugSynchronization} ${platformString} ${artifactsLocation}`;

console.log(command);
cp.execSync(command, {stdio: 'inherit'});
}

function runJest() {
const configFile = runnerConfig ? `--config=${runnerConfig}` : '';
const platform = program.platform ? `--testNamePattern='^((?!${getPlatformSpecificString(program.platform)}).)*$'` : '';
const command = `node_modules/.bin/jest ${testFolder} ${configFile} --runInBand ${platform}`;
const platformString = platform ? `--testNamePattern='^((?!${getPlatformSpecificString(platform)}).)*$'` : '';
const command = `node_modules/.bin/jest ${testFolder} ${configFile} --runInBand ${platformString}`;
console.log(command);
cp.execSync(command, {
stdio: 'inherit',
Expand Down Expand Up @@ -123,4 +130,4 @@ function getPlatformSpecificString(platform) {
}

return platformRevertString;
}
}
2 changes: 1 addition & 1 deletion detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"child-process-promise": "^2.2.0",
"commander": "^2.9.0",
"commander": "^2.15.1",
"detox-server": "^7.0.0",
"fs-extra": "^4.0.2",
"get-port": "^2.1.0",
Expand Down
4 changes: 2 additions & 2 deletions detox/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test": ":",
"packager": "react-native start",
"detox-server": "detox run-server",
"e2e:ios": "detox test --configuration ios.sim.release --debug-synchronization 10000 --platform ios",
"e2e:android": "detox test --configuration android.emu.release --loglevel verbose --platform android",
"e2e:ios": "detox test --configuration ios.sim.release --debug-synchronization 10000",
"e2e:android": "detox test --configuration android.emu.release --loglevel verbose",
"build:ios": "detox build --configuration ios.sim.release",
"build:android": "detox build --configuration android.emu.release"
},
Expand Down

0 comments on commit 58e145c

Please sign in to comment.