UNPKG

hamjest

Version:

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

1,179 lines (1,122 loc) 1.34 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'; require("core-js/modules/es.string.starts-with.js"); require("core-js/modules/es.string.ends-with.js"); var IsEqual = require('./lib/matchers/IsEqual'); var Matcher = require('./lib/matchers/Matcher'); var SubstringMatcher = require('./lib/matchers/SubstringMatcher'); var NumberComparisonMatcher = require('./lib/matchers/NumberComparisonMatcher'); var DateComparisonMatcher = require('./lib/matchers/DateComparisonMatcher'); var Description = require('./lib/Description'); require('./lib/fixErrorJson')(); // asserts module.exports.assertThat = require('./lib/assertThat'); module.exports.promiseThat = require('./lib/promiseThat'); module.exports.fail = require('./lib/fail'); // matchers module.exports.Matcher = Matcher; module.exports.TypeSafeMatcher = require('./lib/matchers/TypeSafeMatcher'); module.exports.FeatureMatcher = require('./lib/matchers/FeatureMatcher'); module.exports.anything = require('./lib/matchers/IsAnything').anything; module.exports.strictlyEqualTo = require('./lib/matchers/IsSame').strictlyEqualTo; module.exports.is = require('./lib/matchers/Is').is; module.exports.not = require('./lib/matchers/IsNot').not; module.exports.equalTo = IsEqual.equalTo; module.exports.truthy = require('./lib/matchers/truthy'); module.exports.falsy = require('./lib/matchers/falsy'); module.exports.falsey = require('./lib/matchers/falsy'); module.exports.defined = require('./lib/matchers/IsDefined').defined; module.exports.undefined = require('./lib/matchers/IsDefined').undefined; module.exports.undef = require('./lib/matchers/IsDefined').undefined; module.exports.instanceOf = require('./lib/matchers/IsInstanceOf').instanceOf; module.exports.array = require('./lib/matchers/IsArray').array; module.exports.bool = require('./lib/matchers/IsBoolean').bool; module.exports.boolean = require('./lib/matchers/IsBoolean').bool; module.exports.date = require('./lib/matchers/IsDate').date; module.exports.func = require('./lib/matchers/IsFunction').func; module.exports.number = require('./lib/matchers/IsNumber').number; module.exports.object = require('./lib/matchers/IsObject').object; module.exports.regExp = require('./lib/matchers/IsRegExp').regExp; module.exports.string = require('./lib/matchers/IsString').string; module.exports.containsString = SubstringMatcher.containsString; module.exports.containsStrings = SubstringMatcher.containsStrings; module.exports.startsWith = SubstringMatcher.startsWith; module.exports.endsWith = SubstringMatcher.endsWith; module.exports.matchesPattern = require('./lib/matchers/IsStringMatching').matchesPattern; module.exports.matches = require('./lib/matchers/matches'); module.exports.failsToMatch = require('./lib/matchers/failsToMatch'); module.exports.hasDescription = require('./lib/matchers/hasDescription'); module.exports.lessThan = NumberComparisonMatcher.lessThan; module.exports.lessThanOrEqualTo = NumberComparisonMatcher.lessThanOrEqualTo; module.exports.greaterThan = NumberComparisonMatcher.greaterThan; module.exports.greaterThanOrEqualTo = NumberComparisonMatcher.greaterThanOrEqualTo; module.exports.inRange = require('./lib/matchers/inRange'); module.exports.after = DateComparisonMatcher.after; module.exports.afterOrEqualTo = DateComparisonMatcher.afterOrEqualTo; module.exports.before = DateComparisonMatcher.before; module.exports.beforeOrEqualTo = DateComparisonMatcher.beforeOrEqualTo; module.exports.closeTo = require('./lib/matchers/IsCloseTo').closeTo; module.exports.allOf = require('./lib/matchers/AllOf').allOf; module.exports.anyOf = require('./lib/matchers/AnyOf').anyOf; module.exports.everyItem = require('./lib/matchers/Every').everyItem; module.exports.startsWithItems = require('./lib/matchers/IsArrayStartingWith').startsWithItems; module.exports.endsWithItems = require('./lib/matchers/IsArrayEndingWith').endsWithItems; module.exports.contains = require('./lib/matchers/IsArrayContaining').contains; module.exports.containsInAnyOrder = require('./lib/matchers/IsArrayContainingInAnyOrder').containsInAnyOrder; module.exports.hasItem = require('./lib/matchers/IsArrayWithItem').hasItem; module.exports.hasItems = require('./lib/matchers/IsArrayWithItems').hasItems; module.exports.hasExactlyOneItem = require('./lib/matchers/hasExactlyOneItem'); module.exports.orderedBy = require('./lib/matchers/IsArrayOrderedBy').orderedBy; module.exports.hasSize = require('./lib/matchers/hasSize'); module.exports.isEmpty = require('./lib/matchers/isEmpty'); module.exports.empty = require('./lib/matchers/isEmpty'); module.exports.hasProperties = require('./lib/matchers/IsObjectWithProperties').hasProperties; module.exports.hasDeepProperties = require('./lib/matchers/IsObjectWithProperties').hasDeepProperties; module.exports.hasProperty = require('./lib/matchers/IsObjectWithProperties').hasProperty; module.exports.throws = require('./lib/matchers/IsFunctionThrowing').throws; module.exports.returns = require('./lib/matchers/returns'); module.exports.typedError = require('./lib/matchers/typedError'); module.exports.promise = require('./lib/matchers/IsPromise').promise; module.exports.fulfilled = require('./lib/matchers/IsFulfilled').fulfilled; module.exports.isFulfilledWith = require('./lib/matchers/IsFulfilled').isFulfilledWith; module.exports.willBe = require('./lib/matchers/IsFulfilled').isFulfilledWith; module.exports.rejected = require('./lib/matchers/IsRejected').rejected; module.exports.isRejectedWith = require('./lib/matchers/IsRejected').isRejectedWith; // Deprecated module.exports.promiseAllOf = require('./lib/matchers/AllOf').allOf; // utils module.exports.isMatcher = Matcher.isMatcher; module.exports.asMatcher = require('./lib/utils/asMatcher'); module.exports.acceptingMatcher = require('./lib/utils/acceptingMatcher'); module.exports.Description = Description; module.exports.describe = function (matcher) { return new Description().appendDescriptionOf(matcher).get(); }; },{"./lib/Description":2,"./lib/assertThat":3,"./lib/fail":4,"./lib/fixErrorJson":5,"./lib/matchers/AllOf":6,"./lib/matchers/AnyOf":7,"./lib/matchers/DateComparisonMatcher":8,"./lib/matchers/Every":9,"./lib/matchers/FeatureMatcher":10,"./lib/matchers/Is":11,"./lib/matchers/IsAnything":12,"./lib/matchers/IsArray":13,"./lib/matchers/IsArrayContaining":14,"./lib/matchers/IsArrayContainingInAnyOrder":15,"./lib/matchers/IsArrayEndingWith":16,"./lib/matchers/IsArrayOrderedBy":17,"./lib/matchers/IsArrayStartingWith":18,"./lib/matchers/IsArrayWithItem":19,"./lib/matchers/IsArrayWithItems":20,"./lib/matchers/IsBoolean":21,"./lib/matchers/IsCloseTo":22,"./lib/matchers/IsDate":23,"./lib/matchers/IsDefined":24,"./lib/matchers/IsEqual":25,"./lib/matchers/IsFulfilled":26,"./lib/matchers/IsFunction":27,"./lib/matchers/IsFunctionThrowing":28,"./lib/matchers/IsInstanceOf":29,"./lib/matchers/IsNot":30,"./lib/matchers/IsNumber":31,"./lib/matchers/IsObject":32,"./lib/matchers/IsObjectWithProperties":33,"./lib/matchers/IsPromise":34,"./lib/matchers/IsRegExp":35,"./lib/matchers/IsRejected":36,"./lib/matchers/IsSame":37,"./lib/matchers/IsString":38,"./lib/matchers/IsStringMatching":39,"./lib/matchers/Matcher":40,"./lib/matchers/NumberComparisonMatcher":41,"./lib/matchers/SubstringMatcher":42,"./lib/matchers/TypeSafeMatcher":43,"./lib/matchers/failsToMatch":44,"./lib/matchers/falsy":45,"./lib/matchers/hasDescription":46,"./lib/matchers/hasExactlyOneItem":47,"./lib/matchers/hasSize":48,"./lib/matchers/inRange":49,"./lib/matchers/isEmpty":50,"./lib/matchers/matches":51,"./lib/matchers/returns":53,"./lib/matchers/truthy":54,"./lib/matchers/typedError":55,"./lib/promiseThat":56,"./lib/utils/acceptingMatcher":57,"./lib/utils/asMatcher":58,"core-js/modules/es.string.ends-with.js":253,"core-js/modules/es.string.starts-with.js":256}],2:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.symbol.js"); require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.symbol.iterator.js"); require("core-js/modules/es.array.iterator.js"); require("core-js/modules/es.string.iterator.js"); require("core-js/modules/web.dom-collections.iterator.js"); require("core-js/modules/es.regexp.exec.js"); require("core-js/modules/es.string.replace.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/es.promise.js"); require("core-js/modules/es.function.name.js"); require("core-js/modules/es.regexp.to-string.js"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _forEach = require('lodash/forEach'); var _isArray = require('lodash/isArray'); var _isFunction = require('lodash/isFunction'); var _isNull = require('lodash/isNull'); var _isNumber = require('lodash/isNumber'); var _isRegExp = require('lodash/isRegExp'); var _isString = require('lodash/isString'); var _isUndefined = require('lodash/isUndefined'); var _padEnd = require('lodash/padEnd'); 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)) { var decrementIndentation = function decrementIndentation() { _this.indentation -= 1; }; // Promise.finally based on https://www.npmjs.com/package/finally-polyfill return Promise.resolve(result).then(function (result) { return Promise.resolve(decrementIndentation()).then(result); }, function (error) { return Promise.resolve(decrementIndentation()).then(function () { throw error; }); }); } 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, indentLists) { var _this2 = this; 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)) { if (indentLists && value.length > 1) { this.indented(function () { return _this2.appendList('[\n', ',\n', '', value); }).append('\n]'); } else { 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 _this3 = this; this.append('{'); var first = true; _forEach(value, function (innerValue, key) { if (!first) { _this3.append(', '); } first = false; _this3.append(key).append(': '); _this3.appendValue(innerValue); }, this); this.append('}'); }, appendList: function appendList(start, separator, end, list) { var _this4 = this; this.append(start); _forEach(list, function (value, index) { if (index !== 0) { _this4.append(separator); } _this4.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; },{"core-js/modules/es.array.iterator.js":228,"core-js/modules/es.function.name.js":233,"core-js/modules/es.object.to-string.js":241,"core-js/modules/es.promise.js":245,"core-js/modules/es.regexp.exec.js":251,"core-js/modules/es.regexp.to-string.js":252,"core-js/modules/es.string.iterator.js":254,"core-js/modules/es.string.replace.js":255,"core-js/modules/es.symbol.description.js":258,"core-js/modules/es.symbol.iterator.js":260,"core-js/modules/es.symbol.js":261,"core-js/modules/web.dom-collections.iterator.js":265,"lodash/forEach":438,"lodash/isArray":444,"lodash/isFunction":450,"lodash/isNull":453,"lodash/isNumber":454,"lodash/isRegExp":457,"lodash/isString":459,"lodash/isUndefined":462,"lodash/padEnd":468}],3:[function(require,module,exports){ (function (global){(function (){ 'use strict'; require("core-js/modules/es.symbol.js"); require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/es.symbol.iterator.js"); require("core-js/modules/es.array.iterator.js"); require("core-js/modules/es.string.iterator.js"); require("core-js/modules/web.dom-collections.iterator.js"); require("core-js/modules/es.array.slice.js"); require("core-js/modules/es.regexp.to-string.js"); require("core-js/modules/es.function.name.js"); require("core-js/modules/es.array.from.js"); require("core-js/modules/es.regexp.exec.js"); require("core-js/modules/es.array.concat.js"); 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" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) 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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var _isFunction = require('lodash/isFunction'); 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)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"./Description":2,"./utils/asMatcher":58,"assertion-error":61,"core-js/modules/es.array.concat.js":226,"core-js/modules/es.array.from.js":227,"core-js/modules/es.array.iterator.js":228,"core-js/modules/es.array.slice.js":230,"core-js/modules/es.function.name.js":233,"core-js/modules/es.object.to-string.js":241,"core-js/modules/es.regexp.exec.js":251,"core-js/modules/es.regexp.to-string.js":252,"core-js/modules/es.string.iterator.js":254,"core-js/modules/es.symbol.description.js":258,"core-js/modules/es.symbol.iterator.js":260,"core-js/modules/es.symbol.js":261,"core-js/modules/web.dom-collections.iterator.js":265,"lodash/isFunction":450}],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":61}],5:[function(require,module,exports){ 'use strict'; require("core-js/modules/web.url.to-json.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/web.dom-collections.for-each.js"); require("core-js/modules/es.object.get-own-property-names.js"); 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.object.get-own-property-names.js":237,"core-js/modules/es.object.to-string.js":241,"core-js/modules/web.dom-collections.for-each.js":264,"core-js/modules/web.url.to-json.js":266}],6:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _every = require('lodash/every'); var _map = require('lodash/map'); var _mapValues = require('lodash/mapValues'); 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":40,"./promiseAgnostic":52,"lodash/create":433,"lodash/every":435,"lodash/map":465,"lodash/mapValues":466}],7:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _map = require('lodash/map'); var _some = require('lodash/some'); var Matcher = require('./Matcher'); var promiseAgnostic = require('./promiseAgnostic'); var asMatcher = require('../utils/asMatcher'); function AnyOf(matchers) { 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; },{"../utils/asMatcher":58,"./Matcher":40,"./promiseAgnostic":52,"lodash/create":433,"lodash/map":465,"lodash/some":473}],8:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); 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); } }); } DateComparisonMatcher.after = function (threshold) { return new DateComparisonMatcher(threshold, 'after', function (actual) { return actual > threshold; }); }; DateComparisonMatcher.afterOrEqualTo = function (threshold) { return new DateComparisonMatcher(threshold, 'after or equal to', function (actual) { return actual >= threshold; }); }; DateComparisonMatcher.before = function (threshold) { return new DateComparisonMatcher(threshold, 'before', function (actual) { return actual < threshold; }); }; DateComparisonMatcher.beforeOrEqualTo = function (threshold) { return new DateComparisonMatcher(threshold, 'before or equal to', function (actual) { return actual <= threshold; }); }; module.exports = DateComparisonMatcher; },{"../assertThat":3,"./Is":11,"./IsDate":23,"lodash/create":433}],9:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _every = require('lodash/every'); var _isArray = require('lodash/isArray'); var _isObject = require('lodash/isObject'); var _map = require('lodash/map'); var _mapValues = require('lodash/mapValues'); var TypeSafeMatcher = require('./TypeSafeMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var asMatcher = require('../utils/asMatcher'); function Every(valueOrMatcher) { var matcher = asMatcher(valueOrMatcher); 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/asMatcher":58,"./TypeSafeMatcher":43,"./promiseAgnostic":52,"lodash/create":433,"lodash/every":435,"lodash/isArray":444,"lodash/isObject":455,"lodash/map":465,"lodash/mapValues":466}],10:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); 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":58,"./Matcher":40,"./promiseAgnostic":52,"lodash/create":433}],11:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var Matcher = require('./Matcher'); var asMatcher = require('../utils/asMatcher'); function Is(valueOrMatcher) { var innerMatcher = asMatcher(valueOrMatcher); 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/asMatcher":58,"./Matcher":40,"lodash/create":433}],12:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); 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":40,"lodash/create":433}],13:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _isArray = require('lodash/isArray'); 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":43,"lodash/create":433,"lodash/isArray":444}],14:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.slice.js"); var _create = require('lodash/create'); var _every = require('lodash/every'); var _map = require('lodash/map'); 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":58,"./IsArray":13,"./promiseAgnostic":52,"core-js/modules/es.array.slice.js":230,"lodash/create":433,"lodash/every":435,"lodash/map":465}],15:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.splice.js"); var _clone = require('lodash/clone'); var _create = require('lodash/create'); var _forEach = require('lodash/forEach'); var _map = require('lodash/map'); 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, true).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, true); } } }); }; IsArrayContainingInAnyOrder.containsInAnyOrder = function () { return new IsArrayContainingInAnyOrder(arguments); }; module.exports = IsArrayContainingInAnyOrder; },{"../utils/asMatcher":58,"./IsArray":13,"core-js/modules/es.array.splice.js":231,"lodash/clone":431,"lodash/create":433,"lodash/forEach":438,"lodash/map":465}],16:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.slice.js"); var _create = require('lodash/create'); var _every = require('lodash/every'); var _map = require('lodash/map'); var IsArray = require('./IsArray'); var asMatcher = require('../utils/asMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var IsArrayEndingWith = function IsArrayEndingWith(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[actual.length - matchers.length + index]); }); return promiseAgnostic.matchesAggregate(results, _every); }, describeTo: function describeTo(description) { description.appendList('[…, ', ', ', ']', matchers); }, describeMismatchSafely: function describeMismatchSafely(actual, description) { var results = _map(matchers, function (matcher, index) { var actualIndex = actual.length - matchers.length + index; if (actualIndex >= 0) { return matcher.matches(actual[actualIndex]); } }); var first = true; return promiseAgnostic.describeMismatchAggregate(results, function (result, index) { var actualIndex = actual.length - matchers.length + index; if (result || actualIndex < 0) { return; } if (!first) { description.append('\n'); } first = false; description.append('item ').append(actualIndex).append(': '); return matchers[index].describeMismatch(actual[actualIndex], description); }, function () { if (!first) { description.append('\n'); } if (actual.length < matchers.length) { description.indented(function () { return description.appendList('missing:\n', ',\n', '', matchers.slice(0, matchers.length - actual.length)); }); } }); } }); }; IsArrayEndingWith.endsWithItems = function () { return new IsArrayEndingWith(arguments); }; module.exports = IsArrayEndingWith; },{"../utils/asMatcher":58,"./IsArray":13,"./promiseAgnostic":52,"core-js/modules/es.array.slice.js":230,"lodash/create":433,"lodash/every":435,"lodash/map":465}],17:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.function.name.js"); var _create = require('lodash/create'); var _reduce = require('lodash/reduce'); 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":13,"core-js/modules/es.function.name.js":233,"lodash/create":433,"lodash/reduce":470}],18:[function(require,module,exports){ 'use strict'; require("core-js/modules/es.array.slice.js"); var _create = require('lodash/create'); var _every = require('lodash/every'); var _map = require('lodash/map'); var IsArray = require('./IsArray'); var asMatcher = require('../utils/asMatcher'); var promiseAgnostic = require('./promiseAgnostic'); var IsArrayStartingWith = function IsArrayStartingWith(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(matchers, function (matcher, index) { if (index < actual.length) { return matcher.matches(actual[index]); } }); var first = true; return promiseAgnostic.describeMismatchAggregate(results, function (result, index) { if (result || index >= actual.length) { 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('missing:\n', ',\n', '', matchers.slice(actual.length)); }); } }); } }); }; IsArrayStartingWith.startsWithItems = function () { return new IsArrayStartingWith(arguments); }; module.exports = IsArrayStartingWith; },{"../utils/asMatcher":58,"./IsArray":13,"./promiseAgnostic":52,"core-js/modules/es.array.slice.js":230,"lodash/create":433,"lodash/every":435,"lodash/map":465}],19:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _map = require('lodash/map'); var _some = require('lodash/some'); var IsArray = require('./IsArray'); var promiseAgnostic = require('./promiseAgnostic'); var asMatcher = require('../utils/asMatcher'); function IsArrayWithItem(valueOrMatcher) { var matcher = asMatcher(valueOrMatcher); 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/asMatcher":58,"./IsArray":13,"./promiseAgnostic":52,"lodash/create":433,"lodash/map":465,"lodash/some":473}],20:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _forEach = require('lodash/forEach'); var _map = require('lodash/map'); 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":58,"./AllOf":6,"./IsArray":13,"./IsArrayWithItem":19,"lodash/create":433,"lodash/forEach":438,"lodash/map":465}],21:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _isBoolean = require('lodash/isBoolean'); 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":43,"lodash/create":433,"lodash/isBoolean":446}],22:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); 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":11,"./IsNumber":31,"lodash/create":433}],23:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _isDate = require('lodash/isDate'); 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":43,"lodash/create":433,"lodash/isDate":448}],24:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _isUndefined = require('lodash/isUndefined'); 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":30,"./Matcher":40,"lodash/create":433,"lodash/isUndefined":462}],25:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); var _isEqual = require('lodash/isEqual'); 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":40,"lodash/create":433,"lodash/isEqual":449}],26:[function(require,module,exports){ 'use strict'; var _create = require('lodash/create'); 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); ret