add unit tests

This commit is contained in:
Brian Gough 2017-03-28 15:54:12 +01:00
parent 013c2a693c
commit c7a6f44b11
2 changed files with 76 additions and 1 deletions

View file

@ -9,7 +9,17 @@
"version": "1.5.6",
"dependencies": {
"bunyan": "1.5.1",
"chai": "",
"coffee-script": "1.12.4",
"raven": "^1.1.3"
"grunt": "^0.4.5",
"grunt-bunyan": "^0.5.0",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-coffee": "^0.11.0",
"grunt-execute": "^0.2.2",
"grunt-mocha-test": "^0.11.0",
"raven": "^1.1.3",
"sandboxed-module": "",
"sinon": "",
"timekeeper": "^1.0.0"
}
}

View file

@ -0,0 +1,65 @@
require('coffee-script')
chai = require('chai')
should = chai.should()
expect = chai.expect
path = require('path')
modulePath = path.join __dirname, '../../../logging-manager.coffee'
SandboxedModule = require('sandboxed-module')
sinon = require("sinon")
tk = require("timekeeper")
describe 'logger.error', ->
beforeEach ->
@captureException = sinon.stub()
@start = Date.now()
tk.travel(new Date(@start))
@LoggingManager = SandboxedModule.require modulePath, requires:
'bunyan': {createLogger: sinon.stub().returns({error:sinon.stub()})}
'raven': {Client: sinon.stub().returns({captureException:@captureException})}
@logger = @LoggingManager.initialize("test")
@logger.initializeErrorReporting("test_dsn")
it 'should report a single error to sentry', () ->
@logger.error {foo:'bar'}, "message"
@captureException.called.should.equal true
it 'for multiple errors should only report a maximum of 5 errors to sentry', () ->
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@captureException.callCount.should.equal 5
it 'for multiple errors with a minute delay should report 10 errors to sentry', () ->
# the first five errors should be reported to sentry
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
# the following errors should not be reported
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
# allow a minute to pass
tk.travel(new Date(@start + 61*10000));
# after a minute the next five errors should be reported to sentry
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
# the following errors should not be reported to sentry
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@logger.error {foo:'bar'}, "message"
@captureException.callCount.should.equal 10