Ben
Ben

Reputation: 6086

Mocha, Express test error - "after all" hook error - Object function has no method 'close'

Im just getting started with Mocha, testing against a very basic Express 4.0 rest API.

describe('API CALL UNIT TESTING', function(){
    var app = require('../../app');
    before(function(){
        app.listen(3000);
    });

    describe('GET', function(){

        it('respond with json', function(done){
            request(app)
                .get('/api/compile')
                .set('Accept', 'application/json')
                .expect('Content-Type', 'application/json')
                .expect(200, done)
                .end(function(e, res){
                    //console.log(res)
                    done();
                })
        })
    });

    after(function() {
        app.close();
    });
});

Im getting the following error when running the test:

1 passing (48ms) 1 failing

1) API CALL UNIT TESTING "after all" hook: TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'close'

Can anyone advise what is causing the "after all" hook error?

Upvotes: 0

Views: 7932

Answers (1)

iandotkelly
iandotkelly

Reputation: 9134

Clearly the app object does not have a close() method. You don't actually tell us precisely what app is - but if I recall the express API correctly, you actually call the close() on the object returned from listen(), so perhaps you could try:

var server;

before(function(){
    server = app.listen(3000);
});

 ....

after(function() {
    server.close();
});

Upvotes: 7

Related Questions