TypeError: expect(...).to.have.length.of is not a function


#1

Trying to write integration tests using Chai. The test I’ve written is supposed to return all existing blog posts from my test DB, prove the response has the correct status, and that the number of posts in the response is equal to the number in the DB. I’m trying to check the length of the response body to be sure that my test DB seeding worked, but I keep getting this error:

TypeError: expect(...).to.have.length.of is not a function

This is the relevant piece of code:

`describe('GET endpoint', function() {
		it('should return all existing blog posts', function() {
			let res;
			return chai.request(app)
			.get('/posts')
			.then(function(_res) {
				res = _res;
				expect(res).to.have.status(200);
				expect(res.body).to.have.length.of.at.least(1);
				return BlogPost.count();
			})
			.then(function(count) {
				expect(res.body).to.have.length.of(count);
			})
		})`

I tried changing the offending line to res.body.should.have.length.of.at.least(1), but that gave me this error: AssertionError: expected { Object (_events, _eventsCount, ...) } to have property 'length'.

Anyone have any idea why this is happening?


#2

It’s been a while since I’ve used Chai. Does this work?

expect(res.body).to.have.lengthOf.at.least(1)

#4

Is it the exact same error or is length now lengthOf on the error message?


#5

I was wrong, @object88’s solution worked – I was so eager to run it that I didn’t change the following assertion to match. The test is passing now :blush:Thanks for your help!