diff --git a/src/bin/run.js b/src/bin/run.js index 88d7af4..faf7419 100644 --- a/src/bin/run.js +++ b/src/bin/run.js @@ -16,6 +16,7 @@ import { clearDirectories, fetchRemoteComparisonImages } from '../comparisonActions'; +import filterScenario from '../scenarioFilter'; import validateConfig from '../configValidator'; import Reporter from '../reporter'; @@ -38,6 +39,7 @@ program 'Select the browser to run your tests on. E.G. chrome, firefox, etc.' ) .option('c, --config [config]', 'Path to your config') + .option('f, --run [optional]', 'Filter scenarios based on label name') .option('r, --remote', 'Upload new baseline to remote storage') .action(async options => { try { @@ -45,6 +47,11 @@ program if (options.browser) config.browser = options.browser; + if (options.run) + filterScenario(config, options.run).then( + scenario => (config.scenarios = scenario) + ); + validateConfig(config, options.remote); logger.info('run', 'Getting snapshots... 📸 '); @@ -64,6 +71,7 @@ program 'Select the browser to run your tests on. E.G. chrome, firefox, etc.' ) .option('c, --config [config]', 'Path to your config') + .option('f, --run [optional]', 'Filter scenarios based on label name') .option('r, --remote', 'Upload new baseline to remote storage') .action(async options => { try { @@ -71,6 +79,11 @@ program if (options.browser) config.browser = options.browser; + if (options.run) + filterScenario(config, options.run).then( + scenario => (config.scenarios = scenario) + ); + validateConfig(config, options.remote); createDirectories(fs, config); @@ -90,12 +103,19 @@ program 'Select the browser to run your tests on. E.G. chrome, firefox, etc.' ) .option('c, --config [config]', 'Path to your config') + .option('f, --run [optional]', 'Filter scenarios based on label name') .option('r, --remote', 'Upload new baseline to remote storage') .action(async options => { try { const config = require(path.resolve(options.config)); // eslint-disable-line import/no-dynamic-require if (options.browser) config.browser = options.browser; + + if (options.run) + filterScenario(config, options.run).then( + scenario => (config.scenarios = scenario) + ); + config.remote = options.remote; validateConfig(config, config.remote); diff --git a/src/scenarioFilter.js b/src/scenarioFilter.js new file mode 100644 index 0000000..657a19c --- /dev/null +++ b/src/scenarioFilter.js @@ -0,0 +1,21 @@ +import logger from './logger'; + +const filterScenario = (config, filter) => { + return new Promise(resolve => { + const filteredScenario = Object.values(config.scenarios).filter( + scenario => scenario.label === filter + ); + + if (filteredScenario.length === 0) { + logger.info( + 'filterConfig', + `❗️ ${filter} not found on your scenarios. Exiting Aye Spy` + ); + process.exitCode = 1; + process.exit(); + } + resolve(filteredScenario); + }); +}; + +export default filterScenario; diff --git a/src/scenarioFilter.test.js b/src/scenarioFilter.test.js new file mode 100644 index 0000000..47d7687 --- /dev/null +++ b/src/scenarioFilter.test.js @@ -0,0 +1,44 @@ +/* globals jest expect */ + +import filterScenario from './scenarioFilter'; + +describe('Scenario filter', () => { + it('Filters scenario correctly', () => { + const config = { + gridUrl: 'http://selenium.com:4444/wd/hub', + scenarios: [ + { + url: 'http:/google.com/', + label: 'homepage' + }, + { + url: 'http://test.com', + label: 'test' + } + ] + }; + + filterScenario(config, 'test').then(scenario => { + expect(scenario.length).toBe(1); + }); + }); + + it('Throws an error if scenario is not found', () => { + const config = { + gridUrl: 'http://selenium.com:4444/wd/hub', + scenarios: [ + { + url: 'http:/google.com/', + label: 'homepage' + }, + { + url: 'http://test.com', + label: 'test' + } + ] + }; + process.exit = jest.fn(); + filterScenario(config, 'lol'); + expect(process.exit.mock.calls.length).toBe(1); + }); +});