1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const AssertionError = require('assertion-error');
|
5 | const Description = require('./Description');
|
6 | const asMatcher = require('./utils/asMatcher');
|
7 |
|
8 | const processArgs = (args) => {
|
9 | const hasThreeArgs = args.length === 3;
|
10 | const [reason, actual, maybeMatcher] = hasThreeArgs ? args : ['', ...args];
|
11 | return {reason, actual, matcher: asMatcher(maybeMatcher)};
|
12 | };
|
13 |
|
14 | const assertThat = (...args) => {
|
15 | const {reason, matcher, actual} = processArgs(args);
|
16 | const matches = matcher.matches(actual);
|
17 |
|
18 | if (matches && _.isFunction(matches.then)) {
|
19 | throw new AssertionError('Matcher returned a promise instead of a boolean - use promiseThat for promising matchers!', {}, assertThat);
|
20 | }
|
21 |
|
22 | if (!matches) {
|
23 | const description = new Description();
|
24 | description.append(reason)
|
25 | .append('\nExpected: ')
|
26 | .appendDescriptionOf(matcher)
|
27 | .append('\n but: ');
|
28 | matcher.describeMismatch(actual, description);
|
29 |
|
30 | let errorProperties = {};
|
31 | if (_.isFunction(matcher.getExpectedForDiff) &&
|
32 | _.isFunction(matcher.formatActualForDiff)) {
|
33 | errorProperties = {
|
34 | showDiff: true,
|
35 | expected: matcher.getExpectedForDiff(),
|
36 | actual: matcher.formatActualForDiff(actual)
|
37 | };
|
38 | }
|
39 |
|
40 | throw new AssertionError(description.get(), errorProperties, assertThat);
|
41 | } else {
|
42 | if (global && global.expect) {
|
43 | const expectation = global.expect();
|
44 | if (expectation && expectation.nothing) {
|
45 | expectation.nothing();
|
46 | }
|
47 | }
|
48 | }
|
49 | };
|
50 |
|
51 | module.exports = assertThat;
|