Skip to content

Commit

Permalink
Merge pull request #12 from davesag/feature/20190420/update-deps
Browse files Browse the repository at this point in the history
[optimisation, n/a] updated dependencies and eliminated the last of the mutation test…
  • Loading branch information
davesag committed Apr 18, 2019
2 parents 3f434e3 + da4a3a4 commit c76096c
Show file tree
Hide file tree
Showing 12 changed files with 805 additions and 1,031 deletions.
1,659 changes: 657 additions & 1,002 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -54,7 +54,7 @@
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.17.1",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-mocha": "^5.3.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prettier": "^3.0.1",
Expand All @@ -64,10 +64,10 @@
"lint-staged": "^8.1.5",
"mocha": "^6.1.3",
"mock-req-res": "^1.1.0",
"nyc": "^13.3.0",
"nyc": "^14.0.0",
"prettier": "^1.17.0",
"proxyquire": "^2.1.0",
"sinon": "^7.3.1",
"sinon": "^7.3.2",
"sinon-chai": "^3.3.0",
"supertest": "^4.0.2"
},
Expand Down
10 changes: 3 additions & 7 deletions src/api/index.js
@@ -1,17 +1,13 @@
const path = require('path')
const traverse = require('traverse-folders')

const pathSeparator = new RegExp(path.sep, 'g')
const pathToRoute = require('src/utils/api/pathToRoute')

const apis = {}
const base = __dirname
// const ignore = path.basename(module.filename)

const processor = file => {
const name = file.slice(base.length + 1, -3).replace(pathSeparator, '_')
const name = pathToRoute(file, __dirname)
apis[name] = require(file)
}

traverse(base, processor /* , { ignore } */)
traverse(__dirname, processor)

