-
Notifications
You must be signed in to change notification settings - Fork 5
/
unexpectedExpress.js
132 lines (117 loc) · 4.3 KB
/
unexpectedExpress.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const messy = require('messy');
const resolveExpectedResponseProperties = require('./resolveExpectedResponseProperties');
const UnexpectedExpressMocker = require('./UnexpectedExpressMocker');
module.exports = {
name: 'unexpected-express',
version: require('../package.json').version,
installInto(expect) {
expect = expect.child();
const topLevelExpect = expect;
expect.use(require('unexpected-messy'));
expect.addType({
name: 'IncomingMessage',
base: 'object',
identify(obj) {
return (
obj && obj.constructor && obj.constructor.name === 'IncomingMessage'
);
},
inspect(obj, depth, output) {
output.text(obj.constructor.name, 'jsFunctionName');
},
});
expect.exportAssertion(
[
'<function> to yield exchange satisfying <any>', // Please prefer this one because it does use 'to satisfy' semantics
'<function> to yield exchange <any>',
],
(expect, subject, value) => {
if (!subject.handle || !subject.set) {
expect.subjectOutput = function () {
this.text('express middleware');
};
} else {
expect.subjectOutput = function () {
this.text('express app');
};
}
const missingProperties = Object.keys(value).filter(
(key) => key !== 'request' && key !== 'response'
);
if (missingProperties.length > 0) {
throw new Error(`Property "${missingProperties[0]}" does not exist`);
}
const { expectedResponseProperties, expectedMetadata } =
resolveExpectedResponseProperties(value);
const mocker = new UnexpectedExpressMocker(subject);
const options = {
...value,
expectedErrorPassedToNext: expectedMetadata.errorPassedToNext,
};
return mocker.mock(options).then((context) => {
const { errorPassedToNext } = context.metadata;
// rethrow error that was passed to next() and captured as handled
if (errorPassedToNext && errorPassedToNext.UnexpectedExpressError) {
const { name, data } = errorPassedToNext;
if (['UnknownRouteError', 'SilentRouteError'].includes(name)) {
throw errorPassedToNext.data.error; // original error
} else {
context = { ...context };
context.metadata = { ...context.metadata };
context.metadata.errorPassedToNext = data.error;
}
}
const promiseByKey = {
httpExchange: expect.promise(() =>
expect(context.httpExchange, 'to satisfy', {
response: expectedResponseProperties,
})
),
metadata: {},
};
for (const key of Object.keys(expectedMetadata)) {
promiseByKey.metadata[key] = expect.promise(() =>
topLevelExpect(
context.metadata[key],
'to satisfy',
expectedMetadata[key]
)
);
}
return expect.promise.settle(promiseByKey).then((promises) => {
if (promises.some((promise) => promise.isRejected())) {
expect.fail({
diff(output) {
if (promiseByKey.httpExchange.isRejected()) {
output.append(
promiseByKey.httpExchange.reason().getDiff(output)
);
} else {
output.appendInspected(context.httpExchange);
}
Object.keys(promiseByKey.metadata).forEach((key) => {
if (promiseByKey.metadata[key].isRejected()) {
output.nl().annotationBlock(function () {
this.text(key)
.text(':')
.sp()
.append(
promiseByKey.metadata[key]
.reason()
.getErrorMessage(output)
);
});
}
});
return output;
},
});
}
return context;
});
});
}
);
},
};
module.exports.messy = messy;