question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'address' of undefined

See original GitHub issue

I created an express.js server with ts and tried to test it, but got an error. I can’t find a solution no matter how I search.

server.ts

import App from './app';
import PostsRouter from './routes/posts';

export const app = new App(
    [
        new PostsRouter(),
    ],
    3000,
).listen();

app.ts

import * as express from 'express';

class App {
    public app: express.Application;
    public port: number;

    constructor(controllers: any[], port: number) {
        this.app = express();
        this.port = port;

        this.initializeControllers(controllers);
    }

    private initializeControllers(controllers: any[]) {
        controllers.forEach((controller) => {
            this.app.use('/', controller.router);
        });
    }

    public listen() {
        this.app.listen(this.port, () => {
            console.log(`App listening on the port ${this.port}`);
        });
    }
}

export default App;

posts.spec.ts

import * as request from "supertest";
import { app } from "../server";


describe("GET /posts", () => {
    it("respond with json", async done => {
        await request(app)
            .get("/posts")
            .set("Accept", "application/json")
            .expect("Content-Type", /json/)
            .expect(200, done);
    });
});

Screen Shot 2020-03-03 at 7 47 26 AM

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:9

github_iconTop GitHub Comments

7reactions
mirsahibcommented, Jan 2, 2021

I have the same issue because i forgot to mention module.exports = app in app.js but my folder structure is different here is my folder structure app.js

const http = require("http");
const express = require("express");

const app = express();
const server = http.Server(app);

app.post("/example", function (request, response) {
    // my router logic
});

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", () => {
  console.log("Server running");
});
module.exports = app

test/app.test.js

const app = require("../app.js"); // runs app.js
const request = require("supertest"); // needed to make API requests

describe("When the example router  is running", () => {
  // Add individual test cases
});

3reactions
iamdarshanshahcommented, Apr 9, 2020

@voiddeveloper I was also facing the same issue, this should work

posts.spec.ts

import * as request from "supertest";
import App from '../app';
import PostsRouter from '../routes/posts';

// This will return the express application 
const api = request(new App([
        new PostsRouter(),
    ],
    3000).app);

describe("GET /posts", () => {
    it("respond with json", async done => {
        await api
            .get("/posts")
            .set("Accept", "application/json")
            .expect("Content-Type", /json/)
            .expect(200, done);
    });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'address' of undefined supertest - Stack ...
I received a similar error from mocha when testing an express app. Full text of error: 0 passing (185ms) 2 failing 1) loading...
Read more >
TypeError: Cannot read property 'address' of undefined · Issue ...
I created an express.js server with ts and tried to test it, but got an error. I can't find a solution no matter...
Read more >
TypeError: Cannot read property 'address' of undefined
I created an express.js server with ts and tried to test it, but got an error. I can't find a solution no matter...
Read more >
Cannot read property 'address' of undefined
It means you are not importing the address in the file. console.log the object that is supposed to have the address, and start...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError: Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found