This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
85 lines (66 loc) · 2.33 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const test = require('tape');
const WaitExpressMiddleware = require('./index');
test('A identity middleware', (t) => {
function identityMiddleware(req, res, next) {
next();
};
const reqMock = {};
t.plan(1);
WaitExpressMiddleware(identityMiddleware, reqMock)
.then(({ req, res, next }) => {
t.deepEqual(next, [], '`next` callback was called, interrupting the middleware.');
t.end();
});
});
test('A middleware that passes a error to the next middleware', (t) => {
function middlewareWithError(req, res, next) {
next('ERROR!');
};
const reqMock = {};
t.plan(2);
WaitExpressMiddleware(middlewareWithError, reqMock)
.then(({ req, res, next }) => {
t.equal(next.length, 1, '`next` callback was called, interrupting the middleware.');
t.deepEqual(next, [ 'ERROR!' ], 'we captured the error handled to the next callback');
t.end();
});
});
test('A middleware that throws a error', (t) => {
function neverEndingMiddleware(req, res, next) {
throw new Error('Such error');
}
const reqMock = {};
t.plan(2);
WaitExpressMiddleware(neverEndingMiddleware, reqMock)
.catch((err) => {
t.assert(err, 'captured the exception thrown');
t.equal(err.message, 'Such error', 'was the error we created');
t.end();
});
});
test('A middleware "sends a json"', (t) => {
function neverEndingMiddleware(req, res, next) {
res.json({ doge: "amaze" });
};
const reqMock = {};
t.plan(2);
WaitExpressMiddleware(neverEndingMiddleware, reqMock)
.then(({ req, res, next }) => {
t.assert(res.json, 'captured a json response');
t.deepEqual(res.json, { doge: "amaze" }, 'captured the correct json response');
t.end();
});
});
test('A middleware that sets a HTTP status', (t) => {
function neverEndingMiddleware(req, res, next) {
res.status(418).json({ teapot: "excite" });
}
const reqMock = {};
t.plan(2);
WaitExpressMiddleware(neverEndingMiddleware, reqMock)
.then(({ req, res, next }) => {
t.assert(res.status, 'captured a status');
t.equal(res.status, 418, 'captured the correct HTTP status supplied');
t.end();
});
});