UNPKG

hamjest

Version:

A library of composable matchers for defining meaningful and readable assertions in JavaScript.

1,562 lines (1,187 loc) 2.59 MB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.hamjest = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ 'use strict'; module.exports = require('./lib/hamjest'); },{"./lib/hamjest":6}],2:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.symbol"); require("core-js/modules/es.symbol.description"); require("core-js/modules/es.symbol.iterator"); require("core-js/modules/es.array.for-each"); require("core-js/modules/es.array.iterator"); require("core-js/modules/es.function.name"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.promise"); require("core-js/modules/es.promise.finally"); require("core-js/modules/es.regexp.exec"); require("core-js/modules/es.regexp.to-string"); require("core-js/modules/es.string.iterator"); require("core-js/modules/es.string.pad-end"); require("core-js/modules/es.string.replace"); require("core-js/modules/web.dom-collections.for-each"); require("core-js/modules/web.dom-collections.iterator"); function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var _ = require('lodash'); var Bluebird = require('bluebird'); function asSelfDescribing(value) { if (!value || !_.isFunction(value.describeTo)) { return { describeTo: function describeTo(description) { description.appendValue(value); } }; } else { return value; } } function makeNaNReadable(__key, value) { return typeof value === 'number' && isNaN(value) ? '<NaN>' : value; } function Description() { var value = ''; return { useJsonForObjects: true, indentation: 0, append: function append(text) { if (this.indentation) { text = ('' + text).replace('\n', _.padEnd('\n', this.indentation + 1, '\t')); } try { value += text; } catch (e) { value += '[ ' + _typeof(text) + ']'; } return this; }, indented: function indented(describingfn) { var _this = this; this.indentation += 1; var result = describingfn(); if (result && _.isFunction(result.then)) { return Bluebird.resolve(result).finally(function () { _this.indentation -= 1; }); } else { this.indentation -= 1; return result; } }, appendDescriptionOf: function appendDescriptionOf(selfDescribing) { if (selfDescribing && _.isFunction(selfDescribing.describeTo)) { selfDescribing.describeTo(this); } else { this.appendValue(selfDescribing); } return this; }, appendValue: function appendValue(value) { if (_.isUndefined(value)) { this.append('undefined'); } else if (_.isNull(value)) { this.append('null'); } else if (_.isString(value)) { this.append('"'); this.append(value); this.append('"'); } else if (_.isNumber(value)) { this.append('<'); this.append(value); this.append('>'); } else if (_.isArray(value)) { this.appendList('[', ', ', ']', value); } else if (isDomNode(value)) { this.append('DOM node ').appendValue(_.isFunction(value.html) ? value.html() : value.outerHTML); } else if (_.isFunction(value)) { this.append('Function' + (value.name ? ' ' + value.name : '')); } else if (_.isRegExp(value)) { this.append(value.toString()); } else if (this.useJsonForObjects) { try { this.append(JSON.stringify(value, makeNaNReadable)); } catch (e) { var oldJsonFlag = this.useJsonForObjects; this.useJsonForObjects = false; this.appendNonJson(value); this.useJsonForObjects = oldJsonFlag; } } else { this.append(value); } return this; }, appendNonJson: function appendNonJson(value) { var _this2 = this; this.append('{'); var first = true; _.forEach(value, function (innerValue, key) { if (!first) { _this2.append(', '); } first = false; _this2.append(key).append(': '); _this2.appendValue(innerValue); }, this); this.append('}'); }, appendList: function appendList(start, separator, end, list) { var _this3 = this; this.append(start); _.forEach(list, function (value, index) { if (index !== 0) { _this3.append(separator); } _this3.appendDescriptionOf(asSelfDescribing(value)); }, this); this.append(end); return this; }, get: function get() { return value; } }; function isDomNode(value) { if (!value) { return false; } return _.isFunction(value.appendChild) && _.isFunction(value.isEqualNode) && !_.isUndefined(value.outerHTML) || _.isFunction(value.html) && _.isFunction(value.text); } } module.exports = Description; },{"bluebird":61,"core-js/modules/es.array.for-each":179,"core-js/modules/es.array.iterator":182,"core-js/modules/es.function.name":189,"core-js/modules/es.object.to-string":193,"core-js/modules/es.promise":195,"core-js/modules/es.promise.finally":194,"core-js/modules/es.regexp.exec":198,"core-js/modules/es.regexp.to-string":199,"core-js/modules/es.string.iterator":201,"core-js/modules/es.string.pad-end":202,"core-js/modules/es.string.replace":203,"core-js/modules/es.symbol":208,"core-js/modules/es.symbol.description":206,"core-js/modules/es.symbol.iterator":207,"core-js/modules/web.dom-collections.for-each":209,"core-js/modules/web.dom-collections.iterator":210,"lodash":212}],3:[function(require,module,exports){ (function (global){ 'use strict'; require("core-js/modules/es.symbol"); require("core-js/modules/es.symbol.description"); require("core-js/modules/es.symbol.iterator"); require("core-js/modules/es.array.concat"); require("core-js/modules/es.array.from"); require("core-js/modules/es.array.iterator"); require("core-js/modules/es.array.slice"); require("core-js/modules/es.function.name"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.regexp.to-string"); require("core-js/modules/es.string.iterator"); require("core-js/modules/web.dom-collections.iterator"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var _ = require('lodash'); var AssertionError = require('assertion-error'); var Description = require('./Description'); var asMatcher = require('./utils/asMatcher'); var processArgs = function processArgs(args) { var hasThreeArgs = args.length === 3; var _ref = hasThreeArgs ? args : [''].concat(_toConsumableArray(args)), _ref2 = _slicedToArray(_ref, 3), reason = _ref2[0], actual = _ref2[1], maybeMatcher = _ref2[2]; return { reason: reason, actual: actual, matcher: asMatcher(maybeMatcher) }; }; var assertThat = function assertThat() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _processArgs = processArgs(args), reason = _processArgs.reason, matcher = _processArgs.matcher, actual = _processArgs.actual; var matches = matcher.matches(actual); if (matches && _.isFunction(matches.then)) { throw new AssertionError('Matcher returned a promise instead of a boolean - use promiseThat for promising matchers!', {}, assertThat); } if (!matches) { var description = new Description(); description.append(reason).append('\nExpected: ').appendDescriptionOf(matcher).append('\n but: '); matcher.describeMismatch(actual, description); var errorProperties = {}; if (_.isFunction(matcher.getExpectedForDiff) && _.isFunction(matcher.formatActualForDiff)) { errorProperties = { showDiff: true, expected: matcher.getExpectedForDiff(), actual: matcher.formatActualForDiff(actual) }; } throw new AssertionError(description.get(), errorProperties, assertThat); } else { if (global && global.expect) { var expectation = global.expect(); if (expectation && expectation.nothing) { expectation.nothing(); } } } }; module.exports = assertThat; }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"./Description":2,"./utils/asMatcher":57,"assertion-error":60,"core-js/modules/es.array.concat":176,"core-js/modules/es.array.from":180,"core-js/modules/es.array.iterator":182,"core-js/modules/es.array.slice":186,"core-js/modules/es.function.name":189,"core-js/modules/es.object.to-string":193,"core-js/modules/es.regexp.to-string":199,"core-js/modules/es.string.iterator":201,"core-js/modules/es.symbol":208,"core-js/modules/es.symbol.description":206,"core-js/modules/es.symbol.iterator":207,"core-js/modules/web.dom-collections.iterator":210,"lodash":212}],4:[function(require,module,exports){ 'use strict'; var AssertionError = require('assertion-error'); function fail(reason) { throw new AssertionError(reason, {}, fail); } module.exports = fail; },{"assertion-error":60}],5:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.for-each"); require("core-js/modules/es.object.get-own-property-names"); require("core-js/modules/web.dom-collections.for-each"); require("core-js/modules/web.url.to-json"); module.exports = function () { if (!Error.prototype.toJSON) { Object.defineProperty(Error.prototype, 'toJSON', { value: function value() { var alt = {}; Object.getOwnPropertyNames(this).forEach(function (key) { alt[key] = this[key]; }, this); return alt; }, configurable: true, writable: true }); } }; },{"core-js/modules/es.array.for-each":179,"core-js/modules/es.object.get-own-property-names":190,"core-js/modules/web.dom-collections.for-each":209,"core-js/modules/web.url.to-json":211}],6:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.string.ends-with"); require("core-js/modules/es.string.starts-with"); var _ = require('lodash'); var IsEqual = require('./matchers/IsEqual'); var Matcher = require('./matchers/Matcher'); var SubstringMatcher = require('./matchers/SubstringMatcher'); var NumberComparisonMatcher = require('./matchers/NumberComparisonMatcher'); var DateComparisonMatcher = require('./matchers/DateComparisonMatcher'); var Description = require('./Description'); require('./fixErrorJson')(); var asserts = { assertThat: require('./assertThat'), promiseThat: require('./promiseThat'), fail: require('./fail') }; var matchers = { Matcher: Matcher, TypeSafeMatcher: require('./matchers/TypeSafeMatcher'), FeatureMatcher: require('./matchers/FeatureMatcher'), anything: require('./matchers/IsAnything').anything, strictlyEqualTo: require('./matchers/IsSame').strictlyEqualTo, is: require('./matchers/Is').is, not: require('./matchers/IsNot').not, equalTo: IsEqual.equalTo, truthy: require('./matchers/truthy'), falsy: require('./matchers/falsy'), falsey: require('./matchers/falsy'), defined: require('./matchers/IsDefined').defined, undefined: require('./matchers/IsDefined').undefined, undef: require('./matchers/IsDefined').undefined, instanceOf: require('./matchers/IsInstanceOf').instanceOf, array: require('./matchers/IsArray').array, bool: require('./matchers/IsBoolean').bool, boolean: require('./matchers/IsBoolean').bool, date: require('./matchers/IsDate').date, func: require('./matchers/IsFunction').func, number: require('./matchers/IsNumber').number, object: require('./matchers/IsObject').object, regExp: require('./matchers/IsRegExp').regExp, string: require('./matchers/IsString').string, containsString: SubstringMatcher.containsString, containsStrings: SubstringMatcher.containsStrings, startsWith: SubstringMatcher.startsWith, endsWith: SubstringMatcher.endsWith, matchesPattern: require('./matchers/IsStringMatching').matchesPattern, matches: require('./matchers/matches'), failsToMatch: require('./matchers/failsToMatch'), hasDescription: require('./matchers/hasDescription'), lessThan: NumberComparisonMatcher.lessThan, lessThanOrEqualTo: NumberComparisonMatcher.lessThanOrEqualTo, greaterThan: NumberComparisonMatcher.greaterThan, greaterThanOrEqualTo: NumberComparisonMatcher.greaterThanOrEqualTo, inRange: require('./matchers/inRange'), after: DateComparisonMatcher.after, afterOrEqualTo: DateComparisonMatcher.afterOrEqualTo, before: DateComparisonMatcher.before, beforeOrEqualTo: DateComparisonMatcher.beforeOrEqualTo, closeTo: require('./matchers/IsCloseTo').closeTo, allOf: require('./matchers/AllOf').allOf, anyOf: require('./matchers/AnyOf').anyOf, everyItem: require('./matchers/Every').everyItem, hasItem: require('./matchers/IsArrayWithItem').hasItem, hasItems: require('./matchers/IsArrayWithItems').hasItems, hasExactlyOneItem: require('./matchers/hasExactlyOneItem'), contains: require('./matchers/IsArrayContaining').contains, containsInAnyOrder: require('./matchers/IsArrayContainingInAnyOrder').containsInAnyOrder, orderedBy: require('./matchers/IsArrayOrderedBy').orderedBy, hasSize: require('./matchers/hasSize'), isEmpty: require('./matchers/isEmpty'), empty: require('./matchers/isEmpty'), hasProperties: require('./matchers/IsObjectWithProperties').hasProperties, hasDeepProperties: require('./matchers/IsObjectWithProperties').hasDeepProperties, hasProperty: require('./matchers/IsObjectWithProperties').hasProperty, throws: require('./matchers/IsFunctionThrowing').throws, returns: require('./matchers/returns'), typedError: require('./matchers/typedError'), promise: require('./matchers/IsPromise').promise, fulfilled: require('./matchers/IsFulfilled').fulfilled, isFulfilledWith: require('./matchers/IsFulfilled').isFulfilledWith, willBe: require('./matchers/IsFulfilled').isFulfilledWith, rejected: require('./matchers/IsRejected').rejected, isRejectedWith: require('./matchers/IsRejected').isRejectedWith, // Deprecated promiseAllOf: require('./matchers/AllOf').allOf }; var utils = { isMatcher: Matcher.isMatcher, asMatcher: require('./utils/asMatcher'), acceptingMatcher: require('./utils/acceptingMatcher'), Description: Description, describe: function describe(matcher) { return new Description().appendDescriptionOf(matcher).get(); } }; var hamjest = {}; _.extend(hamjest, asserts, matchers, utils); module.exports = hamjest; },{"./Description":2,"./assertThat":3,"./fail":4,"./fixErrorJson":5,"./matchers/AllOf":7,"./matchers/AnyOf":8,"./matchers/DateComparisonMatcher":9,"./matchers/Every":10,"./matchers/FeatureMatcher":11,"./matchers/Is":12,"./matchers/IsAnything":13,"./matchers/IsArray":14,"./matchers/IsArrayContaining":15,"./matchers/IsArrayContainingInAnyOrder":16,"./matchers/IsArrayOrderedBy":17,"./matchers/IsArrayWithItem":18,"./matchers/IsArrayWithItems":19,"./matchers/IsBoolean":20,"./matchers/IsCloseTo":21,"./matchers/IsDate":22,"./matchers/IsDefined":23,"./matchers/IsEqual":24,"./matchers/IsFulfilled":25,"./matchers/IsFunction":26,"./matchers/IsFunctionThrowing":27,"./matchers/IsInstanceOf":28,"./matchers/IsNot":29,"./matchers/IsNumber":30,"./matchers/IsObject":31,"./matchers/IsObjectWithProperties":32,"./matchers/IsPromise":33,"./matchers/IsRegExp":34,"./matchers/IsRejected":35,"./matchers/IsSame":36,"./matchers/IsString":37,"./matchers/IsStringMatching":38,"./matchers/Matcher":39,"./matchers/NumberComparisonMatcher":40,"./matchers/SubstringMatcher":41,"./matchers/TypeSafeMatcher":42,"./matchers/failsToMatch":43,"./matchers/falsy":44,"./matchers/hasDescription":45,"./matchers/hasExactlyOneItem":46,"./matchers/hasSize":47,"./matchers/inRange":48,"./matchers/isEmpty":49,"./matchers/matches":50,"./matchers/returns":52,"./matchers/truthy":53,"./matchers/typedError":54,"./promiseThat":55,"./utils/acceptingMatcher":56,"./utils/asMatcher":57,"core-js/modules/es.string.ends-with":200,"core-js/modules/es.string.starts-with":205,"lodash":212}],7:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.every"); require("core-js/modules/es.array.map"); var _ = require('lodash'); var Matcher = require('./Matcher'); var promiseAgnostic = require('./promiseAgnostic'); function AllOf(matchers) { return _.create(new Matcher(), { matches: function matches(actual) { var results = _.map(matchers, function (matcher) { return matcher.matches(actual); }); return promiseAgnostic.matchesAggregate(results, _.every); }, describeTo: function describeTo(description) { description.appendList('(', ' and ', ')', matchers); }, describeMismatch: function describeMismatch(actual, description) { var results = _.mapValues(matchers, function (matcher) { return matcher.matches(actual); }); var first = true; return promiseAgnostic.describeMismatchAggregate(results, function (result, key) { if (result) { return; } var matcher = matchers[key]; if (!first) { description.append('\n'); } first = false; description.appendDescriptionOf(matcher).append(': '); return description.indented(function () { return matcher.describeMismatch(actual, description); }); }); } }); } AllOf.allOf = function () { return new AllOf(arguments); }; module.exports = AllOf; },{"./Matcher":39,"./promiseAgnostic":51,"core-js/modules/es.array.every":177,"core-js/modules/es.array.map":183,"lodash":212}],8:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.map"); require("core-js/modules/es.array.some"); var _ = require('lodash'); var Matcher = require('./Matcher'); var promiseAgnostic = require('./promiseAgnostic'); function AnyOf(matchers) { var __ = require('../..'); return _.create(new Matcher(), { matches: function matches(actual) { var results = _.map(matchers, function (matcher) { return __.asMatcher(matcher).matches(actual); }); return promiseAgnostic.matchesAggregate(results, _.some); }, describeTo: function describeTo(description) { description.appendList('(', ' or ', ')', matchers); } }); } AnyOf.anyOf = function () { return new AnyOf(arguments); }; module.exports = AnyOf; },{"../..":1,"./Matcher":39,"./promiseAgnostic":51,"core-js/modules/es.array.map":183,"core-js/modules/es.array.some":187,"lodash":212}],9:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var IsDate = require('./IsDate'); var assertThat = require('../assertThat'); var is = require('./Is').is; var date = require('./IsDate').date; function DateComparisonMatcher(threshold, relation, matchesNumber) { assertThat(threshold, is(date())); return _.create(new IsDate(), { matchesSafely: function matchesSafely(actual) { return matchesNumber.call(this, actual); }, describeTo: function describeTo(description) { description.append('a date ').append(relation).append(' ').appendValue(threshold); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { description.append('was ').appendValue(actual); } }); } _.extend(DateComparisonMatcher, { after: function after(threshold) { return new DateComparisonMatcher(threshold, 'after', function (actual) { return actual > threshold; }); }, afterOrEqualTo: function afterOrEqualTo(threshold) { return new DateComparisonMatcher(threshold, 'after or equal to', function (actual) { return actual >= threshold; }); }, before: function before(threshold) { return new DateComparisonMatcher(threshold, 'before', function (actual) { return actual < threshold; }); }, beforeOrEqualTo: function beforeOrEqualTo(threshold) { return new DateComparisonMatcher(threshold, 'before or equal to', function (actual) { return actual <= threshold; }); } }); module.exports = DateComparisonMatcher; },{"../assertThat":3,"./Is":12,"./IsDate":22,"lodash":212}],10:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.every"); require("core-js/modules/es.array.map"); var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); var acceptingMatcher = require('../utils/acceptingMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var Every = acceptingMatcher(function (matcher) { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isArray(actual) || _.isObject(actual); }, matchesSafely: function matchesSafely(actual) { var results = _.map(actual, function (value) { return matcher.matches(value); }); return promiseAgnostic.matchesAggregate(results, _.every); }, describeTo: function describeTo(description) { description.append('every item is ').appendDescriptionOf(matcher); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { var results; if (_.isArray(actual)) { results = _.map(actual, function (value) { return matcher.matches(value); }); } else { results = _.mapValues(actual, function (value) { return matcher.matches(value); }); } return promiseAgnostic.describeMismatchAggregate(results, function (result, key) { if (result) { return; } description.append('\nitem ').appendValue(key).append(' '); return description.indented(function () { return matcher.describeMismatch(actual[key], description); }); }); } }); }); Every.everyItem = function (valueOrMatcher) { return new Every(valueOrMatcher); }; module.exports = Every; },{"../utils/acceptingMatcher":56,"./TypeSafeMatcher":42,"./promiseAgnostic":51,"core-js/modules/es.array.every":177,"core-js/modules/es.array.map":183,"lodash":212}],11:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); var asMatcher = require('../utils/asMatcher'); var promiseAgnostic = require('./promiseAgnostic'); function FeatureMatcher(valueOrMatcher, featureDescription, featureName, featureFunction) { var matcher = asMatcher(valueOrMatcher); featureFunction = featureFunction || function (item) { return item[featureName]; }; return _.create(new Matcher(), { matches: function matches(actual) { var featureValue = featureFunction(actual); return matcher.matches(featureValue); }, describeTo: function describeTo(description) { description.append(featureDescription).append(' ').appendDescriptionOf(matcher); }, describeMismatch: function describeMismatch(actual, description) { var featureValue = featureFunction(actual); return promiseAgnostic.describeMismatch(matcher.matches(featureValue), function () { description.append(featureName).append(' '); return matcher.describeMismatch(featureValue, description); }, function () { description.append('\nfor ').appendValue(actual); }); } }); } module.exports = FeatureMatcher; },{"../utils/asMatcher":57,"./Matcher":39,"./promiseAgnostic":51,"lodash":212}],12:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); var acceptingMatcher = require('../utils/acceptingMatcher'); var Is = acceptingMatcher(function (innerMatcher) { return _.create(new Matcher(), { matches: function matches(actualValue) { return innerMatcher.matches(actualValue); }, describeTo: function describeTo(description) { description.append('is ').appendDescriptionOf(innerMatcher); }, describeMismatch: function describeMismatch(value, description) { return innerMatcher.describeMismatch(value, description); }, getExpectedForDiff: innerMatcher.getExpectedForDiff, formatActualForDiff: innerMatcher.formatActualForDiff }); }); Is.is = function (innerMatcher) { return new Is(innerMatcher); }; module.exports = Is; },{"../utils/acceptingMatcher":56,"./Matcher":39,"lodash":212}],13:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); function IsAnything() { return _.create(new Matcher(), { matches: function matches() { return true; }, describeTo: function describeTo(description) { description.append('anything'); } }); } IsAnything.anything = function () { return new IsAnything(); }; module.exports = IsAnything; },{"./Matcher":39,"lodash":212}],14:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsArray() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isArray(actual); }, describeTo: function describeTo(description) { description.append('an array'); } }); } IsArray.array = function () { return new IsArray(); }; module.exports = IsArray; },{"./TypeSafeMatcher":42,"lodash":212}],15:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.every"); require("core-js/modules/es.array.map"); require("core-js/modules/es.array.slice"); var _ = require('lodash'); var IsArray = require('./IsArray'); var asMatcher = require('../utils/asMatcher'); var promiseAgnostic = require('./promiseAgnostic'); function IsArrayContaining(itemsOrMatchers) { var matchers = _.map(itemsOrMatchers, asMatcher); return _.create(new IsArray(), { matchesSafely: function matchesSafely(actual) { if (actual.length !== matchers.length) { return false; } var results = _.map(matchers, function (matcher, index) { return matcher.matches(actual[index]); }); return promiseAgnostic.matchesAggregate(results, _.every); }, describeTo: function describeTo(description) { description.appendList('[', ', ', ']', matchers); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { var results = _.map(actual, function (value, index) { if (matchers.length > index) { return matchers[index].matches(value); } }); var first = true; return promiseAgnostic.describeMismatchAggregate(results, function (result, index) { if (result || matchers.length <= index || actual.length <= index) { return; } if (!first) { description.append('\n'); } first = false; description.append('item ').append(index).append(': '); return matchers[index].describeMismatch(actual[index], description); }, function () { if (!first) { description.append('\n'); } if (actual.length > matchers.length) { description.indented(function () { return description.appendList('not matched:\n', ',\n', '', actual.slice(matchers.length)); }); } else if (actual.length < matchers.length) { description.indented(function () { return description.appendList('missing:\n', ',\n', '', matchers.slice(actual.length)); }); } }); } }); } IsArrayContaining.contains = function () { return new IsArrayContaining(arguments); }; module.exports = IsArrayContaining; },{"../utils/asMatcher":57,"./IsArray":14,"./promiseAgnostic":51,"core-js/modules/es.array.every":177,"core-js/modules/es.array.map":183,"core-js/modules/es.array.slice":186,"lodash":212}],16:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.for-each"); require("core-js/modules/es.array.map"); require("core-js/modules/es.array.splice"); require("core-js/modules/web.dom-collections.for-each"); var _ = require('lodash'); var IsArray = require('./IsArray'); var asMatcher = require('../utils/asMatcher'); // TODO: Make promise agnostic function ConsumingMatcher(matchers) { return _.create({}, { unmatchedMatchers: _.clone(matchers), matches: function matches(actual) { var _this = this; var matched = false; _.forEach(this.unmatchedMatchers, function (matcher, index) { if (matcher.matches(actual)) { matched = true; _this.unmatchedMatchers.splice(index, 1); return false; } }, this); return matched; } }); } var IsArrayContainingInAnyOrder = function IsArrayContainingInAnyOrder(itemsOrMatchers) { var matchers = _.map(itemsOrMatchers, asMatcher); return _.create(new IsArray(), { matchesSafely: function matchesSafely(actual) { if (actual.length !== matchers.length) { return false; } var matcher = new ConsumingMatcher(matchers); _.forEach(actual, function (item) { if (!matcher.matches(item)) { return false; } }); return matcher.unmatchedMatchers.length === 0; }, describeTo: function describeTo(description) { description.appendList('[', ', ', ']', matchers).append(' in any order'); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { var matcher = new ConsumingMatcher(matchers); var unmatchedItems = []; _.forEach(actual, function (item) { if (!matcher.matches(item)) { unmatchedItems.push(item); } }); if (matcher.unmatchedMatchers.length !== 0) { description.append('no item in ').appendValue(actual).indented(function () { return description.appendList(' matches:\n', ',\n', '', matcher.unmatchedMatchers); }); } else if (unmatchedItems.length !== 0) { description.indented(function () { return description.appendList('not matched:\n', ',\n', '', unmatchedItems); }).append('\nfrom ').appendValue(actual); } } }); }; IsArrayContainingInAnyOrder.containsInAnyOrder = function () { return new IsArrayContainingInAnyOrder(arguments); }; module.exports = IsArrayContainingInAnyOrder; },{"../utils/asMatcher":57,"./IsArray":14,"core-js/modules/es.array.for-each":179,"core-js/modules/es.array.map":183,"core-js/modules/es.array.splice":188,"core-js/modules/web.dom-collections.for-each":209,"lodash":212}],17:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.reduce"); require("core-js/modules/es.function.name"); var _ = require('lodash'); var IsArray = require('./IsArray'); function IsArrayOrderedBy(comp, compDescription) { compDescription = compDescription || comp.name; return _.create(new IsArray(), { matchesSafely: function matchesSafely(actual) { var correctOrder = true; _.reduce(actual, function (previous, element) { if (!comp(previous, element)) { correctOrder = false; } return element; }); return correctOrder; }, describeTo: function describeTo(description) { description.append('an array ordered ').append(compDescription); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { var correctOrder = true; var firstMismatch; _.reduce(actual, function (previous, element, index) { if (!comp(previous, element) && correctOrder) { correctOrder = false; firstMismatch = { a: previous, aIndex: index - 1, b: element, bIndex: index }; } return element; }); description.appendValue(firstMismatch.a).append(' at index ').append(firstMismatch.aIndex).append(' and ').appendValue(firstMismatch.b).append(' at index ').append(firstMismatch.bIndex).append(' are not in order'); } }); } IsArrayOrderedBy.orderedBy = function (comp, orderName) { return new IsArrayOrderedBy(comp, orderName); }; module.exports = IsArrayOrderedBy; },{"./IsArray":14,"core-js/modules/es.array.reduce":185,"core-js/modules/es.function.name":189,"lodash":212}],18:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.map"); require("core-js/modules/es.array.some"); var _ = require('lodash'); var IsArray = require('./IsArray'); var acceptingMatcher = require('../utils/acceptingMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var IsArrayWithItem = acceptingMatcher(function (matcher) { return _.create(new IsArray(), { matchesSafely: function matchesSafely(actual) { var results = _.map(actual, function (value) { return matcher.matches(value); }); return promiseAgnostic.matchesAggregate(results, _.some); }, describeTo: function describeTo(description) { description.append('an array containing ').appendDescriptionOf(matcher); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { if (actual.length === 0) { description.append('was empty'); return; } var results = _.map(actual, function (value) { return matcher.matches(value); }); return promiseAgnostic.describeMismatchAggregate(results, function (__result, index) { description.append('\n'); description.append('item ').append(index).append(': '); return description.indented(function () { return matcher.describeMismatch(actual[index], description); }); }); } }); }); IsArrayWithItem.hasItem = function (valueOrMatcher) { return new IsArrayWithItem(valueOrMatcher); }; module.exports = IsArrayWithItem; },{"../utils/acceptingMatcher":56,"./IsArray":14,"./promiseAgnostic":51,"core-js/modules/es.array.map":183,"core-js/modules/es.array.some":187,"lodash":212}],19:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.for-each"); require("core-js/modules/es.array.map"); require("core-js/modules/web.dom-collections.for-each"); var _ = require('lodash'); var IsArray = require('./IsArray'); var hasItem = require('./IsArrayWithItem').hasItem; var AllOf = require('./AllOf'); var asMatcher = require('../utils/asMatcher'); var IsArrayWithItems = function IsArrayWithItems(items) { var innerMatcher = new AllOf(_.map(items, hasItem)); return _.create(new IsArray(), { matchesSafely: function matchesSafely(actual) { return innerMatcher.matches(actual); }, describeTo: function describeTo(description) { description.append('an array containing '); var first = true; _.forEach(items, function (item) { if (!first) { description.append(', '); } first = false; asMatcher(item).describeTo(description); }); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { if (actual.length === 0) { description.append('was empty'); return; } innerMatcher.describeMismatch(actual, description); } }); }; IsArrayWithItems.hasItems = function () { return new IsArrayWithItems(arguments); }; module.exports = IsArrayWithItems; },{"../utils/asMatcher":57,"./AllOf":7,"./IsArray":14,"./IsArrayWithItem":18,"core-js/modules/es.array.for-each":179,"core-js/modules/es.array.map":183,"core-js/modules/web.dom-collections.for-each":209,"lodash":212}],20:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsBoolean() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isBoolean(actual); }, describeTo: function describeTo(description) { description.append('a boolean'); } }); } IsBoolean.bool = function () { return new IsBoolean(); }; module.exports = IsBoolean; },{"./TypeSafeMatcher":42,"lodash":212}],21:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var IsNumber = require('./IsNumber'); var assertThat = require('../assertThat'); var is = require('./Is').is; var number = require('./IsNumber').number; function IsCloseTo(threshold, delta) { assertThat(threshold, is(number())); assertThat(delta, is(number())); function getDelta(actual) { return Math.abs(actual - threshold); } return _.create(new IsNumber(), { matchesSafely: function matchesSafely(actual) { return getDelta(actual) <= delta; }, describeTo: function describeTo(description) { description.append('a number within ').appendValue(delta).append(' of ').appendValue(threshold); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { description.appendValue(actual).append(' differed by ').appendValue(getDelta(actual)); } }); } IsCloseTo.closeTo = function (threshold, delta) { return new IsCloseTo(threshold, delta); }; module.exports = IsCloseTo; },{"../assertThat":3,"./Is":12,"./IsNumber":30,"lodash":212}],22:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsDate() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isDate(actual); }, describeTo: function describeTo(description) { description.append('a date'); } }); } IsDate.date = function () { return new IsDate(); }; module.exports = IsDate; },{"./TypeSafeMatcher":42,"lodash":212}],23:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); var not = require('./IsNot').not; function IsDefined() { return _.create(new Matcher(), { matches: function matches(actual) { return !_.isUndefined(actual); }, describeTo: function describeTo(description) { description.append('defined'); } }); } IsDefined.defined = function () { return new IsDefined(); }; IsDefined.undefined = function () { return not(IsDefined.defined()); }; module.exports = IsDefined; },{"./IsNot":29,"./Matcher":39,"lodash":212}],24:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); function IsEqual(expectedValue) { return _.create(new Matcher(), { matches: function matches(actualValue) { return _.isEqual(expectedValue, actualValue); }, describeTo: function describeTo(description) { description.appendValue(expectedValue); }, getExpectedForDiff: function getExpectedForDiff() { return expectedValue; }, formatActualForDiff: function formatActualForDiff(actual) { return actual; } }); } IsEqual.equalTo = function (operand) { return new IsEqual(operand); }; module.exports = IsEqual; },{"./Matcher":39,"lodash":212}],25:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var IsPromise = require('./IsPromise'); var asMatcher = require('../utils/asMatcher'); var anything = require('./IsAnything').anything; function IsFulfilled(valueOrMatcher) { var anyValue = arguments.length === 0; var valueMatcher = anyValue ? anything() : asMatcher(valueOrMatcher); return _.create(new IsPromise(), { matchesSafely: function matchesSafely(actual) { return actual.then(function (value) { return valueMatcher.matches(value); }, function () { return false; }); }, describeTo: function describeTo(description) { if (anyValue) { description.append('a fulfilled promise'); } else { description.append('a promise fulfilled with '); valueMatcher.describeTo(description); } }, describeMismatchSafely: function describeMismatchSafely(actual, description) { return actual.then(function (actualValue) { description.append('fulfillment value: '); return valueMatcher.describeMismatch(actualValue, description); }, function (error) { description.append('was rejected with ').appendValue(error); }); } }); } IsFulfilled.fulfilled = function (operand) { if (arguments.length === 0) { return new IsFulfilled(); } else { return new IsFulfilled(operand); } }; IsFulfilled.isFulfilledWith = IsFulfilled.fulfilled; module.exports = IsFulfilled; },{"../utils/asMatcher":57,"./IsAnything":13,"./IsPromise":33,"lodash":212}],26:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsFunction() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isFunction(actual); }, describeTo: function describeTo(description) { description.append('a function'); } }); } IsFunction.func = function () { return new IsFunction(); }; module.exports = IsFunction; },{"./TypeSafeMatcher":42,"lodash":212}],27:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var IsFunction = require('./IsFunction'); var asMatcher = require('../utils/asMatcher'); var anything = require('./IsAnything').anything; function IsFunctionThrowing(valueOrMatcher) { var anyValue = arguments.length === 0; var exceptionMatcher = anyValue ? anything() : asMatcher(valueOrMatcher); return _.create(new IsFunction(), { matchesSafely: function matchesSafely(throwingFunction) { try { throwingFunction(); return false; } catch (e) { return exceptionMatcher.matches(e); } }, describeTo: function describeTo(description) { description.append('a function throwing '); exceptionMatcher.describeTo(description); }, describeMismatch: function describeMismatch(throwingFunction, description) { try { throwingFunction(); description.appendValue(throwingFunction).append(' did not throw anything'); } catch (e) { description.append('thrown object: '); return exceptionMatcher.describeMismatch(e, description); } } }); } IsFunctionThrowing.throws = function (operand) { if (arguments.length === 0) { return new IsFunctionThrowing(); } else { return new IsFunctionThrowing(operand); } }; module.exports = IsFunctionThrowing; },{"../utils/asMatcher":57,"./IsAnything":13,"./IsFunction":26,"lodash":212}],28:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); var assertThat = require('../assertThat'); var is = require('./Is').is; var func = require('./IsFunction').func; var getType = require('../utils/getType'); var getTypeName = require('../utils/getTypeName'); function IsInstanceOf(expectedType) { assertThat(expectedType, is(func())); return _.create(new Matcher(), { matches: function matches(actual) { return actual instanceof expectedType; }, describeTo: function describeTo(description) { description.append('an instance of ').append(getTypeName(expectedType)); }, describeMismatch: function describeMismatch(actual, description) { if (_.isUndefined(actual)) { description.append('was ').appendValue(actual); return; } description.appendValue(actual).append(' is a ').append(getType(actual)); } }); } IsInstanceOf.instanceOf = function (operand) { return new IsInstanceOf(operand); }; module.exports = IsInstanceOf; },{"../assertThat":3,"../utils/getType":58,"../utils/getTypeName":59,"./Is":12,"./IsFunction":26,"./Matcher":39,"lodash":212}],29:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var Matcher = require('./Matcher'); var acceptingMatcher = require('../utils/acceptingMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var IsNot = acceptingMatcher(function (innerMatcher) { return _.create(new Matcher(), { matches: function matches(actual) { return promiseAgnostic.matches(innerMatcher.matches(actual), function (result) { return !result; }); }, describeTo: function describeTo(description) { description.append('not ').appendDescriptionOf(innerMatcher); }, describeMismatch: function describeMismatch(value, description) { description.append('was ').appendValue(value); } }); }); IsNot.not = function (innerMatcher) { return new IsNot(innerMatcher); }; module.exports = IsNot; },{"../utils/acceptingMatcher":56,"./Matcher":39,"./promiseAgnostic":51,"lodash":212}],30:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsNumber() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isNumber(actual); }, describeTo: function describeTo(description) { description.append('a number'); } }); } IsNumber.number = function () { return new IsNumber(); }; module.exports = IsNumber; },{"./TypeSafeMatcher":42,"lodash":212}],31:[function(require,module,exports){ 'use strict'; var _ = require('lodash'); var TypeSafeMatcher = require('./TypeSafeMatcher'); function IsObject() { return _.create(new TypeSafeMatcher(), { isExpectedType: function isExpectedType(actual) { return _.isObject(actual); }, describeTo: function describeTo(description) { description.append('an object'); } }); } IsObject.object = function () { return new IsObject(); }; module.exports = IsObject; },{"./TypeSafeMatcher":42,"lodash":212}],32:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.every"); require("core-js/modules/es.array.for-each"); require("core-js/modules/es.array.reduce-right"); require("core-js/modules/es.object.get-prototype-of"); require("core-js/modules/es.regexp.exec"); require("core-js/modules/es.string.split"); require("core-js/modules/web.dom-collections.for-each"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var _ = require('lodash'); var IsObject = require('./IsObject'); var isMatcher = require('./Matcher').isMatcher; var asMatcher = require('../utils/asMatcher'); var defined = require('./IsDefined').defined; var promiseAgnostic = require('./promiseAgnostic'); function IsObjectWithProperties(properties) { var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var propertyMatchers = _.mapValues(properties,