'Number Matcher Error even though values are of type Number

I am having trouble with the third test case in my describe case. More specifically, I get the error with the line expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1). Here I keep getting a "Matcher error: received value must have a length property whose value must be a number" error. I used the typeof function and it found that both my received and expected values are numbers but I still keep getting this error. Any help would be greatly appreciated.

Code

const supertest = require('supertest')
const mongoose = require('mongoose')
const app = require('../app')
const Blog = require('../models/blog')
const { initial } = require('lodash')
const api = supertest(app)


const initalBlogs = [
        {
        title: "React patterns",
        author: "Michael Chan",
        url: "https://reactpatterns.com/",
        likes: 7
        },
        {
        title: "Go To Statement Considered Harmful",
        author: "Edsger W. Dijkstra",
        url: "http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html",
        likes: 5
        }
]

beforeEach(async () => {
    await Blog.deleteMany({})
    let newBlog = new Blog(initalBlogs[0])
    await newBlog.save()
    newBlog = new Blog(initalBlogs[1])
    await newBlog.save()
})

describe('blog tests', () => {
    test('returns blog list length', async () => {
        const blogs = await api.get('/api/blogs')
        const result = blogs._body
        expect(result.length).toBe(2)
    })

    test('verify unique identifier is named id', async () => {
        const blogs = await api.get('/api/blogs')
        const result = blogs._body
        result.forEach((element) => {
            expect(element.id).toBeDefined()
        })
    })
//ISSUES HERE
    test('adds new blog', async () => {
        const newBlog = {
            title: "Node patterns",
            author: "Cool Chan",
            url: "https://fregrferfref.com/",
            likes: 7
            }
        const result = await api.post('/api/blogs').send(newBlog).expect(201)
        const secondResult = await api.get('/api/blogs')
        const filteredArr = secondResult._body.map(a => a.title);
        expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1) //Says the length property value should be a number even though it is a number
        expect(filteredArr).toContain('Node patterns')
        
    })
})

afterAll(() => {
    mongoose.connection.close()
})


Solution 1:[1]

In this line

expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1)

you are computing the length twice: once with .length and once with .toHaveLength().
In other words, you are testing the value of secondResult._body.length.length, which throws an error because secondResult._body.length is a number and therefore its length is undefined.

Try

expect(secondResult._body).toHaveLength(initalBlogs.length + 1)

or

expect(secondResult._body.length).toBe(initalBlogs.length + 1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ValleyCrisps