TypeError: Cannot read property 'address' of undefined

TypeError: Cannot read property 'address' of undefined

我需要一些关于测试节点 js 代码的帮助。我正在使用 mocha 和 supertest,但我不断收到与测试标题相同的错误。我还使用 mongoose 连接到 mongo 服务器,在我 运行 测试之前添加了一些来宾 objects。我已经为此困惑了 2 天,所以非常感谢您的帮助。

这里是测试错误

  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)

这里是测试代码

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)
                    }
                });
        });
    });

在查看它之后,错误消息让我无所适从,我只是没有导出我的服务器。

这里也一样。我的测试有

const { app } = require('./app');

我的应用程序有

module.exports = app;  

我不得不将 app.js 行更改为像测试中那样有花括号:

module.exports = { app };