TypeError: app.address is not a function


I’m using Mocha to test my API, I’ve created a file to test my users routes which are contained in its custom file. I’ve also made an empty JSON file which will be filled and emptied respectively before and after each test.

File containing tests –> user-routes.spec.js :

const request = require("supertest")
const bodyParser = require("body-parser")

const app = require('./../../app')

const usersFixture = require("../fixtures/users-fixtures")

describe('Courses', () =>{

    beforeEach(() => { usersFixture.up() })
    afterEach(() => { usersFixture.down() })

    describe('Get users list', ()=>{
        it("Should get a list of all users", ()=>{
           return  request(app).get('/courses')
                        .then((res) => {
                            // res.body.should.have.lengthOf(1)
                            // res.body[0].title.should.eql("Ma course")
                        }).catch((err)  =>{
                            throw new Error(err);

app.js :

const mongoose = require("mongoose");
const bodyParser = require("body-parser");
mongoose.Promise = require("bluebird");

const course_router = require('./routes/courses_route');
const user_router = require('./routes/users_route');
const type_course_router = require('./routes/type_courses_route');
const avis_router = require("./routes/avis_router");

const HttpError = require('./errors/http_error')
const NotFoundError = require('./errors/not_found')

const helmet = require("helmet");
const express = require("express");
const cors = require("cors");
const app = express();
const manip = require("./routes/manip");

    extended: true

// app.use(file_upload());
app.set('view engine', 'ejs');
app.set('views',(__dirname +'/views/'));

//Ajout du slug course
app.use('/courses', course_router);
app.use('/users', user_router);
app.use('/type-courses', type_course_router);
app.use('/manip', manip);
app.use('/avis', avis_router);

app.use(function(req, res, next) {
  next(new NotFoundError());

app.use(function(err, req, res, next) {
  if (!(err instanceof HttpError)) {
    err = new HttpError(err.message || 'Unknown error')

//Connexion à la bdd
mongoose.connect('mongodb://WecodeAdmin:AG56_'+'%25'+'[email protected]:41055/runmoov_test')
                    console.log("Connection à mongolab réussie")

app.listen(3000, function(){
    console.log("API en cours d'exécution sur le port 3000");

When launching my test file I have an error :

TypeError: app.address is not a function

I’m pretty sure the problem comes from app.js or user-routes.spec.js, I’ve tested adding module.exports = server; at the end of my app.js, I’ve also seen things talking about agant keyword but I don’t know it is about
Any ideas where the deal is ?


You need to make sure that app.js is exporting your Express server, so that it can be imported in user-routes.spec.js.

First assign the result of app.listen to a variable, and then set that variable as the export in your app.js:

const server = app.listen(3000, function(){
    console.log("API en cours d'exécution sur le port 3000");

module.exports = server;

Answered By – Christian Santos

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published