-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
95 lines (91 loc) · 3.59 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
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
/**
* empower - Power Assert feature enhancer for assert function/object.
*
* https://github.com/power-assert-js/empower
*
* Copyright (c) 2013-2018 Takuto Wada
* Licensed under the MIT license.
* https://github.com/power-assert-js/empower/blob/master/MIT-LICENSE.txt
*/
var empowerCore = require('empower-core');
var defaultOptions = require('./lib/default-options');
var capturable = require('./lib/capturable');
var assign = require('core-js/library/fn/object/assign');
var define = require('./lib/define-properties');
/**
* Enhance Power Assert feature to assert function/object.
* @param assert target assert function or object to enhance
* @param formatter power assert format function
* @param options enhancement options
* @return enhanced assert function/object
*/
function empower (assert, formatter, options) {
var config = assign(defaultOptions(), options);
var eagerEvaluation = !(config.modifyMessageOnRethrow || config.saveContextOnRethrow);
// see: https://github.com/power-assert-js/empower/pull/26
var shouldRecreateAssertionError = (function isStackUnchanged () {
if (typeof assert !== 'function') {
return false;
}
if (typeof assert.AssertionError !== 'function') {
return false;
}
var ae = new assert.AssertionError({
actual: 123,
expected: 456,
operator: '==='
});
ae.message = '[REPLACED MESSAGE]';
return !(/REPLACED MESSAGE/.test(ae.stack)) && /123 === 456/.test(ae.stack);
})();
var empowerCoreConfig = assign(config, {
modifyMessageBeforeAssert: function (beforeAssertEvent) {
var message = beforeAssertEvent.originalMessage;
if (!eagerEvaluation) {
return message;
}
return buildPowerAssertText(formatter, message, beforeAssertEvent.powerAssertContext);
},
onError: function onError (errorEvent) {
var e = errorEvent.error;
if (!/^AssertionError/.test(e.name)) {
throw e;
}
if (!errorEvent.powerAssertContext) {
throw e;
}
var poweredMessage;
if (config.modifyMessageOnRethrow || config.saveContextOnRethrow) {
poweredMessage = buildPowerAssertText(formatter, errorEvent.originalMessage, errorEvent.powerAssertContext);
if (shouldRecreateAssertionError) {
e = new assert.AssertionError({
message: poweredMessage,
actual: e.actual,
expected: e.expected,
operator: e.operator,
stackStartFunction: e.stackStartFunction || onError
});
e.generatedMessage = false;
}
}
if (config.modifyMessageOnRethrow && !shouldRecreateAssertionError) {
e.message = poweredMessage;
e.generatedMessage = false;
}
if (config.saveContextOnRethrow) {
e.powerAssertContext = errorEvent.powerAssertContext;
}
throw e;
}
});
var enhancedAssert = empowerCore(assert, empowerCoreConfig);
define(enhancedAssert, capturable());
return enhancedAssert;
}
function buildPowerAssertText (formatter, message, context) {
// console.log(message);
var powerAssertText = formatter(context);
return message ? message + ' ' + powerAssertText : powerAssertText;
};
empower.defaultOptions = defaultOptions;
module.exports = empower;