Skip to content

Commit

Permalink
Improve testing, run tests against production
Browse files Browse the repository at this point in the history
- added note in readme for running tests on real network targets
  • Loading branch information
kg6zvp committed May 10, 2019
1 parent cbadc78 commit cfaf313
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 45 additions & 20 deletions test/bootstrapUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/geo/locate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cfaf313

Please sign in to comment.