Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
'use strict';
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
7
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
8
9
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11
var _has = require('has');
12
13
var _has2 = _interopRequireDefault(_has);
14
15
var _tmatch = require('tmatch');
16
17
var _tmatch2 = _interopRequireDefault(_tmatch);
18
19
var _assert = require('./assert');
20
21
var _assert2 = _interopRequireDefault(_assert);
22
23
var _SpyUtils = require('./SpyUtils');
24
25
var _TestUtils = require('./TestUtils');
26
27
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31
/**
32
* An Expectation is a wrapper around an assertion that allows it to be written
33
* in a more natural style, without the need to remember the order of arguments.
34
* This helps prevent you from making mistakes when writing tests.
35
*/
36
37
var Expectation = function () {
38
function Expectation(actual) {
39
_classCallCheck(this, Expectation);
40
41
this.actual = actual;
42
43
if ((0, _TestUtils.isFunction)(actual)) {
44
this.context = null;
45
this.args = [];
46
}
47
}
48
49
_createClass(Expectation, [{
50
key: 'toExist',
51
value: function toExist(message) {
52
(0, _assert2.default)(this.actual, message || 'Expected %s to exist', this.actual);
53
54
return this;
55
}
56
}, {
57
key: 'toNotExist',
58
value: function toNotExist(message) {
59
(0, _assert2.default)(!this.actual, message || 'Expected %s to not exist', this.actual);
60
61
return this;
62
}
63
}, {
64
key: 'toBe',
65
value: function toBe(value, message) {
66
(0, _assert2.default)(this.actual === value, message || 'Expected %s to be %s', this.actual, value);
67
68
return this;
69
}
70
}, {
71
key: 'toNotBe',
72
value: function toNotBe(value, message) {
73
(0, _assert2.default)(this.actual !== value, message || 'Expected %s to not be %s', this.actual, value);
74
75
return this;
76
}
77
}, {
78
key: 'toEqual',
79
value: function toEqual(value, message) {
80
try {
81
(0, _assert2.default)((0, _TestUtils.isEqual)(this.actual, value), message || 'Expected %s to equal %s', this.actual, value);
82
} catch (error) {
83
// These attributes are consumed by Mocha to produce a diff output.
84
error.actual = this.actual;
85
error.expected = value;
86
error.showDiff = true;
87
throw error;
88
}
89
90
return this;
91
}
92
}, {
93
key: 'toNotEqual',
94
value: function toNotEqual(value, message) {
95
(0, _assert2.default)(!(0, _TestUtils.isEqual)(this.actual, value), message || 'Expected %s to not equal %s', this.actual, value);
96
97
return this;
98
}
99
}, {
100
key: 'toThrow',
101
value: function toThrow(value, message) {
102
(0, _assert2.default)((0, _TestUtils.isFunction)(this.actual), 'The "actual" argument in expect(actual).toThrow() must be a function, %s was given', this.actual);
103
104
(0, _assert2.default)((0, _TestUtils.functionThrows)(this.actual, this.context, this.args, value), message || 'Expected %s to throw %s', this.actual, value || 'an error');
105
106
return this;
107
}
108
}, {
109
key: 'toNotThrow',
110
value: function toNotThrow(value, message) {
111
(0, _assert2.default)((0, _TestUtils.isFunction)(this.actual), 'The "actual" argument in expect(actual).toNotThrow() must be a function, %s was given', this.actual);
112
113
(0, _assert2.default)(!(0, _TestUtils.functionThrows)(this.actual, this.context, this.args, value), message || 'Expected %s to not throw %s', this.actual, value || 'an error');
114
115
return this;
116
}
117
}, {
118
key: 'toBeA',
119
value: function toBeA(value, message) {
120
(0, _assert2.default)((0, _TestUtils.isFunction)(value) || typeof value === 'string', 'The "value" argument in toBeA(value) must be a function or a string');
121
122
(0, _assert2.default)((0, _TestUtils.isA)(this.actual, value), message || 'Expected %s to be a %s', this.actual, value);
123
124
return this;
125
}
126
}, {
127
key: 'toNotBeA',
128
value: function toNotBeA(value, message) {
129
(0, _assert2.default)((0, _TestUtils.isFunction)(value) || typeof value === 'string', 'The "value" argument in toNotBeA(value) must be a function or a string');
130
131
(0, _assert2.default)(!(0, _TestUtils.isA)(this.actual, value), message || 'Expected %s to not be a %s', this.actual, value);
132
133
return this;
134
}
135
}, {
136
key: 'toMatch',
137
value: function toMatch(pattern, message) {
138
(0, _assert2.default)((0, _tmatch2.default)(this.actual, pattern), message || 'Expected %s to match %s', this.actual, pattern);
139
140
return this;
141
}
142
}, {
143
key: 'toNotMatch',
144
value: function toNotMatch(pattern, message) {
145
(0, _assert2.default)(!(0, _tmatch2.default)(this.actual, pattern), message || 'Expected %s to not match %s', this.actual, pattern);
146
147
return this;
148
}
149
}, {
150
key: 'toBeLessThan',
151
value: function toBeLessThan(value, message) {
152
(0, _assert2.default)(typeof this.actual === 'number', 'The "actual" argument in expect(actual).toBeLessThan() must be a number');
153
154
(0, _assert2.default)(typeof value === 'number', 'The "value" argument in toBeLessThan(value) must be a number');
155
156
(0, _assert2.default)(this.actual < value, message || 'Expected %s to be less than %s', this.actual, value);
157
158
return this;
159
}
160
}, {
161
key: 'toBeLessThanOrEqualTo',
162
value: function toBeLessThanOrEqualTo(value, message) {
163
(0, _assert2.default)(typeof this.actual === 'number', 'The "actual" argument in expect(actual).toBeLessThanOrEqualTo() must be a number');
164
165
(0, _assert2.default)(typeof value === 'number', 'The "value" argument in toBeLessThanOrEqualTo(value) must be a number');
166
167
(0, _assert2.default)(this.actual <= value, message || 'Expected %s to be less than or equal to %s', this.actual, value);
168
169
return this;
170
}
171
}, {
172
key: 'toBeGreaterThan',
173
value: function toBeGreaterThan(value, message) {
174
(0, _assert2.default)(typeof this.actual === 'number', 'The "actual" argument in expect(actual).toBeGreaterThan() must be a number');
175
176
(0, _assert2.default)(typeof value === 'number', 'The "value" argument in toBeGreaterThan(value) must be a number');
177
178
(0, _assert2.default)(this.actual > value, message || 'Expected %s to be greater than %s', this.actual, value);
179
180
return this;
181
}
182
}, {
183
key: 'toBeGreaterThanOrEqualTo',
184
value: function toBeGreaterThanOrEqualTo(value, message) {
185
(0, _assert2.default)(typeof this.actual === 'number', 'The "actual" argument in expect(actual).toBeGreaterThanOrEqualTo() must be a number');
186
187
(0, _assert2.default)(typeof value === 'number', 'The "value" argument in toBeGreaterThanOrEqualTo(value) must be a number');
188
189
(0, _assert2.default)(this.actual >= value, message || 'Expected %s to be greater than or equal to %s', this.actual, value);
190
191
return this;
192
}
193
}, {
194
key: 'toInclude',
195
value: function toInclude(value, compareValues, message) {
196
if (typeof compareValues === 'string') {
197
message = compareValues;
198
compareValues = null;
199
}
200
201
if (compareValues == null) compareValues = _TestUtils.isEqual;
202
203
var contains = false;
204
205
if ((0, _TestUtils.isArray)(this.actual)) {
206
contains = (0, _TestUtils.arrayContains)(this.actual, value, compareValues);
207
} else if ((0, _TestUtils.isObject)(this.actual)) {
208
contains = (0, _TestUtils.objectContains)(this.actual, value, compareValues);
209
} else if (typeof this.actual === 'string') {
210
contains = (0, _TestUtils.stringContains)(this.actual, value);
211
} else {
212
(0, _assert2.default)(false, 'The "actual" argument in expect(actual).toInclude() must be an array, object, or a string');
213
}
214
215
(0, _assert2.default)(contains, message || 'Expected %s to include %s', this.actual, value);
216
217
return this;
218
}
219
}, {
220
key: 'toExclude',
221
value: function toExclude(value, compareValues, message) {
222
if (typeof compareValues === 'string') {
223
message = compareValues;
224
compareValues = null;
225
}
226
227
if (compareValues == null) compareValues = _TestUtils.isEqual;
228
229
var contains = false;
230
231
if ((0, _TestUtils.isArray)(this.actual)) {
232
contains = (0, _TestUtils.arrayContains)(this.actual, value, compareValues);
233
} else if ((0, _TestUtils.isObject)(this.actual)) {
234
contains = (0, _TestUtils.objectContains)(this.actual, value, compareValues);
235
} else if (typeof this.actual === 'string') {
236
contains = (0, _TestUtils.stringContains)(this.actual, value);
237
} else {
238
(0, _assert2.default)(false, 'The "actual" argument in expect(actual).toExclude() must be an array, object, or a string');
239
}
240
241
(0, _assert2.default)(!contains, message || 'Expected %s to exclude %s', this.actual, value);
242
243
return this;
244
}
245
}, {
246
key: 'toIncludeKeys',
247
value: function toIncludeKeys(keys, comparator, message) {
248
var _this = this;
249
250
if (typeof comparator === 'string') {
251
message = comparator;
252
comparator = null;
253
}
254
255
if (comparator == null) comparator = _has2.default;
256
257
(0, _assert2.default)(_typeof(this.actual) === 'object', 'The "actual" argument in expect(actual).toIncludeKeys() must be an object, not %s', this.actual);
258
259
(0, _assert2.default)((0, _TestUtils.isArray)(keys), 'The "keys" argument in expect(actual).toIncludeKeys(keys) must be an array, not %s', keys);
260
261
var contains = keys.every(function (key) {
262
return comparator(_this.actual, key);
263
});
264
265
(0, _assert2.default)(contains, message || 'Expected %s to include key(s) %s', this.actual, keys.join(', '));
266
267
return this;
268
}
269
}, {
270
key: 'toIncludeKey',
271
value: function toIncludeKey(key) {
272
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
273
args[_key - 1] = arguments[_key];
274
}
275
276
return this.toIncludeKeys.apply(this, [[key]].concat(args));
277
}
278
}, {
279
key: 'toExcludeKeys',
280
value: function toExcludeKeys(keys, comparator, message) {
281
var _this2 = this;
282
283
if (typeof comparator === 'string') {
284
message = comparator;
285
comparator = null;
286
}
287
288
if (comparator == null) comparator = _has2.default;
289
290
(0, _assert2.default)(_typeof(this.actual) === 'object', 'The "actual" argument in expect(actual).toExcludeKeys() must be an object, not %s', this.actual);
291
292
(0, _assert2.default)((0, _TestUtils.isArray)(keys), 'The "keys" argument in expect(actual).toIncludeKeys(keys) must be an array, not %s', keys);
293
294
var contains = keys.every(function (key) {
295
return comparator(_this2.actual, key);
296
});
297
298
(0, _assert2.default)(!contains, message || 'Expected %s to exclude key(s) %s', this.actual, keys.join(', '));
299
300
return this;
301
}
302
}, {
303
key: 'toExcludeKey',
304
value: function toExcludeKey(key) {
305
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
306
args[_key2 - 1] = arguments[_key2];
307
}
308
309
return this.toExcludeKeys.apply(this, [[key]].concat(args));
310
}
311
}, {
312
key: 'toHaveBeenCalled',
313
value: function toHaveBeenCalled(message) {
314
var spy = this.actual;
315
316
(0, _assert2.default)((0, _SpyUtils.isSpy)(spy), 'The "actual" argument in expect(actual).toHaveBeenCalled() must be a spy');
317
318
(0, _assert2.default)(spy.calls.length > 0, message || 'spy was not called');
319
320
return this;
321
}
322
}, {
323
key: 'toHaveBeenCalledWith',
324
value: function toHaveBeenCalledWith() {
325
for (var _len3 = arguments.length, expectedArgs = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
326
expectedArgs[_key3] = arguments[_key3];
327
}
328
329
var spy = this.actual;
330
331
(0, _assert2.default)((0, _SpyUtils.isSpy)(spy), 'The "actual" argument in expect(actual).toHaveBeenCalledWith() must be a spy');
332
333
(0, _assert2.default)(spy.calls.some(function (call) {
334
return (0, _TestUtils.isEqual)(call.arguments, expectedArgs);
335
}), 'spy was never called with %s', expectedArgs);
336
337
return this;
338
}
339
}, {
340
key: 'toNotHaveBeenCalled',
341
value: function toNotHaveBeenCalled(message) {
342
var spy = this.actual;
343
344
(0, _assert2.default)((0, _SpyUtils.isSpy)(spy), 'The "actual" argument in expect(actual).toNotHaveBeenCalled() must be a spy');
345
346
(0, _assert2.default)(spy.calls.length === 0, message || 'spy was not supposed to be called');
347
348
return this;
349
}
350
}]);
351
352
return Expectation;
353
}();
354
355
var deprecate = function deprecate(fn, message) {
356
var alreadyWarned = false;
357
358
return function () {
359
if (!alreadyWarned) {
360
alreadyWarned = true;
361
console.warn(message);
362
}
363
364
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
365
args[_key4] = arguments[_key4];
366
}
367
368
return fn.apply(this, args);
369
};
370
};
371
372
Expectation.prototype.withContext = deprecate(function (context) {
373
(0, _assert2.default)((0, _TestUtils.isFunction)(this.actual), 'The "actual" argument in expect(actual).withContext() must be a function');
374
375
this.context = context;
376
377
return this;
378
}, '\nwithContext is deprecated; use a closure instead.\n\n expect(fn).withContext(context).toThrow()\n\nbecomes\n\n expect(() => fn.call(context)).toThrow()\n');
379
380
Expectation.prototype.withArgs = deprecate(function () {
381
var _args;
382
383
(0, _assert2.default)((0, _TestUtils.isFunction)(this.actual), 'The "actual" argument in expect(actual).withArgs() must be a function');
384
385
if (arguments.length) this.args = (_args = this.args).concat.apply(_args, arguments);
386
387
return this;
388
}, '\nwithArgs is deprecated; use a closure instead.\n\n expect(fn).withArgs(a, b, c).toThrow()\n\nbecomes\n\n expect(() => fn(a, b, c)).toThrow()\n');
389
390
var aliases = {
391
toBeAn: 'toBeA',
392
toNotBeAn: 'toNotBeA',
393
toBeTruthy: 'toExist',
394
toBeFalsy: 'toNotExist',
395
toBeFewerThan: 'toBeLessThan',
396
toBeMoreThan: 'toBeGreaterThan',
397
toContain: 'toInclude',
398
toNotContain: 'toExclude',
399
toNotInclude: 'toExclude',
400
toContainKeys: 'toIncludeKeys',
401
toNotContainKeys: 'toExcludeKeys',
402
toNotIncludeKeys: 'toExcludeKeys',
403
toContainKey: 'toIncludeKey',
404
toNotContainKey: 'toExcludeKey',
405
toNotIncludeKey: 'toExcludeKey'
406
};
407
408
for (var alias in aliases) {
409
if (aliases.hasOwnProperty(alias)) Expectation.prototype[alias] = Expectation.prototype[aliases[alias]];
410
}exports.default = Expectation;
411