diff --git a/.env-template b/.env-template index a836e649..8c4fe11c 100644 --- a/.env-template +++ b/.env-template @@ -1,2 +1,3 @@ SAUCE_USERNAME=ascribe SAUCE_ACCESS_KEY= +SAUCE_DEFAULT_URL= diff --git a/package.json b/package.json index deb70cca..cd1eae93 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "preinstall": "export SAUCE_CONNECT_DOWNLOAD_ON_INSTALL=true", "postinstall": "npm run build", "build": "gulp build --production", - "start": "node server.js" + "start": "node server.js", + "test": "mocha", + "tunnel": "node test/tunnel.js" }, "browser": { "fineUploader": "./js/components/ascribe_uploader/vendor/s3.fine-uploader.js" @@ -38,6 +40,7 @@ "babel-jest": "^5.2.0", "chai": "^3.4.1", "chai-as-promised": "^5.1.0", + "colors": "^1.1.2", "dotenv": "^1.2.0", "gulp-sass": "^2.1.1", "jest-cli": "^0.4.0", diff --git a/test/README.md b/test/README.md index 10b5784c..49d7718c 100644 --- a/test/README.md +++ b/test/README.md @@ -1,3 +1,13 @@ +# TL;DR +Copy the file `.env-template` in `.env` and fill up the missing keys. + +```bash +$ npm install +$ npm run tunnel +$ npm test && git commit +``` + + # Welcome to our test suite, let me be your guide Dear reader, first of all thanks for taking your time reading this document. @@ -199,3 +209,10 @@ $ mocha By default the test suite runs on `http://www.localhost.com:3000/`, if you want to change the URL, change the `APP_URL` env variable. + + +# How to have fun +Try this! +```bash +$ mocha -R nyan +``` diff --git a/test/config.js b/test/config.js index a1ac78db..aad28856 100644 --- a/test/config.js +++ b/test/config.js @@ -1,5 +1,7 @@ 'use strict'; +require('dotenv').load(); + // https://code.google.com/p/selenium/wiki/DesiredCapabilities const BROWSERS = [ @@ -9,8 +11,9 @@ const BROWSERS = [ 'internet explorer,10,VISTA' ]; -const APP_URL = process.env.APP_URL || 'http://www.localhost.com:3000'; - -module.exports.BROWSERS = BROWSERS.map(x => x.split(',')); -module.exports.APP_URL = APP_URL; +module.exports = { + BROWSERS: BROWSERS.map(x => x.split(',')), + APP_URL: process.env.SAUCE_DEFAULT_URL || 'http://www.localhost.com:3000', + TUNNEL_AUTO_CONNECT: process.env.SAUCE_AUTO_CONNECT +}; diff --git a/test/setup.js b/test/setup.js index 0313a4ae..0202ef29 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,14 +1,27 @@ 'use strict'; -require('dotenv').load(); - +const config = require('./config'); +const colors = require('colors'); const sauceConnectLauncher = require('sauce-connect-launcher'); + + let globalSauceProcess; +if (!process.env.SAUCE_USERNAME) { + console.log(colors.red('SAUCE_USERNAME is missing. Please check the README.md file.')); + process.exit(1); //eslint-disable-line no-process-exit +} -if (process.env.SAUCE_AUTO_CONNECT) { +if (!process.env.SAUCE_ACCESS_KEY2) { + console.log(colors.red('SAUCE_ACCESS_KEY is missing. Please check the README.md file.')); + process.exit(1); //eslint-disable-line no-process-exit +} + + +if (config.TUNNEL_AUTO_CONNECT) { before(function(done) { - // Creating the tunnel takes a bit of time. For this case we can safely disable it. + console.log(colors.yellow('Setting up tunnel from Saucelabs to your lovely computer, will take a while.')); + // Creating the tunnel takes a bit of time. For this case we can safely disable Mocha timeouts. this.timeout(0); sauceConnectLauncher(function (err, sauceConnectProcess) { @@ -30,4 +43,8 @@ if (process.env.SAUCE_AUTO_CONNECT) { globalSauceProcess.close(done); } }); +} else if (config.APP_URL.match(/localhost/)) { + console.log(colors.yellow(`You are running tests on ${config.APP_URL}, make sure you already have a tunnel running.`)); + console.log(colors.yellow('To create the tunnel, run:')); + console.log(colors.yellow(' $ node test/tunnel.js')); } diff --git a/test/test-login.js b/test/test-login.js index 0df913ca..38a1e377 100644 --- a/test/test-login.js +++ b/test/test-login.js @@ -16,6 +16,8 @@ function testSuite(browserName, version, platform) { let browser; before(function() { + // No need to inject `username` or `access_key`, by default the constructor + // looks up the values in `process.env.SAUCE_USERNAME` and `process.env.SAUCE_ACCESS_KEY` browser = wd.promiseChainRemote('ondemand.saucelabs.com', 80); return browser.init({ browserName, version, platform }); }); diff --git a/test/tunnel.js b/test/tunnel.js new file mode 100644 index 00000000..2a7fa371 --- /dev/null +++ b/test/tunnel.js @@ -0,0 +1,23 @@ +'use strict'; + +const config = require('./config'); //eslint-disable-line no-unused-vars +const colors = require('colors'); +const sauceConnectLauncher = require('sauce-connect-launcher'); + + +function connect() { + console.log(colors.yellow('Setting up tunnel from Saucelabs to your lovely computer, will take a while.')); + // Creating the tunnel takes a bit of time. For this case we can safely disable Mocha timeouts. + + sauceConnectLauncher(function (err) { + if (err) { + console.error(err.message); + return; + } + console.log(colors.green('Connected! Keep this process running and execute your tests.')); + }); +} + +if (require.main === module) { + connect(); +}