You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.<computed> [as get] (node_modules/supertest/index.js:43:18)
at Object.<anonymous> (__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);
})
})
})
})
in the last line of your app.ts you exported the app like that: module.exports= app;
when importing it on movie.test.ts, you imported destructuring your app object import {app} from '../app';
when descructuring, this line of code is trying to access the property app from your module.exports, but app is the module.exports itself
the correct syntax would be import app from '../app';
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
Actual behavior
Expected behavior
Code to reproduce
app.ts
movie.test.ts
pacckage.json
Checklist
The text was updated successfully, but these errors were encountered: