question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocha Supertest Express: app.address is not a function even if I exported correctly app.

See original GitHub issue

Good morning. I’m still considering quite a newbe in Node.Js since very often I get into problems that with a bit of searching I figure out how to solve. Except this.

This is my ./src/app.js file:

const express = require('express');
const bodyParser = require('body-parser');
const ENV = require('./env');
const request = require('supertest');
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req, res) => {
    res.json({
        service: 'user-service',
    });
});

// This works correctly
const req = request(app);
req
    .get('/')
    .expect('Content-Type', /json/)
    .expect(JSON.stringify({
        service: 'user-service'
    }))
    .expect(200)
    .end(function (err, res) {
        if (err) throw err;
    });

// I tired in both ways
module.exports = Object.assign({}, app);
// module.exports = app;

This is my ./index.js file:

const app = require('./src/app');
const ENV = require('./src/env');

app.listen(3000, () => {
    console.log(`Server started on port ${ENV.port}`);
});

module.exports = app;

This is my ./test/test.js file:

const request = require('supertest');
const app = request('../src/app');
/*
  using request('http://localhost:3000') works correctly
  but i know it's not the best way to setup a testing environment
*/
//const req = request('http://localhost:3000'); 
const req = request(app);

describe('Integration test exaple', function() {
    it('get /', function(done) {
        req
        .get('/')
        .expect('Content-Type', /json/)
        .expect(JSON.stringify({ service: 'user-service' }))
        .expect(200, done);
    });
});

I tried to require app both from app.js and index.js but the result it’s the same:

>> mocha ./test/test

  Integration test exaple
    1) get /

  0 passing (9ms)
  1 failing

  1) Integration test exaple
       get /:
     TypeError: app.address is not a function
      at Test.serverAddress (node_modules\supertest\lib\test.js:55:18)
      at new Test (node_modules\supertest\lib\test.js:36:12)
      at Object.obj.(anonymous function) [as get] (node_modules\supertest\index.js:25:14)
      at Context.<anonymous> (test\integration.test.js:12:10)

Thanks in advance for any kind of help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
jonathansaminescommented, Aug 17, 2018

@Danver97 Not sure how I didn’t notice that before but the problem is that you are using the function request to try to require your app in your tests. You should use require instead.

See: https://github.com/Danver97/user-service/blob/master/test/integration.test.js#L4

2reactions
Danver97commented, Aug 16, 2018

@jonathansamines It’s pretty curious: I checked again and using module.exports = app doesn’t work, BUT in another project it’s working fine… Also using the result of app.listen doesn’t work. I can’t understand why…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocha Supertest Express: app.address is not a function even ...
I'm still considering quite a newbe in Node. ... Mocha Supertest Express: app.address is not a function even if I exported correctly app....
Read more >
Mocha API Testing: getting 'TypeError: app.address is not a ...
The answers above correctly address the issue: supertest wants an http. ... Export app at the end of the main API file like...
Read more >
For anyone who found TypeError: app.address is not a function
You simply just have to export your server.js. “For anyone who found TypeError: app.address is not a function” is published by Irfandy Jip....
Read more >
Mocha Supertest Express: app.address is not a function even if I ...
Mocha Supertest Express : app.address is not a function even if I exported correctly app.
Read more >
getting 'TypeError: app.address is not a function' - YouTube
JavaScript : Mocha API Testing: getting 'TypeError: app. address is not a function ' [ Gift : Animated Search Engine ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found