Skip to content

Commit 70c82c7

Browse files
AdriVanHoudtcjihrig
authored andcommittedOct 9, 2017
fix(length): throw decent error on length check of null (#111)
Fixes #110
1 parent a878e86 commit 70c82c7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
 

‎lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ internals.addMethod('empty', internals.empty);
258258

259259
internals.length = function (size) {
260260

261-
internals.assert(this, typeof this._ref === 'object' || typeof this._ref === 'string', 'Can only assert empty on object, array or string');
261+
internals.assert(this, (typeof this._ref === 'object' && this._ref !== null) || typeof this._ref === 'string', 'Can only assert length on object, array or string');
262262

263263
const length = this._ref.length !== undefined ? this._ref.length : Object.keys(this._ref).length;
264264
return this.assert(length === size, 'have a length of ' + size, length);

‎test/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,20 @@ describe('expect()', () => {
15381538
Hoek.assert(exception.message === 'Expected \'a\' to have a length of 10 but got 1', exception);
15391539
done();
15401540
});
1541+
1542+
it('throws on length check on objects with no length property', (done) => {
1543+
1544+
let exception = false;
1545+
try {
1546+
Code.expect(null).to.have.length(2);
1547+
}
1548+
catch (err) {
1549+
exception = err;
1550+
}
1551+
1552+
Hoek.assert(exception.message === 'Can only assert length on object, array or string', exception);
1553+
done();
1554+
});
15411555
});
15421556

15431557
describe('equal()', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.