UNPKG

1.5 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const AssertionError = require('assertion-error');
5const Description = require('./Description');
6const asMatcher = require('./utils/asMatcher');
7
8const 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
14const 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
51module.exports = assertThat;