From a89e5420a9c0523e4aebd68577a14915b38b99a7 Mon Sep 17 00:00:00 2001 From: Eugene Tiutiunnyk Date: Mon, 24 Jul 2017 11:29:03 -0700 Subject: [PATCH] Add an option of exit with fail on tests fail --- README.md | 1 + bin/cli.js | 10 ++++++---- bin/runner.js | 10 +++++++--- lib/server.js | 3 ++- lib/utils.js | 10 ++++++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4ed5675..0a7e859 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ To run browser tests on BrowserStack infrastructure, you need to create a `brows * `browsers`: A list of browsers on which tests are to be run. Find a [list of all supported browsers and platforms on browerstack.com](https://www.browserstack.com/list-of-browsers-and-platforms?product=js_testing). * `build`: A string to identify your test run in Browserstack. In `TRAVIS` setup `TRAVIS_COMMIT` will be the default identifier. * `proxy`: Specify a proxy to use for the local tunnel. Object with `host`, `port`, `username` and `password` properties. + * `exit_with_fail`: If set to true the cli process will exit with fail if any of the tests failed. Useful for automatic build systems. A sample configuration file: diff --git a/bin/cli.js b/bin/cli.js index 1522a7c..3e8ff45 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -306,10 +306,11 @@ var statusPoller = { } logger.trace('[%s] worker.activityTimeout: all tests done', worker.id, config.status && 'with failures'); + var testsFailedError = utils.createTestsFailedError(config); if(server && server.reports) { - callback(null, server.reports); + callback(testsFailedError, server.reports); } else { - callback(null, {}); + callback(testsFailedError, {}); } } } else { @@ -336,10 +337,11 @@ var statusPoller = { } logger.trace('[%s] worker.testActivityTimeout: all tests done', worker.id, config.status && 'with failures'); + var testsFailedError = utils.createTestsFailedError(config); if(server && server.reports) { - callback(null, server.reports); + callback(testsFailedError, server.reports); } else { - callback(null, {}); + callback(testsFailedError, {}); } } } else { diff --git a/bin/runner.js b/bin/runner.js index aef3b9e..484e2d7 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -39,9 +39,13 @@ try { var runner = require('./cli.js'); runner.run(config, function(err) { if(err) { - console.error(err); - console.error(err.stack); - console.error('Invalid Command'); + if (err.name === 'TestsFailedError') { + console.error('Exit with fail due to some tests failure.'); + } else { + console.error(err); + console.error(err.stack); + console.error('Invalid Command'); + } process.exit(1); } process.exit(0); diff --git a/lib/server.js b/lib/server.js index 5ee0af7..8b9300c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -340,7 +340,8 @@ exports.Server = function Server(bsClient, workers, config, callback) { } logger.trace('[%s] _report: checkAndTerminateWorker: all tests done', worker.id, config.status && 'with failures'); - callback(null, reports); + var testsFailedError = utils.createTestsFailedError(config); + callback(testsFailedError, reports); } }); }); diff --git a/lib/utils.js b/lib/utils.js index dab1f82..e6b7f18 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -43,7 +43,17 @@ var objectSize = function objectSize(obj) { return size; }; +var createTestsFailedError = function createTestsFailedError(config) { + var error = null; + if (config.status && config.exit_with_fail) { + error = new Error('Some tests failed.'); + error.name = 'TestsFailedError'; + } + return error; +}; + exports.titleCase = titleCase; exports.uuid = uuid; exports.browserString = browserString; exports.objectSize = objectSize; +exports.createTestsFailedError = createTestsFailedError;