module.exports = apis
4 changes: 2 additions & 2 deletions src/server.js
Expand Up @@ -9,9 +9,9 @@ const start = async () => {
logger.debug('Server started. Listening on port', PORT)

return { server }
} catch (err) /* istanbul ignore next */ {
} catch (err) {
logger.error('Could not start the server', err)
process.exit(1)
throw err
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/api/detailSummariser.js
@@ -0,0 +1,6 @@
const ROOT = 'root'

const detailSummariser = base => (path, { tags }) =>
Array.isArray(tags) && tags[0] === ROOT ? path : `${base}${path}`

module.exports = detailSummariser
7 changes: 7 additions & 0 deletions src/utils/api/pathToRoute.js
@@ -0,0 +1,7 @@
const path = require('path')
const pathSeparator = new RegExp(path.sep, 'g')

const pathToRoute = (path, base) =>
path.slice(base.length + 1, -3).replace(pathSeparator, '_')

module.exports = pathToRoute
7 changes: 3 additions & 4 deletions src/utils/api/summarisePaths.js
@@ -1,10 +1,9 @@
const normalisePath = path => path.replace(/\{/g, ':').replace(/\}/g, '')
const detailSummariser = require('./detailSummariser')

const detailSummariser = (paths, basePath) => (path, { tags }) =>
tags && tags.length !== 0 && tags[0] === 'root' ? path : `${basePath}${path}`
const normalisePath = path => path.replace(/\{/g, ':').replace(/\}/g, '')

const pathSummariser = (paths, basePath, result) => {
const summariser = detailSummariser(paths, basePath)
const summariser = detailSummariser(basePath)

return path => {
const thePath = paths[path]
Expand Down
5 changes: 5 additions & 0 deletions stryker.conf.js
Expand Up @@ -3,6 +3,7 @@ module.exports = function(config) {
mutate: [
'src/**/*.js',
'!src/index.js',
'!src/errors.js',
'!src/utils/api/apiValidator.js',
'!src/utils/config.js',
'!src/utils/logger.js'
Expand All @@ -11,6 +12,10 @@ module.exports = function(config) {
packageManager: 'npm',
reporters: ['clear-text', 'progress'],
testRunner: 'mocha',
mochaOptions: {
files: ['test/unit/**/*.test.js'],
require: ['test/unit/testHelper.js']
},
transpilers: [],
testFramework: 'mocha',
coverageAnalysis: 'perTest',
Expand Down
71 changes: 62 additions & 9 deletions test/unit/server.test.js
Expand Up @@ -14,21 +14,74 @@ describe('src/server', () => {
'src/utils/logger': logger
})

const resetStubs = () => {
mockMakeApp.reset()
mockApp.listen.reset()
}

const mockServer = 'a server'

let outcome

before(async () => {
mockMakeApp.resolves(mockApp)
mockApp.listen.resolves(mockServer)
outcome = await server.start()
})
context('when it works fine', () => {
before(async () => {
mockMakeApp.resolves(mockApp)
mockApp.listen.resolves(mockServer)
outcome = await server.start()
})

after(resetStubs)

it('called makeApp once', () => {
expect(mockMakeApp).to.have.been.calledOnce
})

it('invoked app.listen', () => {
expect(mockApp.listen).to.have.been.calledOnce
})

it('logged the startup', () => {
expect(logger.debug).to.have.been.calledWith(
'Server started. Listening on port'
)
})

it('invoked app.listen', () => {
expect(mockApp.listen).to.have.been.calledOnce
it('returns the server', () => {
expect(outcome).to.have.property('server', mockServer)
})
})

it('returns the server', () => {
expect(outcome).to.have.property('server', mockServer)
context('when it fails', () => {
const error = new Error('oops')

before(async () => {
mockMakeApp.rejects(error)
try {
await server.start()
} catch (err) {
outcome = err
}
})

after(resetStubs)

it('called makeApp once', () => {
expect(mockMakeApp).to.have.been.calledOnce
})

it('did not invoke app.listen', () => {
expect(mockApp.listen).not.to.have.been.called
})

it('logged the error', () => {
expect(logger.error).to.have.been.calledWith(
'Could not start the server',
error
)
})

it('threw the error', () => {
expect(outcome).to.equal(error)
})
})
})
25 changes: 25 additions & 0 deletions test/unit/utils/api/detailSummariser.test.js
@@ -0,0 +1,25 @@
const { expect } = require('chai')

const detailSummariser = require('src/utils/api/detailSummariser')

describe('src/utils/api/detailSummariser', () => {
const base = '/api/v1'
const path = '/test'
const withBase = `${base}${path}`
const summarise = detailSummariser(base)

const doTest = ([label, tags, expected]) => {
context(label, () => {
it('returns the expected path', () => {
expect(summarise(path, { tags })).to.equal(expected)
})
})
}

;[
['without tags', undefined, withBase],
['with empty tags', [], withBase],
['with non-root tag', ['something'], withBase],
['with root tag', ['root'], path]
].forEach(doTest)
})
13 changes: 13 additions & 0 deletions test/unit/utils/api/pathToRoute.test.js
@@ -0,0 +1,13 @@
const { expect } = require('chai')

const pathToRoute = require('src/utils/api/pathToRoute')

describe('src/utils/api/pathToRoute', () => {
const base = 'starts-here'
const file = `${base}/my/awesome/file.js`
const expected = 'my_awesome_file'

it('converts the path to a route as expected', () => {
expect(pathToRoute(file, base)).to.equal(expected)
})
})
23 changes: 19 additions & 4 deletions test/unit/utils/makeApp.test.js
Expand Up @@ -25,12 +25,15 @@ describe('src/utils/makeApp', () => {
use: mockUse,
set: mockSet
})
const fakeUI = 'some UI thing'
const mockUiExpress = { setup: stub().returns(fakeUI), serve: stub() }

const makeApp = proxyquire('src/utils/makeApp', {
express: fakeExpress,
cors: mockCors,
'body-parser': fakeBodyParser,
'swagger-routes-express': mockApiConnector,
'swagger-ui-express': mockUiExpress,
'src/utils/notFoundError': fakeNotFoundError,
'src/utils/api/apiDefinition': fakeApiDefinition,
'src/utils/api/apiValidator': mockApiValidator,
Expand All @@ -47,20 +50,32 @@ describe('src/utils/makeApp', () => {
expect(mockCors).to.have.been.calledOnce
expect(mockUse).to.have.been.calledWith(fakeCors)
})
// app.use('/docs', swaggerUi.serve, swaggerUi.setup(apiDefinition))

it('uses docs', () => {
expect(mockUiExpress.setup).to.have.been.calledOnceWith(
fakeApiDefinition.apiDefinition
)
expect(mockUse).to.have.been.calledWith(
'/docs',
mockUiExpress.serve,
fakeUI
)
})

it('uses bodyParser.json', () => {
expect(fakeBodyParser.json).to.have.been.calledOnce
expect(mockUse).to.have.been.calledWith('json-parser')
})

it('creates the api validator', () => {
expect(mockApiValidator).to.have.been.called
})

it('sets trust proxy to true', () => {
expect(mockSet).to.have.been.calledWith('trust proxy', true)
})

it('creates the api validator', () => {
expect(mockApiValidator).to.have.been.called
})

it('uses the api validator', () => {
expect(mockUse).to.have.been.calledWith('api-validator')
})
Expand Down

0 comments on commit c76096c

Please sign in to comment.