-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(instrumentation/expressError): add tests
- Loading branch information
Peter Marton
committed
Jul 6, 2017
1 parent
be1a4be
commit 8795641
Showing
4 changed files
with
90 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict' | ||
|
||
const request = require('super-request') | ||
const { expect } = require('chai') | ||
const { Tracer, Tags } = require('opentracing') | ||
const express = require('express') | ||
const cls = require('../cls') | ||
const instrumentation = require('./expressError') | ||
|
||
describe('instrumentation: expressError', () => { | ||
let tracer | ||
let mockRootSpan | ||
let mockChildSpan | ||
|
||
beforeEach(function () { | ||
tracer = new Tracer() | ||
mockRootSpan = { | ||
setTag: this.sandbox.spy() | ||
} | ||
mockChildSpan = { | ||
setTag: this.sandbox.spy(), | ||
log: this.sandbox.spy(), | ||
finish: this.sandbox.spy() | ||
} | ||
|
||
this.sandbox.stub(cls, 'getRootSpan').callsFake(() => mockRootSpan) | ||
this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan) | ||
|
||
instrumentation.patch(express, tracer) | ||
}) | ||
|
||
afterEach(() => { | ||
instrumentation.unpatch(express) | ||
}) | ||
|
||
describe('#patch', () => { | ||
it('should catch error when error middleware is presented', async () => { | ||
const err = new Error('My Error') | ||
const app = express() | ||
|
||
app.get('/', (req, res, next) => next(err)) | ||
app.use((error, req, res, next) => { | ||
res.statusCode = 500 | ||
res.end() | ||
next() | ||
}) | ||
|
||
await request(app) | ||
.get('/') | ||
.expect(500) | ||
.end() | ||
|
||
expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME) | ||
|
||
expect(mockRootSpan.setTag).to.be.calledWith(Tags.ERROR, true) | ||
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) | ||
expect(mockChildSpan.log).to.be.calledWith({ | ||
event: 'error', | ||
'error.object': err, | ||
message: err.message, | ||
stack: err.stack | ||
}) | ||
expect(mockChildSpan.finish).to.have.callCount(1) | ||
}) | ||
|
||
it.skip('should catch error when error middleware is not presented', async () => { | ||
const err = new Error('My Error') | ||
const app = express() | ||
|
||
app.get('/', (req, res, next) => next(err)) | ||
|
||
await request(app) | ||
.get('/') | ||
.expect(500) | ||
.end() | ||
|
||
expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME) | ||
|
||
expect(mockRootSpan.setTag).to.be.calledWith(Tags.ERROR, true) | ||
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) | ||
expect(mockChildSpan.log).to.be.calledWith({ | ||
event: 'error', | ||
'error.object': err, | ||
message: err.message, | ||
stack: err.stack | ||
}) | ||
expect(mockChildSpan.finish).to.have.callCount(1) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.