12

I need some help to resolve my problem with testing on nodejs codes. I'm using mocha and supertest. I'm confused with the implementation in supertest. I don't know to resolved it. I'm trying to automate downloading a file.

describe('GET /entry/:entryId/file/:id/download', function(){
    it('should pass download function', function(done){
        this.timeout(15000);
        request(app.webServer)
            .get('/entry/543CGsdadtrE/file/wDRDasdDASAS/download')
            .set('Authorization', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGco')
            .expect(200)
            .end(function(err, res) {
                if (err) return done(err);
                console.log(err, res);
                done();
            });
    });
});
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Saitama
  • 131
  • 1
  • 1
  • 6
  • i already include it thank you i hope you can help me bro – Saitama May 05 '16 at 07:02
  • it says that theres a problem with the /node-modules/supertest/lib/test.js. and when I look at the test.js i found this – Saitama May 05 '16 at 07:13
  • ` Test.prototype.serverAddress = function(app, path){ var addr = app.address(); if (!addr) this._server = app.listen(0); var port = app.address().port; var protocol = app instanceof https.Server ? 'https' : 'http'; return protocol + '://127.0.0.1:' + port + path; }; ` – Saitama May 05 '16 at 07:14
  • I already did some config on the url and port but the same problem exist – Saitama May 05 '16 at 07:16
  • 1
    Can you post what it is printing console.log(app.webServer)? – Subburaj May 05 '16 at 07:21
  • The post itself should have a [mcve] of the server code to reproduce the problem. `app.webServer` must be undefined or some other object depending on how/what is exported by the server module and what is imported by the testing suite. None of these things are shown. – ggorlen Oct 22 '20 at 20:57

3 Answers3

19

I received a similar error from mocha when testing an express app. Full text of error:

0 passing (185ms)
2 failing

1) loading express responds to /:
 TypeError: app.address is not a function
  at Test.serverAddress (test.js:55:18)
  at new Test (test.js:36:12)
  at Object.obj.(anonymous function) [as get] (index.js:25:14)
  at Context.testSlash (test.js:12:14)

2) loading express 404 everything else:
 TypeError: app.address is not a function
  at Test.serverAddress (test.js:55:18)
  at new Test (test.js:36:12)
  at Object.obj.(anonymous function) [as get] (index.js:25:14)
  at Context.testPath (test.js:17:14)

I fixed it by adding this to my express server.js, i.e. export the server object

module.exports = app
Colin D
  • 2,822
  • 1
  • 31
  • 38
  • 1
    I don't know why, but `module.exports = {app}` worked for me instead of `module.exports = app` – Ruchit Vithani Apr 03 '20 at 18:10
  • 2
    Whether `module.exports = app` or `module.exports = {app}` fixes the problem has to do with how the test suite is using it, e.g. `const {app} = require("./your-server")` or `const app = require("./your-server")`. Regardless of how you export/import it, the important thing is that the `chai.request(app)` is actually being given a real app object and not something like undefined or some other object. – ggorlen Oct 22 '20 at 20:55
7

Typescript users, who are facing this error, check two things:

  1. The express server should have module.exports = app (thanks to @Collin D)
  2. Use import * as app from "./app"
    instead of wrong import app from "./app"
Artur A
  • 7,115
  • 57
  • 60
  • @Saitama I know this is old but you should mark this as the answer as it solved for me as well. – Zacho Dec 10 '17 at 22:29
  • I can't import that way, I get this compiler error when I try and configure app: `error TS2339: Property 'set' does not exist on type 'typeof "src/app"'` – Derek Shockey Jun 10 '18 at 02:36
2

I was facing same problem, above solution didn't work for me, some one in my shoes kindly follow this guy's

exports in server.js should be

module.exports.app = app;

If you have multiple modules than use es6 feature

module.exports = {
  app,
  something-else,
  and-so-on
}

my package.json for version cross ref..

{
  "name": "expressjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha **/*.test.js",
    "start": "node app.js",
    "test-watch": "nodemon --exec npm test"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4",
    "hbs": "^4.0.1"
  },
  "devDependencies": {
    "mocha": "^5.2.0",
    "supertest": "^3.3.0"
  }
}
hemant singh
  • 149
  • 3
  • 7
  • Showing how you're importing it might help too. `module.exports = app` is just fine as long as the import is `const app = require("./your-server")`. – ggorlen Oct 22 '20 at 20:59