Mocha Supertest Express: app.address is not a function even if I exported correctly app.
See original GitHub issueGood 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:
- Created 5 years ago
- Comments:9
Top 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 >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
@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 userequire
instead.See: https://github.com/Danver97/user-service/blob/master/test/integration.test.js#L4
@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 ofapp.listen
doesn’t work. I can’t understand why…