-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
64 lines (61 loc) · 2.38 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
/**
* empower - Power Assert feature enhancer for assert function/object.
*
* https://github.com/power-assert-js/empower
*
* Copyright (c) 2013-2016 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);
var empowerCoreConfig = assign(config, {
modifyMessageBeforeAssert: function (beforeAssertEvent) {
var message = beforeAssertEvent.originalMessage;
if (!eagerEvaluation) {
return message;
}
return buildPowerAssertText(formatter, message, beforeAssertEvent.powerAssertContext);
},
onError: function (errorEvent) {
var e = errorEvent.error;
if (e.name !== 'AssertionError') {
throw e;
}
if (!errorEvent.powerAssertContext) {
throw e;
}
// console.log(JSON.stringify(errorEvent, null, 2));
if (config.modifyMessageOnRethrow) {
e.message = buildPowerAssertText(formatter, errorEvent.originalMessage, errorEvent.powerAssertContext);
}
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;