diff --git a/test/helpers/gather-os-metrics.spec.js b/test/helpers/gather-os-metrics.spec.js index 7d099a1..d5003fd 100644 --- a/test/helpers/gather-os-metrics.spec.js +++ b/test/helpers/gather-os-metrics.spec.js @@ -4,17 +4,15 @@ chai.should(); const gatherOsMetrics = require('../../src/helpers/gather-os-metrics'); -describe('helpers', () => { - describe('gather-os-metrics', () => { - describe('when invoked', () => { - it('then ...', () => { - const span = { os: [], responses: [] }; +describe('gather-os-metrics', () => { + describe('when invoked', () => { + it('then ...', () => { + const span = { os: [], responses: [] }; - gatherOsMetrics({}, span); + gatherOsMetrics({}, span); - // todo: not sure what should I test, maybe the resulted span structure? - // todo: also this component has got some internal timing events? - }); + // todo: not sure what should I test, maybe the resulted span structure? + // todo: also this component has got some internal timing events? }); }); }); diff --git a/test/helpers/on-headers-listener.spec.js b/test/helpers/on-headers-listener.spec.js index 0abf95c..0b03c9d 100644 --- a/test/helpers/on-headers-listener.spec.js +++ b/test/helpers/on-headers-listener.spec.js @@ -6,42 +6,40 @@ chai.should(); const onHeadersListener = require('../../src/helpers/on-headers-listener'); const defaultConfig = require('../../src/helpers/default-config'); -describe('helpers', () => { - describe('on-headers-listener', () => { - describe('when invoked', () => { - const clock = sinon.useFakeTimers(); - const spans = defaultConfig.spans; - - before(() => { - spans.forEach((span) => { - span.responses = []; - }); +describe('on-headers-listener', () => { + describe('when invoked', () => { + const clock = sinon.useFakeTimers(); + const spans = defaultConfig.spans; + + before(() => { + spans.forEach((span) => { + span.responses = []; }); + }); - after(() => { - clock.restore(); - }); + after(() => { + clock.restore(); + }); - it('then for all spans, responses length should equal 1', () => { - onHeadersListener(404, process.hrtime(), spans); + it('then for all spans, responses length should equal 1', () => { + onHeadersListener(404, process.hrtime(), spans); - spans.forEach((span) => { - span.responses.length.should.equal(1); - }); + spans.forEach((span) => { + span.responses.length.should.equal(1); }); + }); - describe('when invoked after 1 second', () => { - it('then for span interval 1, responses length should equal 2', () => { - clock.tick(1000); - onHeadersListener(500, process.hrtime(), spans); - - spans.forEach((span) => { - if (span.interval === 1) { - span.responses.length.should.equal(2); - } else { - span.responses.length.should.equal(1); - } - }); + describe('when invoked after 1 second', () => { + it('then for span interval 1, responses length should equal 2', () => { + clock.tick(1000); + onHeadersListener(500, process.hrtime(), spans); + + spans.forEach((span) => { + if (span.interval === 1) { + span.responses.length.should.equal(2); + } else { + span.responses.length.should.equal(1); + } }); }); }); diff --git a/test/helpers/send-metrics.spec.js b/test/helpers/send-metrics.spec.js index 4bea6e0..f48a80d 100644 --- a/test/helpers/send-metrics.spec.js +++ b/test/helpers/send-metrics.spec.js @@ -5,17 +5,15 @@ chai.should(); const sendMetrics = require('../../src/helpers/send-metrics'); -describe('helpers', () => { - describe('send-metrics', () => { - describe('when invoked', () => { - it('then io.emit called', () => { - const io = { emit: sinon.stub() }; - const span = { os: [], responses: [] }; +describe('send-metrics', () => { + describe('when invoked', () => { + it('then io.emit called', () => { + const io = { emit: sinon.stub() }; + const span = { os: [], responses: [] }; - sendMetrics(io, span); + sendMetrics(io, span); - sinon.assert.calledWith(io.emit, 'esm_stats'); - }); + sinon.assert.calledWith(io.emit, 'esm_stats'); }); }); }); diff --git a/test/helpers/socket-io-init.spec.js b/test/helpers/socket-io-init.spec.js index bedc698..520eb4a 100644 --- a/test/helpers/socket-io-init.spec.js +++ b/test/helpers/socket-io-init.spec.js @@ -5,24 +5,22 @@ chai.should(); const socketIoInit = require('../../src/helpers/socket-io-init'); const defaultConfig = require('../../src/helpers/default-config'); -describe('helpers', () => { - describe('socket-io-init', () => { - describe('when invoked', () => { - it('then all spans should have os and responses property', () => { - const spans = defaultConfig.spans; +describe('socket-io-init', () => { + describe('when invoked', () => { + it('then all spans should have os and responses property', () => { + const spans = defaultConfig.spans; - spans.forEach((span) => { - span.should.not.have.property('os'); - // info: not working as if it was another test interfering - // span.should.not.have.property('responses'); - }); + spans.forEach((span) => { + span.should.not.have.property('os'); + // info: not working as if it was another test interfering + // span.should.not.have.property('responses'); + }); - socketIoInit({}, defaultConfig); + socketIoInit({}, defaultConfig); - spans.forEach((span) => { - span.should.have.property('os'); - span.should.have.property('responses'); - }); + spans.forEach((span) => { + span.should.have.property('os'); + span.should.have.property('responses'); }); }); }); diff --git a/test/helpers/validate.spec.js b/test/helpers/validate.spec.js index ae68d0e..5ae9404 100644 --- a/test/helpers/validate.spec.js +++ b/test/helpers/validate.spec.js @@ -6,79 +6,77 @@ chai.should(); const defaultConfig = require('../../src/helpers/default-config'); const validate = require('../../src/helpers/validate'); -describe('helpers', () => { - describe('validate', () => { - describe('when config is null or undefined', () => { - const config = validate(); - - it(`then title === ${defaultConfig.title}`, () => { - config.title.should.equal(defaultConfig.title); - }); - - it(`then path === ${defaultConfig.path}`, () => { - config.path.should.equal(defaultConfig.path); - }); - - it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => { - config.spans.should.equal(defaultConfig.spans); - }); - - it('then port === null', () => { - chai.expect(config.port).to.be.null; - }); - - it('then websocket === null', () => { - chai.expect(config.websocket).to.be.null; - }); +describe('validate', () => { + describe('when config is null or undefined', () => { + const config = validate(); + + it(`then title === ${defaultConfig.title}`, () => { + config.title.should.equal(defaultConfig.title); }); - describe('when config is invalid', () => { - const config = validate({ title: true, path: false, spans: 'not-an-array', port: 'abc', websocket: false }); + it(`then path === ${defaultConfig.path}`, () => { + config.path.should.equal(defaultConfig.path); + }); - it(`then title === ${defaultConfig.title}`, () => { - config.title.should.equal(defaultConfig.title); - }); + it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => { + config.spans.should.equal(defaultConfig.spans); + }); - it(`then path === ${defaultConfig.path}`, () => { - config.path.should.equal(defaultConfig.path); - }); + it('then port === null', () => { + chai.expect(config.port).to.be.null; + }); - it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => { - config.spans.should.equal(defaultConfig.spans); - }); + it('then websocket === null', () => { + chai.expect(config.websocket).to.be.null; + }); + }); - it('then port === null', () => { - chai.expect(config.port).to.be.null; - }); + describe('when config is invalid', () => { + const config = validate({ title: true, path: false, spans: 'not-an-array', port: 'abc', websocket: false }); - it('then websocket === null', () => { - chai.expect(config.websocket).to.be.null; - }); + it(`then title === ${defaultConfig.title}`, () => { + config.title.should.equal(defaultConfig.title); }); - describe('when config is valid', () => { - const customConfig = { title: 'Custom title', path: '/custom-path', spans: [{}, {}, {}], port: 9999, websocket: {} }; - const config = validate(customConfig); + it(`then path === ${defaultConfig.path}`, () => { + config.path.should.equal(defaultConfig.path); + }); - it(`then title === ${customConfig.title}`, () => { - config.title.should.equal(customConfig.title); - }); + it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => { + config.spans.should.equal(defaultConfig.spans); + }); - it(`then path === ${customConfig.path}`, () => { - config.path.should.equal(customConfig.path); - }); + it('then port === null', () => { + chai.expect(config.port).to.be.null; + }); - it(`then spans === ${JSON.stringify(customConfig.spans)}`, () => { - config.spans.should.equal(customConfig.spans); - }); + it('then websocket === null', () => { + chai.expect(config.websocket).to.be.null; + }); + }); + + describe('when config is valid', () => { + const customConfig = { title: 'Custom title', path: '/custom-path', spans: [{}, {}, {}], port: 9999, websocket: {} }; + const config = validate(customConfig); - it('then websocket === {}', () => { - config.websocket.should.deep.equal({}); - }); + it(`then title === ${customConfig.title}`, () => { + config.title.should.equal(customConfig.title); + }); + + it(`then path === ${customConfig.path}`, () => { + config.path.should.equal(customConfig.path); + }); + + it(`then spans === ${JSON.stringify(customConfig.spans)}`, () => { + config.spans.should.equal(customConfig.spans); + }); + + it('then websocket === {}', () => { + config.websocket.should.deep.equal({}); + }); - it(`then port === ${customConfig.port}`, () => { - config.port.should.equal(customConfig.port); - }); + it(`then port === ${customConfig.port}`, () => { + config.port.should.equal(customConfig.port); }); }); }); diff --git a/test/middleware-wrapper.spec.js b/test/middleware-wrapper.spec.js index d8b8fa4..478481c 100644 --- a/test/middleware-wrapper.spec.js +++ b/test/middleware-wrapper.spec.js @@ -6,11 +6,11 @@ chai.should(); const expresStatusMonitor = require('../src/middleware-wrapper'); const defaultConfig = require('../src/helpers/default-config'); -describe('express-status-monitor', () => { +describe('middleware-wrapper', () => { describe('when initialised', () => { const middleware = expresStatusMonitor(); - it('then it should be an instance of Function', () => { + it('then it should be an instance of a Function', () => { middleware.should.be.an.instanceof(Function); }); @@ -35,7 +35,27 @@ describe('express-status-monitor', () => { sinon.assert.notCalled(res.send); }); - describe('and used as separate middlware and page handler', () => { + it('and res.removeHeader is present, then header is removed', () => { + const middlewareWithConfig = expresStatusMonitor({ + iframe: true, + }); + const resWithHeaders = Object.assign({}, res); + resWithHeaders.headers = { + 'X-Frame-Options': 1, + }; + resWithHeaders.removeHeader = sinon.stub(); + + middlewareWithConfig(req, resWithHeaders, next); + sinon.assert.called(resWithHeaders.removeHeader); + + resWithHeaders.removeHeader = undefined; + resWithHeaders.remove = sinon.stub(); + + middlewareWithConfig(req, resWithHeaders, next); + sinon.assert.called(resWithHeaders.remove); + }); + + describe('and used as separate middleware and page handler', () => { it('exposes a page handler', () => { middleware.pageRoute.should.be.an.instanceof(Function); middleware.pageRoute(req, res, next);