Skip to content
This repository was archived by the owner on Jun 19, 2022. It is now read-only.

Commit 00d5bd9

Browse files
committed
Fix express tests
1 parent d7bee48 commit 00d5bd9

File tree

2 files changed

+61
-51
lines changed

2 files changed

+61
-51
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
"stack-trace": "~0.0.9"
1212
},
1313
"devDependencies": {
14-
"express": "^4.13.4",
14+
"express": "^4.17.1",
1515
"mocha": "^5.1.1",
1616
"nock": "^10.0.0",
1717
"sinon": "^9.0.1",
18-
"supertest": "^4.0.0"
18+
"supertest": "^4.0.2"
1919
},
2020
"scripts": {
2121
"test": "mocha"

test/middleware.js

+59-49
Original file line numberDiff line numberDiff line change
@@ -6,93 +6,103 @@ var assert = require('assert'),
66
Singleton = require('../lib/honeybadger');
77

88
describe('Express Middleware', function () {
9-
var subject, client_mock, client;
10-
var error = new Error('Badgers!');
9+
let subject, client_mock, client;
10+
const error = new Error('Badgers!');
1111

12-
setup(function() {
12+
beforeEach(function() {
1313
client = Singleton.factory({ apiKey: 'fake api key' });
1414
subject = client.errorHandler;
15-
1615
client_mock = sinon.mock(client);
1716
});
1817

19-
it('calls next', function() {
20-
var app = express();
21-
var expected = sinon.spy();
18+
it('is sane', function(done) {
19+
const app = express();
2220

23-
app.use(function(req, res, next) {
24-
throw(error);
25-
});
26-
app.use(subject);
27-
app.use(function(err, req, res, next) {
28-
expected();
21+
app.get('/user', function(req, res) {
22+
res.status(200).json({ name: 'john' });
2923
});
3024

31-
request(app.listen())
32-
.get('/')
33-
.end(function(err, res){
34-
if (err) return done(err);
35-
assert(expected.called);
36-
done();
37-
});
25+
request(app)
26+
.get('/user')
27+
.set('Accept', 'application/json')
28+
.expect('Content-Type', /json/)
29+
.expect(200, done);
3830
});
3931

40-
it('reports the error to Honeybadger', function(done) {
41-
var app = express();
32+
it('reports the error to Honeybadger and calls next error handler', function(done) {
33+
const app = express();
34+
const expected = sinon.spy();
4235

43-
app.use(function(req, res, next) {
36+
app.use(client.requestHandler);
37+
38+
app.get('/', function(req, res) {
4439
throw(error);
4540
});
41+
4642
app.use(subject);
4743

48-
request(app.listen())
49-
.get('/')
50-
.end(function(err, res){
51-
if (err) return done(err);
52-
client_mock.verify();
53-
done();
44+
app.use(function(err, req, res, next) {
45+
expected();
46+
next();
47+
});
48+
49+
client_mock.expects('notify').once().withArgs(error);
50+
51+
request(app)
52+
.get('/')
53+
.expect(500, function() {
54+
client_mock.verify();
55+
assert(expected.calledOnce);
56+
done();
5457
});
5558
});
5659

57-
it('reports async errors to Honeybadger', function(done) {
58-
var app = express();
60+
it('reports async errors to Honeybadger and calls next error handler', function(done) {
61+
const app = express();
62+
const expected = sinon.spy();
5963

6064
app.use(client.requestHandler);
61-
app.use(function(req, res, next) {
65+
66+
app.get('/', function(req, res) {
6267
setTimeout(function asyncThrow() {
6368
throw(error);
6469
}, 0);
6570
});
71+
6672
app.use(subject);
6773

74+
app.use(function(err, req, res, next) {
75+
expected();
76+
next();
77+
});
78+
6879
client_mock.expects('notify').once().withArgs(error);
6980

70-
request(app.listen())
71-
.get('/')
72-
.end(function(err, res){
73-
if (err) return done(err);
74-
client_mock.verify();
75-
done();
76-
});
81+
request(app)
82+
.get('/')
83+
.expect(500, function() {
84+
client_mock.verify();
85+
assert(expected.calledOnce);
86+
done();
87+
});
7788
});
7889

7990
it('provides a noop metricsHandler', function(done) {
80-
var app = express();
81-
var spy = sinon.spy();
91+
const app = express();
92+
const expected = sinon.spy();
8293

8394
app.use(client.metricsHandler);
8495
app.use(function(req, res, next) {
85-
spy();
96+
expected();
8697
next();
8798
});
8899

89-
request(app.listen())
90-
.get('/')
91-
.end(function(err, res){
92-
if (err) return done(err);
93-
assert(spy.calledOnce);
94-
done();
95-
});
100+
request(app)
101+
.get('/')
102+
.expect(200, function() {
103+
assert(expected.calledOnce);
104+
done();
105+
});
96106
});
97107
});
98108

0 commit comments

Comments
 (0)