Thursday, 11 May 2017

Mocha Async before() hook timing out

Background

I am creating a dummy server using net from Node.js in my Mocha test.

I have a dummy test, and I want to start the server before the test starts, and kill it after:

"use strict";

/*global describe, it, expect, before, after*/

const net = require("net");

describe("dummy server test", () => {

    const dummyReader = {
        IP: "localhost",
        port: 4002,
        server: undefined,
        socket: undefined
    };

    before("Starts dummy server", done => {

        dummyReader.server = net.createServer(socket => {
            dummyReader.socket = socket;
            done();
        });

        dummyReader.server.listen(dummyReader.IP, dummyReader.port);

    });

    after("Kills dummy server", done => {
        dummyReader.server.close();
        dummyReader.socket.destroy();
        done();
    });

    it("should pass", () => {
        expect(true).to.be.true;
    });
});

Problem

The problem is that my async before hook never completes. For a reason I can't understand done is never called, and thus the hook times out.

I tried increasing the time out, believing it could fix the issue, but to no avail.

Question

How can I fix my code?



via Flame_Phoenix

No comments:

Post a Comment