1

Ok, my mocha tests will pass if I comment out the 'before' and 'after' methods. I am sure that both of my errors are related to each other.

The 'after' method fails stating app.close isn't a function. The 'before' method fails saying it cant find 'app' on my line 7 (clearing server cache).

I am completely out of options or ideas. I would like to be able to start and stop my server at my command. This is the first time that I have attempted to include any type of 'before/after' methods to my mocha testing. working code below, but with my failing portion commented out. Any suggestions??

var request = require('supertest');
var app = require('../../server');

describe('server', function() {
    before(function () {
        //var app = require('../../server')();
        //delete require.cache[require.resolve('app')];
    });
    after(function () {
        //app.close();
    });
    describe('basic comms', function() {
        it('responds to root route', function testSlash(done) {
            request(app)
                .get('/')
                .expect('Content-type', /json/)
                //.expect(res.message).to.equal('Hello World!')
                .expect(200, done);
        });
        it('404 everything else', function testPath(done) {
            //console.log('testing 404 response');
            request(app)
                .get('/foo/bar')
                .expect(404, done);
        });
    });
});
Energetic Pixels
  • 349
  • 3
  • 15

1 Answers1

0

In before you require your app in a different way than in line 2. Why would you not use already required app?

Example:

before(function () {
    // here you can use app from line 2
});

Regarding app.close, where did you find this function?

Check Express docs: http://expressjs.com/en/4x/api.html#app

To close express server, you can use this approach:

how to properly close node-express server?

Community
  • 1
  • 1
Andrzej Karpuszonak
  • 8,896
  • 2
  • 38
  • 50
  • 1
    Thank you @Andrei. I took your suggestion with that last link and used it. As far as leaving line 2 in there to ref my server, I did that so that I could activate it or comment it out when I needed to, to test that my 'it' still worked without anything being in my 'before' or 'after'. Now that your suggestion proved AWESOME, I have permanently moved that line to my 'before'. Thank you. – Energetic Pixels Feb 28 '16 at 18:03