TypeError: Cannot read property 'address' of undefined
See original GitHub issueI 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);
});
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.jstest/app.test.js
@voiddeveloper I was also facing the same issue, this should work
posts.spec.ts