Skip to content

Commit bece7c3

Browse files
committedNov 3, 2017
Merge branch 'master' of github.com:hapijs/code
2 parents d2af62a + 7cf8a2d commit bece7c3

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
@@ -259,7 +259,7 @@ internals.addMethod('empty', internals.empty);
259259

260260
internals.length = function (size) {
261261

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

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

‎test/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,20 @@ describe('expect()', () => {
14451445

14461446
Hoek.assert(exception.message === 'Expected \'a\' to have a length of 10 but got 1', exception);
14471447
});
1448+
1449+
it('throws on length check on objects with no length property', (done) => {
1450+
1451+
let exception = false;
1452+
try {
1453+
Code.expect(null).to.have.length(2);
1454+
}
1455+
catch (err) {
1456+
exception = err;
1457+
}
1458+
1459+
Hoek.assert(exception.message === 'Can only assert length on object, array or string', exception);
1460+
done();
1461+
});
14481462
});
14491463

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

0 commit comments

Comments
 (0)
Please sign in to comment.