Supertest: TypeError: Cannot read property 'address' of undefined

Created on 2 Mar 2020  路  9Comments  路  Source: visionmedia/supertest

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

All 9 comments

Having the exact same issue...

@voiddeveloper Your custom .listen method needs to return the express application*.

im having the same issue but with e2e test... any news?

i have this issue with e2e test too!

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

I have this same issue too..

@voiddeveloper Your custom .listen method needs to return the _express application_*.

Is there any method to use supertest without returning express application?

It can be fixed by giving "http.Server" type parameter to request function.
therefore, app.ts should return http.Server to solve that problem. @voiddeveloper

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schm1ty1 picture schm1ty1  路  4Comments

peterjuras picture peterjuras  路  3Comments

nazreen picture nazreen  路  3Comments

rkmax picture rkmax  路  5Comments

nareshbhatia picture nareshbhatia  路  6Comments