From cfaf313c7d685fdc5ac05de0a6dc9ecf7df99a53 Mon Sep 17 00:00:00 2001 From: Sam McCollum Date: Thu, 9 May 2019 17:13:08 -0600 Subject: [PATCH] Improve testing, run tests against production - added note in readme for running tests on real network targets --- README.md | 18 ++++++++++++ package.json | 4 +-- test/bootstrapUtil.js | 65 ++++++++++++++++++++++++++++++------------- test/geo/locate.js | 2 +- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 551b520..8b839d7 100644 --- a/README.md +++ b/README.md @@ -253,3 +253,21 @@ Exposes [superagent](https://visionmedia.github.io/superagent/) with no modifica [travis-url]: https://travis-ci.org/staeco/vandelay-util [travis-image]: https://travis-ci.org/staeco/vandelay-util.png?branch=master + +## Development + +### Running Tests + +In order to execute tests against network services, ensure the environment variable `VANDELAY_UTIL_CONFIG` contains the path to a javascript or json file with the required configuration. + +You can do so by adding the following to your `~/.bashrc`: + +```sh +export VANDELAY_UTIL_CONFIG="/home/gilbates/git/van-config/config.js" +``` + +When you wish for the tests to utilize the network services configured in `${VANDELAY_UTIL_CONFIG}`, define the variable `USE_NET=true` like so: + +```sh +USE_NET=true npm test +``` diff --git a/package.json b/package.json index fce8573..ad1b647 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,8 @@ "clean": "rimraf dist", "lint": "eslint src test", "lint:fix": "eslint src test --fix", - "test": "mocha --require babel-register --recursive --reporter spec && npm run-script lint", - "test:debug": "mocha --inspect-brk=0.0.0.0 --require babel-register --recursive --reporter spec" + "test": "mocha --require babel-register --recursive --reporter spec --exit && npm run-script lint", + "test:debug": "mocha --inspect-brk=0.0.0.0 --require babel-register --recursive --reporter spec --exit" }, "devDependencies": { "babel-cli": "^6.4.0", diff --git a/test/bootstrapUtil.js b/test/bootstrapUtil.js index 2c168da..711d716 100644 --- a/test/bootstrapUtil.js +++ b/test/bootstrapUtil.js @@ -9,15 +9,29 @@ const structuredURL = '/v1/search/structured/' const searchURL = '/v1/search' const routeURL = '/route' -const getPeliasConfig = (port) => ({ - key: 'stae-1234', - hosts: { - trace: `http://localhost:${port}${traceURL}`, - structured: `http://localhost:${port}${structuredURL}`, - search: `http://localhost:${port}${searchURL}`, - route: `http://localhost:${port}${routeURL}` +const useNetwork = () => { + const { USE_NET, VANDELAY_UTIL_CONFIG } = process.env + return USE_NET && USE_NET.toLowerCase() === 'true' && VANDELAY_UTIL_CONFIG +} + +const getPeliasConfig = async (port) => { + if (useNetwork()) { + const { VANDELAY_UTIL_CONFIG } = process.env + const conf = await require(VANDELAY_UTIL_CONFIG) + return conf } -}) + return { + pelias: { + key: 'stae-1234', + hosts: { + trace: `http://localhost:${port}${traceURL}`, + structured: `http://localhost:${port}${structuredURL}`, + search: `http://localhost:${port}${searchURL}`, + route: `http://localhost:${port}${routeURL}` + } + } + } +} /** * Wrap requests to ensure API key passthrough @@ -29,24 +43,35 @@ const wrap = (lam) => (req, res) => { return lam(req, res) } -export const cleanupUtil = () => { +const cleanupUtil = () => { Object.keys(require.cache) .filter((m) => m.includes('src/util') || m.includes('src/lib')) .map((m) => delete require.cache[m]) // remove from cache so they can be initialized properly } -export default async (responses) => { - const port = await getPort() - const app = express() - if (responses) { - if (responses.structured) app.get(structuredURL, wrap(responses.structured)) - if (responses.trace) console.log('TRACE NOT IMPLEMENTED!') - if (responses.search) console.log('SEARCH NOT IMPLEMENTED!') - if (responses.route) app.get(routeURL, wrap(responses.route)) +const bootstrapUtil = async (responses) => { + let port + let server + if (!useNetwork()) { + port = await getPort() + const app = express() + if (responses) { + if (responses.structured) app.get(structuredURL, wrap(responses.structured)) + if (responses.trace) console.log('TRACE NOT IMPLEMENTED!') + if (responses.search) console.log('SEARCH NOT IMPLEMENTED!') + if (responses.route) app.get(routeURL, wrap(responses.route)) + } + server = app.listen(port) } - const server = app.listen(port) cleanupUtil() - const util = createUtil({ pelias: getPeliasConfig(port) }) - util.close = () => server.close() + const util = createUtil(await getPeliasConfig(port)) + util.close = () => { + cleanupUtil() + return server && server.close() + } return util } + +bootstrapUtil.cleanupUtil = cleanupUtil + +export default bootstrapUtil diff --git a/test/geo/locate.js b/test/geo/locate.js index 81f898a..585767e 100644 --- a/test/geo/locate.js +++ b/test/geo/locate.js @@ -7,7 +7,7 @@ import harrisonStRealAddr from '../fixtures/pelias-structured-401_harrison_st-sy let util describe('geo#locate', function () { - afterEach('cleanup util', () => util && util.close()) + afterEach('cleanup util', () => util && util.close && util.close()) it('should exist', async () => { const util = await bootstrapUtil() should.exist(util.geo.locate)