chai 1.8.1 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/karma.conf.js CHANGED
@@ -1,20 +1,19 @@
1
1
  module.exports = function(config) {
2
2
  config.set({
3
- basePath: ''
4
- , frameworks: [ 'mocha' ]
3
+ frameworks: [ 'mocha' ]
5
4
  , files: [
6
5
  'build/build.js'
7
6
  , 'test/bootstrap/karma.js'
8
7
  , 'test/*.js'
9
8
  ]
10
- , exclude: []
11
9
  , reporters: [ 'progress' ]
12
- , port: 9876
13
10
  , colors: true
14
11
  , logLevel: config.LOG_INFO
15
12
  , autoWatch: false
16
13
  , browsers: [ 'PhantomJS' ]
17
- , captureTimeout: 60000
14
+ , browserDisconnectTimeout: 10000
15
+ , browserDisconnectTolerance: 2
16
+ , browserNoActivityTimeout: 20000
18
17
  , singleRun: true
19
18
  });
20
19
 
@@ -1,10 +1,13 @@
1
1
  /*!
2
2
  * chai
3
3
  * http://chaijs.com
4
- * Copyright(c) 2011-2013 Jake Luer <jake@alogicalparadox.com>
4
+ * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
5
5
  * MIT Licensed
6
6
  */
7
7
 
8
+ var config = require('./config');
9
+ var NOOP = function() { };
10
+
8
11
  module.exports = function (_chai, util) {
9
12
  /*!
10
13
  * Module dependencies.
@@ -33,33 +36,27 @@ module.exports = function (_chai, util) {
33
36
  flag(this, 'message', msg);
34
37
  }
35
38
 
36
- /*!
37
- * ### Assertion.includeStack
38
- *
39
- * User configurable property, influences whether stack trace
40
- * is included in Assertion error message. Default of false
41
- * suppresses stack trace in the error message
42
- *
43
- * Assertion.includeStack = true; // enable stack on error
44
- *
45
- * @api public
46
- */
47
-
48
- Assertion.includeStack = false;
49
-
50
- /*!
51
- * ### Assertion.showDiff
52
- *
53
- * User configurable property, influences whether or not
54
- * the `showDiff` flag should be included in the thrown
55
- * AssertionErrors. `false` will always be `false`; `true`
56
- * will be true when the assertion has requested a diff
57
- * be shown.
58
- *
59
- * @api public
60
- */
39
+ Object.defineProperty(Assertion, 'includeStack', {
40
+ get: function() {
41
+ console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
42
+ return config.includeStack;
43
+ },
44
+ set: function(value) {
45
+ console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
46
+ config.includeStack = value;
47
+ }
48
+ });
61
49
 
62
- Assertion.showDiff = true;
50
+ Object.defineProperty(Assertion, 'showDiff', {
51
+ get: function() {
52
+ console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
53
+ return config.showDiff;
54
+ },
55
+ set: function(value) {
56
+ console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
57
+ config.showDiff = value;
58
+ }
59
+ });
63
60
 
64
61
  Assertion.addProperty = function (name, fn) {
65
62
  util.addProperty(this.prototype, name, fn);
@@ -73,6 +70,10 @@ module.exports = function (_chai, util) {
73
70
  util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
74
71
  };
75
72
 
73
+ Assertion.addChainableNoop = function(name, fn) {
74
+ util.addChainableMethod(this.prototype, name, NOOP, fn);
75
+ };
76
+
76
77
  Assertion.overwriteProperty = function (name, fn) {
77
78
  util.overwriteProperty(this.prototype, name, fn);
78
79
  };
@@ -81,6 +82,10 @@ module.exports = function (_chai, util) {
81
82
  util.overwriteMethod(this.prototype, name, fn);
82
83
  };
83
84
 
85
+ Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) {
86
+ util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
87
+ };
88
+
84
89
  /*!
85
90
  * ### .assert(expression, message, negateMessage, expected, actual)
86
91
  *
@@ -88,8 +93,8 @@ module.exports = function (_chai, util) {
88
93
  *
89
94
  * @name assert
90
95
  * @param {Philosophical} expression to be tested
91
- * @param {String} message to display if fails
92
- * @param {String} negatedMessage to display if negated expression fails
96
+ * @param {String or Function} message or function that returns message to display if fails
97
+ * @param {String or Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
93
98
  * @param {Mixed} expected value (remember to check for negation)
94
99
  * @param {Mixed} actual (optional) will default to `this.obj`
95
100
  * @api private
@@ -98,7 +103,7 @@ module.exports = function (_chai, util) {
98
103
  Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
99
104
  var ok = util.test(this, arguments);
100
105
  if (true !== showDiff) showDiff = false;
101
- if (true !== Assertion.showDiff) showDiff = false;
106
+ if (true !== config.showDiff) showDiff = false;
102
107
 
103
108
  if (!ok) {
104
109
  var msg = util.getMessage(this, arguments)
@@ -107,7 +112,7 @@ module.exports = function (_chai, util) {
107
112
  actual: actual
108
113
  , expected: expected
109
114
  , showDiff: showDiff
110
- }, (Assertion.includeStack) ? this.assert : flag(this, 'ssfi'));
115
+ }, (config.includeStack) ? this.assert : flag(this, 'ssfi'));
111
116
  }
112
117
  };
113
118
 
@@ -0,0 +1,50 @@
1
+ module.exports = {
2
+
3
+ /**
4
+ * ### config.includeStack
5
+ *
6
+ * User configurable property, influences whether stack trace
7
+ * is included in Assertion error message. Default of false
8
+ * suppresses stack trace in the error message.
9
+ *
10
+ * chai.config.includeStack = true; // enable stack on error
11
+ *
12
+ * @param {Boolean}
13
+ * @api public
14
+ */
15
+
16
+ includeStack: false,
17
+
18
+ /**
19
+ * ### config.showDiff
20
+ *
21
+ * User configurable property, influences whether or not
22
+ * the `showDiff` flag should be included in the thrown
23
+ * AssertionErrors. `false` will always be `false`; `true`
24
+ * will be true when the assertion has requested a diff
25
+ * be shown.
26
+ *
27
+ * @param {Boolean}
28
+ * @api public
29
+ */
30
+
31
+ showDiff: true,
32
+
33
+ /**
34
+ * ### config.truncateThreshold
35
+ *
36
+ * User configurable property, sets length threshold for actual and
37
+ * expected values in assertion errors. If this threshold is exceeded,
38
+ * the value is truncated.
39
+ *
40
+ * Set it to zero if you want to disable truncating altogether.
41
+ *
42
+ * chai.config.truncateThreshold = 0; // disable truncating
43
+ *
44
+ * @param {Number}
45
+ * @api public
46
+ */
47
+
48
+ truncateThreshold: 40
49
+
50
+ };
@@ -1,7 +1,7 @@
1
1
  /*!
2
2
  * chai
3
3
  * http://chaijs.com
4
- * Copyright(c) 2011-2013 Jake Luer <jake@alogicalparadox.com>
4
+ * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>
5
5
  * MIT Licensed
6
6
  */
7
7
 
@@ -13,9 +13,9 @@ module.exports = function (chai, _) {
13
13
  /**
14
14
  * ### Language Chains
15
15
  *
16
- * The following are provide as chainable getters to
16
+ * The following are provided as chainable getters to
17
17
  * improve the readability of your assertions. They
18
- * do not provide an testing capability unless they
18
+ * do not provide testing capabilities unless they
19
19
  * have been overwritten by a plugin.
20
20
  *
21
21
  * **Chains**
@@ -26,6 +26,7 @@ module.exports = function (chai, _) {
26
26
  * - is
27
27
  * - that
28
28
  * - and
29
+ * - has
29
30
  * - have
30
31
  * - with
31
32
  * - at
@@ -37,7 +38,7 @@ module.exports = function (chai, _) {
37
38
  */
38
39
 
39
40
  [ 'to', 'be', 'been'
40
- , 'is', 'and', 'have'
41
+ , 'is', 'and', 'has', 'have'
41
42
  , 'with', 'that', 'at'
42
43
  , 'of', 'same' ].forEach(function (chain) {
43
44
  Assertion.addProperty(chain, function () {
@@ -145,9 +146,28 @@ module.exports = function (chai, _) {
145
146
 
146
147
  function include (val, msg) {
147
148
  if (msg) flag(this, 'message', msg);
148
- var obj = flag(this, 'object')
149
+ var obj = flag(this, 'object');
150
+ var expected = false;
151
+ if (_.type(obj) === 'array' && _.type(val) === 'object') {
152
+ for (var i in obj) {
153
+ if (_.eql(obj[i], val)) {
154
+ expected = true;
155
+ break;
156
+ }
157
+ }
158
+ } else if (_.type(val) === 'object') {
159
+ if (!flag(this, 'negate')) {
160
+ for (var k in val) new Assertion(obj).property(k, val[k]);
161
+ return;
162
+ }
163
+ var subset = {}
164
+ for (var k in val) subset[k] = obj[k]
165
+ expected = _.eql(subset, val);
166
+ } else {
167
+ expected = obj && ~obj.indexOf(val)
168
+ }
149
169
  this.assert(
150
- ~obj.indexOf(val)
170
+ expected
151
171
  , 'expected #{this} to include ' + _.inspect(val)
152
172
  , 'expected #{this} to not include ' + _.inspect(val));
153
173
  }
@@ -166,11 +186,15 @@ module.exports = function (chai, _) {
166
186
  * expect(undefined).to.not.be.ok;
167
187
  * expect(null).to.not.be.ok;
168
188
  *
189
+ * Can also be used as a function, which prevents some linter errors.
190
+ *
191
+ * expect('everthing').to.be.ok();
192
+ *
169
193
  * @name ok
170
194
  * @api public
171
195
  */
172
196
 
173
- Assertion.addProperty('ok', function () {
197
+ Assertion.addChainableNoop('ok', function () {
174
198
  this.assert(
175
199
  flag(this, 'object')
176
200
  , 'expected #{this} to be truthy'
@@ -185,11 +209,15 @@ module.exports = function (chai, _) {
185
209
  * expect(true).to.be.true;
186
210
  * expect(1).to.not.be.true;
187
211
  *
212
+ * Can also be used as a function, which prevents some linter errors.
213
+ *
214
+ * expect(true).to.be.true();
215
+ *
188
216
  * @name true
189
217
  * @api public
190
218
  */
191
219
 
192
- Assertion.addProperty('true', function () {
220
+ Assertion.addChainableNoop('true', function () {
193
221
  this.assert(
194
222
  true === flag(this, 'object')
195
223
  , 'expected #{this} to be true'
@@ -206,11 +234,15 @@ module.exports = function (chai, _) {
206
234
  * expect(false).to.be.false;
207
235
  * expect(0).to.not.be.false;
208
236
  *
237
+ * Can also be used as a function, which prevents some linter errors.
238
+ *
239
+ * expect(false).to.be.false();
240
+ *
209
241
  * @name false
210
242
  * @api public
211
243
  */
212
244
 
213
- Assertion.addProperty('false', function () {
245
+ Assertion.addChainableNoop('false', function () {
214
246
  this.assert(
215
247
  false === flag(this, 'object')
216
248
  , 'expected #{this} to be false'
@@ -227,11 +259,15 @@ module.exports = function (chai, _) {
227
259
  * expect(null).to.be.null;
228
260
  * expect(undefined).not.to.be.null;
229
261
  *
262
+ * Can also be used as a function, which prevents some linter errors.
263
+ *
264
+ * expect(null).to.be.null();
265
+ *
230
266
  * @name null
231
267
  * @api public
232
268
  */
233
269
 
234
- Assertion.addProperty('null', function () {
270
+ Assertion.addChainableNoop('null', function () {
235
271
  this.assert(
236
272
  null === flag(this, 'object')
237
273
  , 'expected #{this} to be null'
@@ -247,11 +283,15 @@ module.exports = function (chai, _) {
247
283
  * expect(undefined).to.be.undefined;
248
284
  * expect(null).to.not.be.undefined;
249
285
  *
286
+ * Can also be used as a function, which prevents some linter errors.
287
+ *
288
+ * expect(undefined).to.be.undefined();
289
+ *
250
290
  * @name undefined
251
291
  * @api public
252
292
  */
253
293
 
254
- Assertion.addProperty('undefined', function () {
294
+ Assertion.addChainableNoop('undefined', function () {
255
295
  this.assert(
256
296
  undefined === flag(this, 'object')
257
297
  , 'expected #{this} to be undefined'
@@ -272,11 +312,15 @@ module.exports = function (chai, _) {
272
312
  * expect(bar).to.not.exist;
273
313
  * expect(baz).to.not.exist;
274
314
  *
315
+ * Can also be used as a function, which prevents some linter errors.
316
+ *
317
+ * expect(foo).to.exist();
318
+ *
275
319
  * @name exist
276
320
  * @api public
277
321
  */
278
322
 
279
- Assertion.addProperty('exist', function () {
323
+ Assertion.addChainableNoop('exist', function () {
280
324
  this.assert(
281
325
  null != flag(this, 'object')
282
326
  , 'expected #{this} to exist'
@@ -296,11 +340,15 @@ module.exports = function (chai, _) {
296
340
  * expect('').to.be.empty;
297
341
  * expect({}).to.be.empty;
298
342
  *
343
+ * Can also be used as a function, which prevents some linter errors.
344
+ *
345
+ * expect([]).to.be.empty();
346
+ *
299
347
  * @name empty
300
348
  * @api public
301
349
  */
302
350
 
303
- Assertion.addProperty('empty', function () {
351
+ Assertion.addChainableNoop('empty', function () {
304
352
  var obj = flag(this, 'object')
305
353
  , expected = obj;
306
354
 
@@ -326,6 +374,12 @@ module.exports = function (chai, _) {
326
374
  * expect(arguments).to.be.arguments;
327
375
  * }
328
376
  *
377
+ * Can also be used as a function, which prevents some linter errors.
378
+ *
379
+ * function test () {
380
+ * expect(arguments).to.be.arguments();
381
+ * }
382
+ *
329
383
  * @name arguments
330
384
  * @alias Arguments
331
385
  * @api public
@@ -341,8 +395,8 @@ module.exports = function (chai, _) {
341
395
  );
342
396
  }
343
397
 
344
- Assertion.addProperty('arguments', checkArguments);
345
- Assertion.addProperty('Arguments', checkArguments);
398
+ Assertion.addChainableNoop('arguments', checkArguments);
399
+ Assertion.addChainableNoop('Arguments', checkArguments);
346
400
 
347
401
  /**
348
402
  * ### .equal(value)
@@ -851,7 +905,7 @@ module.exports = function (chai, _) {
851
905
  }
852
906
 
853
907
  Assertion.addChainableMethod('length', assertLength, assertLengthChain);
854
- Assertion.addMethod('lengthOf', assertLength, assertLengthChain);
908
+ Assertion.addMethod('lengthOf', assertLength);
855
909
 
856
910
  /**
857
911
  * ### .match(regexp)
@@ -930,6 +984,7 @@ module.exports = function (chai, _) {
930
984
  if (!keys.length) throw new Error('keys required');
931
985
 
932
986
  var actual = Object.keys(obj)
987
+ , expected = keys
933
988
  , len = keys.length;
934
989
 
935
990
  // Inclusion
@@ -964,6 +1019,9 @@ module.exports = function (chai, _) {
964
1019
  ok
965
1020
  , 'expected #{this} to ' + str
966
1021
  , 'expected #{this} to not ' + str
1022
+ , expected.sort()
1023
+ , actual.sort()
1024
+ , true
967
1025
  );
968
1026
  }
969
1027
 
@@ -1002,6 +1060,7 @@ module.exports = function (chai, _) {
1002
1060
  * @param {String|RegExp} expected error message
1003
1061
  * @param {String} message _optional_
1004
1062
  * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
1063
+ * @returns error for chaining (null if no error)
1005
1064
  * @api public
1006
1065
  */
1007
1066
 
@@ -1026,7 +1085,10 @@ module.exports = function (chai, _) {
1026
1085
  constructor = null;
1027
1086
  errMsg = null;
1028
1087
  } else if (typeof constructor === 'function') {
1029
- name = (new constructor()).name;
1088
+ name = constructor.prototype.name || constructor.name;
1089
+ if (name === 'Error' && constructor !== Error) {
1090
+ name = (new constructor()).name;
1091
+ }
1030
1092
  } else {
1031
1093
  constructor = null;
1032
1094
  }
@@ -1040,12 +1102,14 @@ module.exports = function (chai, _) {
1040
1102
  err === desiredError
1041
1103
  , 'expected #{this} to throw #{exp} but #{act} was thrown'
1042
1104
  , 'expected #{this} to not throw #{exp}'
1043
- , desiredError
1044
- , err
1105
+ , (desiredError instanceof Error ? desiredError.toString() : desiredError)
1106
+ , (err instanceof Error ? err.toString() : err)
1045
1107
  );
1046
1108
 
1109
+ flag(this, 'object', err);
1047
1110
  return this;
1048
1111
  }
1112
+
1049
1113
  // next, check constructor
1050
1114
  if (constructor) {
1051
1115
  this.assert(
@@ -1053,11 +1117,15 @@ module.exports = function (chai, _) {
1053
1117
  , 'expected #{this} to throw #{exp} but #{act} was thrown'
1054
1118
  , 'expected #{this} to not throw #{exp} but #{act} was thrown'
1055
1119
  , name
1056
- , err
1120
+ , (err instanceof Error ? err.toString() : err)
1057
1121
  );
1058
1122
 
1059
- if (!errMsg) return this;
1123
+ if (!errMsg) {
1124
+ flag(this, 'object', err);
1125
+ return this;
1126
+ }
1060
1127
  }
1128
+
1061
1129
  // next, check message
1062
1130
  var message = 'object' === _.type(err) && "message" in err
1063
1131
  ? err.message
@@ -1072,6 +1140,7 @@ module.exports = function (chai, _) {
1072
1140
  , message
1073
1141
  );
1074
1142
 
1143
+ flag(this, 'object', err);
1075
1144
  return this;
1076
1145
  } else if ((message != null) && errMsg && 'string' === typeof errMsg) {
1077
1146
  this.assert(
@@ -1082,6 +1151,7 @@ module.exports = function (chai, _) {
1082
1151
  , message
1083
1152
  );
1084
1153
 
1154
+ flag(this, 'object', err);
1085
1155
  return this;
1086
1156
  } else {
1087
1157
  thrown = true;
@@ -1104,9 +1174,11 @@ module.exports = function (chai, _) {
1104
1174
  thrown === true
1105
1175
  , 'expected #{this} to throw ' + expectedThrown + actuallyGot
1106
1176
  , 'expected #{this} to not throw ' + expectedThrown + actuallyGot
1107
- , desiredError
1108
- , thrownError
1177
+ , (desiredError instanceof Error ? desiredError.toString() : desiredError)
1178
+ , (thrownError instanceof Error ? thrownError.toString() : thrownError)
1109
1179
  );
1180
+
1181
+ flag(this, 'object', thrownError);
1110
1182
  };
1111
1183
 
1112
1184
  Assertion.addMethod('throw', assertThrows);
@@ -1185,12 +1257,13 @@ module.exports = function (chai, _) {
1185
1257
  Assertion.addMethod('satisfy', function (matcher, msg) {
1186
1258
  if (msg) flag(this, 'message', msg);
1187
1259
  var obj = flag(this, 'object');
1260
+ var result = matcher(obj);
1188
1261
  this.assert(
1189
- matcher(obj)
1262
+ result
1190
1263
  , 'expected #{this} to satisfy ' + _.objDisplay(matcher)
1191
1264
  , 'expected #{this} to not satisfy' + _.objDisplay(matcher)
1192
1265
  , this.negate ? false : true
1193
- , matcher(obj)
1266
+ , result
1194
1267
  );
1195
1268
  });
1196
1269
 
@@ -1211,6 +1284,12 @@ module.exports = function (chai, _) {
1211
1284
  Assertion.addMethod('closeTo', function (expected, delta, msg) {
1212
1285
  if (msg) flag(this, 'message', msg);
1213
1286
  var obj = flag(this, 'object');
1287
+
1288
+ new Assertion(obj, msg).is.a('number');
1289
+ if (_.type(expected) !== 'number' || _.type(delta) !== 'number') {
1290
+ throw new Error('the arguments to closeTo must be numbers');
1291
+ }
1292
+
1214
1293
  this.assert(
1215
1294
  Math.abs(obj - expected) <= delta
1216
1295
  , 'expected #{this} to be close to ' + expected + ' +/- ' + delta
@@ -1218,9 +1297,13 @@ module.exports = function (chai, _) {
1218
1297
  );
1219
1298
  });
1220
1299
 
1221
- function isSubsetOf(subset, superset) {
1300
+ function isSubsetOf(subset, superset, cmp) {
1222
1301
  return subset.every(function(elem) {
1223
- return superset.indexOf(elem) !== -1;
1302
+ if (!cmp) return superset.indexOf(elem) !== -1;
1303
+
1304
+ return superset.some(function(elem2) {
1305
+ return cmp(elem, elem2);
1306
+ });
1224
1307
  })
1225
1308
  }
1226
1309
 
@@ -1228,7 +1311,9 @@ module.exports = function (chai, _) {
1228
1311
  * ### .members(set)
1229
1312
  *
1230
1313
  * Asserts that the target is a superset of `set`,
1231
- * or that the target and `set` have the same members.
1314
+ * or that the target and `set` have the same strictly-equal (===) members.
1315
+ * Alternately, if the `deep` flag is set, set members are compared for deep
1316
+ * equality.
1232
1317
  *
1233
1318
  * expect([1, 2, 3]).to.include.members([3, 2]);
1234
1319
  * expect([1, 2, 3]).to.not.include.members([3, 2, 8]);
@@ -1236,6 +1321,8 @@ module.exports = function (chai, _) {
1236
1321
  * expect([4, 2]).to.have.members([2, 4]);
1237
1322
  * expect([5, 2]).to.not.have.members([5, 2, 1]);
1238
1323
  *
1324
+ * expect([{ id: 1 }]).to.deep.include.members([{ id: 1 }]);
1325
+ *
1239
1326
  * @name members
1240
1327
  * @param {Array} set
1241
1328
  * @param {String} message _optional_
@@ -1249,9 +1336,11 @@ module.exports = function (chai, _) {
1249
1336
  new Assertion(obj).to.be.an('array');
1250
1337
  new Assertion(subset).to.be.an('array');
1251
1338
 
1339
+ var cmp = flag(this, 'deep') ? _.eql : undefined;
1340
+
1252
1341
  if (flag(this, 'contains')) {
1253
1342
  return this.assert(
1254
- isSubsetOf(subset, obj)
1343
+ isSubsetOf(subset, obj, cmp)
1255
1344
  , 'expected #{this} to be a superset of #{act}'
1256
1345
  , 'expected #{this} to not be a superset of #{act}'
1257
1346
  , obj
@@ -1260,7 +1349,7 @@ module.exports = function (chai, _) {
1260
1349
  }
1261
1350
 
1262
1351
  this.assert(
1263
- isSubsetOf(obj, subset) && isSubsetOf(subset, obj)
1352
+ isSubsetOf(obj, subset, cmp) && isSubsetOf(subset, obj, cmp)
1264
1353
  , 'expected #{this} to have the same members as #{act}'
1265
1354
  , 'expected #{this} to not have the same members as #{act}'
1266
1355
  , obj