1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 1× 1× 960× 1× 1× 1× 960× 960× 1× 270× 270× 1× 254× 29× 1× 61× 37× 25× 1× 31× 8× 23× 23× 23× 4× 19× 33× 33× 7× 4× 15× 1× 8× 4× 1× 47× 13× 1× 57× 23× 1× 16× 8× 1× 16× 8× 1× 16× 10× 1× 16× 10× 1× 215× 215× 215× 69× 2× 67× 213× 2× 1× 127× 127× 127× 127× 125× 125× 113× 127× 11× 1× 16× 4× 1× 111× 111× 121× 111× 2617× 28× 1× 1× | "use strict"; var _namespace_1 = require("./errors/_namespace"); /** * Allows checking of test outcomes * @param actualValue - the value or function under test */ function Expect(actualValue) { return new Matcher(actualValue); } exports.Expect = Expect; /** * Gives functionality to ensure the outcome of a test is as expected */ var Matcher = (function () { function Matcher(actualValue) { this._shouldMatch = true; this._actualValue = actualValue; } Object.defineProperty(Matcher.prototype, "not", { /** * Any subsequent matcher function will be looking for the opposite criteria */ get: function () { this._shouldMatch = !this._shouldMatch; return this; }, enumerable: true, configurable: true }); /** * Checks that a value is identical to another * @param expectedValue - the value that will be used to match */ Matcher.prototype.toBe = function (expectedValue) { if (expectedValue !== this._actualValue === this._shouldMatch) { throw new _namespace_1.ExactMatchError(this._actualValue, expectedValue, this._shouldMatch); } }; /** * Checks that a value is equal to another (for objects the function will check for deep equality) * @param expectedValue - the value that will be used to match */ Matcher.prototype.toEqual = function (expectedValue) { if (expectedValue != this._actualValue === this._shouldMatch) { if (typeof expectedValue !== "object" || this._checkObjectsAreDeepEqual(expectedValue, this._actualValue) !== this._shouldMatch) { throw new _namespace_1.EqualMatchError(this._actualValue, expectedValue, this._shouldMatch); } } }; Matcher.prototype._checkObjectsAreDeepEqual = function (objectA, objectB) { // if one object is an array and the other is not then they are not equal if (Array.isArray(objectA) !== Array.isArray(objectB)) { return false; } // get all the property keys for each object var objectAKeys = Object.keys(objectA); var objectBKeys = Object.keys(objectB); // if they don't have the same amount of properties then clearly not if (objectAKeys.length !== objectBKeys.length) { return false; } // check all the properties of each object for (var i = 0; i < objectAKeys.length; i++) { var objectAKey = objectAKeys[i]; // if the property values are not the same if (objectA[objectAKey] !== objectB[objectAKey]) { // and it's not an object or the objects are not equal if (typeof (objectA[objectAKey]) !== "object" || this._checkObjectsAreDeepEqual(objectA[objectAKey], objectB[objectAKey]) === false) { // then not deep equal return false; } } } // all properties match so all is good return true; }; /** * Checks that a value conforms to a regular expression * @param regex - the regular expression that the actual value should match */ Matcher.prototype.toMatch = function (regex) { if (!regex.test(this._actualValue) === this._shouldMatch) { throw new _namespace_1.RegexMatchError(this._actualValue, regex, this._shouldMatch); } }; /** * Checks that a value is not undefined */ Matcher.prototype.toBeDefined = function () { if (this._actualValue === undefined === this._shouldMatch) { throw new _namespace_1.ExactMatchError(this._actualValue, undefined, !this._shouldMatch); } }; /** * Checks that a value is null */ Matcher.prototype.toBeNull = function () { if (this._actualValue !== null === this._shouldMatch) { throw new _namespace_1.ExactMatchError(this._actualValue, null, this._shouldMatch); } }; /** * Checks that a value is equivalent to boolean true */ Matcher.prototype.toBeTruthy = function () { if ((this._actualValue && !this._shouldMatch) || (!this._actualValue && this._shouldMatch)) { throw new _namespace_1.TruthyMatchError(this._actualValue, this._shouldMatch); } }; /** * Checks that a string contains another string or an array contains a specific item * @param expectedContent - the string or array item that the value should contain */ Matcher.prototype.toContain = function (expectedContent) { if (this._actualValue.indexOf(expectedContent) === -1 === this._shouldMatch) { throw new _namespace_1.ContentsMatchError(this._actualValue, expectedContent, this._shouldMatch); } }; /** * Checks that a number is less than a given limit * @param upperLimit - the number that the number under test should be less than */ Matcher.prototype.toBeLessThan = function (upperLimit) { if (this._actualValue < upperLimit !== this._shouldMatch) { throw new _namespace_1.LessThanMatchError(this._actualValue, upperLimit, this._shouldMatch); } }; /** * Checks that a number is greater than a given limit * @param lowerLimit - the number that the number under test should be greater than */ Matcher.prototype.toBeGreaterThan = function (lowerLimit) { if (this._actualValue > lowerLimit !== this._shouldMatch) { throw new _namespace_1.GreaterThanMatchError(this._actualValue, lowerLimit, this._shouldMatch); } }; /** * Checks that a function throws an error when executed */ Matcher.prototype.toThrow = function () { var errorThrown; try { this._actualValue(); } catch (error) { if (!this._shouldMatch) { throw new _namespace_1.ErrorMatchError(error, this._shouldMatch); } errorThrown = error; } if (this._shouldMatch && errorThrown === undefined) { throw new _namespace_1.ErrorMatchError(undefined, this._shouldMatch); } }; /** * Checks that a function throws a specific error * @param errorType - the type of error that should be thrown * @param errorMessage - the message that the error should have */ Matcher.prototype.toThrowError = function (errorType, errorMessage) { var threwRightError = false; var actualError; try { this._actualValue(); } catch (error) { actualError = error; if (error instanceof errorType && error.message === errorMessage) { threwRightError = true; } } if (!threwRightError === this._shouldMatch) { throw new _namespace_1.ErrorMatchError(actualError, this._shouldMatch, errorType, errorMessage); } }; /** * Checks that a spy has been called */ Matcher.prototype.toHaveBeenCalled = function () { if (this._actualValue.calls.length === 0 === this._shouldMatch) { throw new _namespace_1.FunctionCallMatchError(this._actualValue, this._shouldMatch); } }; /** * Checks that a spy has been called with the specified arguments * @param args - a list of arguments that the spy should have been called with */ Matcher.prototype.toHaveBeenCalledWith = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i - 0] = arguments[_i]; } if (this._actualValue.calls.filter(function (call) { return call.args.filter(function (arg, index) { return arg === args[index]; }).length === args.length && call.args.length === args.length; // and the call has the same amount of arguments }).length === 0 === this._shouldMatch) { throw new _namespace_1.FunctionCallMatchError(this._actualValue, this._shouldMatch, args); } }; return Matcher; }()); exports.Matcher = Matcher; //# sourceMappingURL=expect.js.map |