1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const AssertionError = require('assertion-error');
|
5 | const Bluebird = require('bluebird');
|
6 |
|
7 | const Description = require('./Description');
|
8 |
|
9 | function promiseThat(reason, actual, matcher) {
|
10 | if (arguments.length === 2) {
|
11 | matcher = actual;
|
12 | actual = reason;
|
13 | reason = '';
|
14 | }
|
15 |
|
16 | return Bluebird.try(() => matcher.matches(actual)).then((result) => {
|
17 | if (!result) {
|
18 | const description = new Description();
|
19 | description.append(reason)
|
20 | .append('\nExpected: ')
|
21 | .appendDescriptionOf(matcher)
|
22 | .append('\n but: ');
|
23 | return Bluebird.try(() => matcher.describeMismatch(actual, description))
|
24 | .then(() => {
|
25 | if (!_.isFunction(matcher.getExpectedForDiff) ||
|
26 | !_.isFunction(matcher.formatActualForDiff)) {
|
27 | return {};
|
28 | }
|
29 |
|
30 | return Bluebird.all([
|
31 | matcher.getExpectedForDiff(),
|
32 | matcher.formatActualForDiff(actual)
|
33 | ]).spread((expected, actual) => {
|
34 | return {
|
35 | showDiff: true,
|
36 | expected: expected,
|
37 | actual: actual
|
38 | };
|
39 | });
|
40 | })
|
41 | .then((errorProperties) => {
|
42 | throw new AssertionError(description.get(), errorProperties, promiseThat);
|
43 | });
|
44 | }
|
45 | });
|
46 | }
|
47 |
|
48 | module.exports = promiseThat;
|