forwardemail / forwardemail/supertest

[fix] TypeError: Cannot read properties of undefined (reading 'address')

Open
#808 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug
Supertest connot read or find my adress route that i want test
**Node.js version:** v19.0.0

**OS version:** 22.04

**Description:**
If i use supertest to call pne route of my API i get this error

```
FAIL __test__/movie.test.ts
Movie
Hello world route
Hello world given 404
✕ Should be return 404 (1 ms)

● Movie › Hello world route › Hello world given 404 › Should be return 404

TypeError: Cannot read properties of undefined (reading 'address')

10 | it("Should be return 404", async() => {
11 | const movieId = 1;
> 12 | supertest(app).get('/api/v1/').expect(404);
| ^
13 | // expect(true).toBe(true);
14 | })
15 | })

at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj. [as get] (node_modules/supertest/index.js:43:18)
at Object. (__test__/movie.test.ts:12:32)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.973 s, estimated 3 s
Ran all test suites.
```

## Actual behavior

## Expected behavior

## Code to reproduce

**app.ts**
```
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import * as mongoose from "mongoose";
import apiRouter from "./router/apiRouter";

//Auto decouvrability
const hateoasLinker = require('express-hateoas-links');

export const app:express.Application = express();

app.use(express.urlencoded({
extended: true
}));

app.use(hateoasLinker);

// Configurations
app.use(cors());
dotenv.config( {path : './.env'}); // for env variable
app.use(express.json()); // json form data

let hostName:string|undefined = process.env.HOST_NAME;
let port:number|undefined = Number(process.env.PORT);
let mongoDBUrl:string|undefined = process.env.MONGODB_URL;

// MongoDB connection

if(mongoDBUrl) {
mongoose.connect(mongoDBUrl).then( () => {
console.log('Connecting to mongoDB Successfully ...');
}).catch( (error) => {
console.log(error);
process.exit(1); // Stop the node js process
});
}

app.get('/test', async (request:express.Request, response:express.Response) => {
response.status(404);
})

// Route Configuration

app.use('/api/v1/', apiRouter);

if(port !== undefined && hostName !== undefined){
app.listen(port, hostName, () => {
console.log(`Express Server is running at ${hostName}:${port}`);
});
}

module.exports= app;

```

**movie.test.ts**
```
import supertest from "supertest";
import {app} from '../app';

describe("Movie", () => {

describe("Hello world route", () => {

describe("Hello world given 404", () => {

it("Should be return 404", async() => {
const movieId = 1;
supertest(app).get('/api/v1/').expect(404);
// expect(true).toBe(true);
})
})
})
})
```

**pacckage.json**

```
{
"name": "crudmovieservice",
"version": "1.0.0",
"description": "",
"main": "app.ts",
"scripts": {
"ts-jest": "ts-jest config:init",
"test": "jest",
"start": "nodemon app.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/axios": "^0.14.0",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/express-validator": "^3.0.0",
"@types/mongoose": "^5.11.97",
"@types/node": "^18.7.21",
"@types/nodemon": "^1.19.2",
"axios": "^0.27.2",
"cors": "^2.8.5",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"express-hateoas-links": "^1.3.1",
"express-validator": "^6.14.2",
"mocha": "^10.1.0",
"mongodb-memory-server": "^8.10.2",
"mongoose": "^6.6.1",
"node": "^18.9.1",
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"tsc": "^2.0.4"
},
"devDependencies": {
"@types/jest": "^29.2.5",
"@types/supertest": "^2.0.12",
"chai": "^4.3.7",
"jest": "^29.3.1",
"supertest": "^6.3.3",
"ts-jest": "^29.0.3"
}
}

```

## Checklist

- [x] I have searched through GitHub issues for similar issues.
- [x] I have completely read through the README and documentation.
- [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

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.