Search code examples
node.jsmongoosemocha.jssupertest

TypeError: Cannot read property 'address' of undefined


I need some help with testing node js code. I'm using mocha and supertest and I keep getting an error the same as the title on my test. I'm also using mongoose to connect to a mongo server and before I run the tests am adding in a few guest objects. I've been stumped on this for 2 days so any help is appreciated.

Here is the test error

  0 passing (96ms)
  1 failing

  1) Guestss
       GET /guests
         should GET all the guests:
     TypeError: Cannot read property 'address' of undefined
      at Test.serverAddress (node_modules/supertest/lib/test.js:55:18)
      at new Test (node_modules/supertest/lib/test.js:36:12)
      at Object.get (node_modules/supertest/index.js:25:14)
      at Context.<anonymous> (test/functional/api/guestTest.js:95:18)

Here is the test code

describe("GET /guests", () => {
        it("should GET all the guests", done => {
            console.log("before");
            request(server)
                .get("/guests")
                .set("Accept", "application/json")
                .expect("Content-Type", /json/)
                .expect(200)
                .end((err, res) => {
                    try {
                        expect(res.body).to.be.a("array");
                        expect(res.body.length).to.equal(2);
                        let result = _.map(res.body, guest => {
                            return {
                                name: guest.name,
                                people: guest.people,
                                roomno: guest.roomno,
                                breakfast: guest.breakfast,
                                roomtype: guest.roomtype,
                                check: guest.check
                            }
                        });
                        expect(result).to.deep.include({
                            name: "Tommy blue",
                            people: 5,
                            roomno: 0,
                            breakfast: true,
                            roomtype: "family",
                            check: "waiting"
                        });
                        expect(result).to.deep.include({
                            name: "Chad Warren",
                            people: 2,
                            roomno: 45,
                            breakfast: true,
                            roomtype: "double",
                            check: "in"
                        });
                        done()
                    } catch (e) {
                        done(e)
                    }
                });
        });
    });

Solution

  • After looking into it the error messages were sending me on a wild goose chase and i just wasn't exporting my server.