This repository was archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathenhance-assert.js
53 lines (49 loc) · 1.62 KB
/
enhance-assert.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
'use strict';
const concordance = require('concordance');
const dotProp = require('dot-prop');
const generate = require('babel-generator').default;
const concordanceOptions = require('./concordance-options').default;
// When adding patterns, don't forget to add to
// https://github.com/avajs/babel-preset-transform-test-files/blob/master/espower-patterns.json
// Then release a new version of that preset and bump the SemVer range here.
const PATTERNS = [
't.truthy(value, [message])',
't.falsy(value, [message])',
't.true(value, [message])',
't.false(value, [message])',
't.regex(contents, regex, [message])',
't.notRegex(contents, regex, [message])'
];
const computeStatement = node => generate(node, {quotes: 'single'}).code;
const getNode = (ast, path) => dotProp.get(ast, path.replace(/\//g, '.'));
const formatter = context => {
const ast = JSON.parse(context.source.ast);
const args = context.args[0].events;
return args
.map(arg => {
const node = getNode(ast, arg.espath);
const statement = computeStatement(node);
const formatted = concordance.format(arg.value, concordanceOptions);
return [statement, formatted];
})
.reverse();
};
const enhanceAssert = (pass, fail, assertions) => {
const empower = require('empower-core');
return empower(assertions, {
destructive: true,
onError(event) {
const error = event.error;
if (event.powerAssertContext) { // Context may be missing in internal tests.
error.statements = formatter(event.powerAssertContext);
}
fail(this, error);
},
onSuccess() {
pass(this);
},
patterns: PATTERNS,
bindReceiver: false
});
};
module.exports = enhanceAssert;