res.body.some is not a function in node.js tests

Viewed 476

I am writing some tests for a NodeJs API that I am creating and am getting an error when trying to check if a value exists in a MongoDB returned JSON object.

The error I am getting is:

TypeError: res.body.some is not a function

I am sampling some example code that I found, but I think perhaps the method may have been deprecated maybe.

If some is not a valid function what should be used instead?

Versions:

Node: v8.10.0

Express: 3.5.2

Request Object: const request = require('supertest');

Server Object

let server;

describe('/api/hosts', () => {
    beforeEach(() => { 
        server = require('../../index');
    })
    afterEach(async () => { 
      await server.close();
      await Host.deleteMany({});
    });

Declaration of express and JSON parser:

const express = require('express');
const hosts = require('../routes/hosts');

module.exports = function(app) {
  app.use(express.json());
  app.use('/api/hosts', hosts);
}

Full Error:

    GET /:ipV4
      ✕ should return a host when a valid IPv4 is passed (14ms)

  ● /api/hosts › GET /:ipV4 › should return a host when a valid IPv4 is passed

    TypeError: res.body.some is not a function

      61 | 
      62 |             const res = await request(server).get('/api/hosts/' + host.ipV4);
    > 63 |             expect(res.body.some(h => h.ipV4 === '127.0.0.1')).toBeTruthy();
         |                             ^
      64 |             expect(res.status).toBe(200);
      65 |         });
      66 | 

          at Object.it (tests/integration/hosts.test.js:63:29)

Test

describe('GET /:ipV4', () => {
    it('should return a host when a valid IPv4 is passed', async () => {
        const host = new Host({ ipV4: '127.0.0.1' });
        await host.save();

        const res = await request(server).get('/api/hosts/' + host.ipV4);
        expect(res.body.some(h => h.ipV4 === '127.0.0.1')).toBeTruthy();
        expect(res.status).toBe(200);
    });
});
0 Answers
Related