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
/
index.js
61 lines (55 loc) · 1.79 KB
/
index.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
/**
* An Application-level and Router-level middleware test double
* ensemble to test Express-compatible middleware functions.
*
* @param {function} middleware Express middleware function to be
* tested. It will be passed appropriate Express-like `res` and
* `next` test doubles, to follow it's lifecycle.
* @param {object} req A test double object for the Express-like
* HTTP request object.
* @returns {Promise} A promise that resolves whenever the
* `middleware` callback ends the response cycle or called the
* next middleware function in the stack. It's resolved with the
* object { req, res, next }
*/
function waitExpressMiddleware(middleware, req) {
return new Promise((resolve) => {
/**
* The future result of the awaited promise.
* Each key represents the req, res and next elements
* after the middleware "is halted".
* @type object
*/
const endState = {
req,
res: { status: undefined },
next: undefined
};
/**
* wait-express-middleware test double object for an
* Express-like HTTP `res` response object.
* @type object
*/
const res = {
json(j) {
endState.res.json = j;
resolve(endState);
},
status(s) {
endState.res.status = s;
return res;
}
};
/**
* wait-express-middleware test double object for an
* Express-like `next` callback function
* @type function
*/
const next = function (...err) {
endState.next = err;
resolve(endState);
};
middleware(req, res, next);
})
}
module.exports = waitExpressMiddleware;