forwardemail / forwardemail/supertest

TypeError: Cannot read property 'address' of undefined

Open
#633 10 comments 12 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

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](https://user-images.githubusercontent.com/17779284/75725174-46209380-5d23-11ea-8356-72a45bbc070f.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.