0

This is my test:

const request = require('supertest');
const makeRestController = require('../main/controller/restController');

const userRepository = {
    getCount: async () => {
        return 2;
    }
};
const logger = {
    info: () => {
    }
};

let appUnderTest = null;

beforeEach(() => {
    appUnderTest = makeRestController(userRepository, 8081, logger);
});

afterEach(() => {
    appUnderTest.server.close();
});

test('test count', function (done) {
    if(!appUnderTest) console.log("UNDEFINED!");

    request(appUnderTest.app)
        .get('/')
        .end(function (err, res) {
            if (err) return done(err);
            expect(res).toBeDefined();
            done();
        });
});

I'm running it with jest. I have a weird behaviour.

After I run the tests, every other time, this test is failing:

 FAIL  __tests__/appWeb.js
  ● test count

    expect(received).toBeDefined()

    Expected value to be defined, instead received
      undefined

      at Test.<anonymous> (__tests__/appWeb.js:31:21)
      at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:179:6)
      at Server.assert (node_modules/supertest/lib/test.js:131:12)
      at Object.onceWrapper (events.js:314:30)
      at emitNone (events.js:105:13)
      at Server.emit (events.js:207:7)
      at emitCloseNT (net.js:1648:8)
      at _combinedTickCallback (internal/process/next_tick.js:135:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

Then the next time, everything is green, and the next time is red. Could you tell me what I am doing wrong?

The function makeController returns {server, app} because I need to close the server after the test otherwise jest doesn't exit.

const express = require('express');

function makeRestController(userRepository, HTTP_PORT, logger) {
            const app = express();
            app.get('/', function (req, res) {
                userRepository.getCount().then((count) => res.send(`Count: ${count}`));
            });
            const server = app.listen(HTTP_PORT, function () {
                logger.info('App listening on port ' + HTTP_PORT + '!');
            });

            return {server, app};
}

module.exports = makeRestController;

I also did another try return a promise from the beforeEach doing something like this:

test('test count', function (done) {
    if(!appUnderTest) console.log("UNDEFINED!");

    appUnderTest.then(x => {
        request(x.app)
            .get('/')
            .end(function (err, res) {
                if (err) return done(err);
                expect(res).toBeDefined();
                done();
            });
    });
});

But I still get random failure because of expect(res).toBeDefined();

dierre
  • 7,140
  • 12
  • 75
  • 120
  • You sure the server has started properly before sending the request? Considering that the server's listen function is async, the beforeEach function could finish before the server has started, and the tests will start running too early. – Mika Sundland Oct 29 '17 at 14:50
  • Hi! That could be absolutely an issue, although I followed tutorials I found around the web. What would be a good way to verify that? Because app.listen doesn't return a promise as far as I know. – dierre Oct 29 '17 at 15:03
  • By using `done()`. I would actually start by inserting some console.logs in the listen and close callbacks for the server to see that the server is starting and stopping between every test. Also call `done()` inside the callback you make for `appUnderTest.server.close();` in `afterEach`, similar to the test itself. – Mika Sundland Oct 29 '17 at 16:32

0 Answers0