-1

I have a jest test file that looks like this:

import Client from "socket.io-client"

describe("my awesome project", () => {
    let clientSocket;

    beforeAll((done) => {
        clientSocket = new Client(`http://localhost:3000`);
        clientSocket.on("connect", done);
    });

    afterAll(() => {
        clientSocket.close();
    });

    it("should work", (done) => {
        clientSocket.on("redacted", (message) => {
            expect(2 + 2).toBe(56);
            //expect(message === "foobar").toEqual(true);
            done();
        });
        clientSocket.emit("redacted", "world");
    });
});

This is a POC and this currently the entire implementation.

The jest.config looks like this:

export default {
  // Automatically clear mock calls, instances, contexts and results before every test
  clearMocks: true,

  // Indicates whether the coverage information should be collected while executing the test
  collectCoverage: true,

  // The directory where Jest should output its coverage files
  coverageDirectory: "coverage",

  // Indicates which provider should be used to instrument code for coverage
  coverageProvider: "v8",

  // A preset that is used as a base for Jest's configuration
  preset: "ts-jest",
};

Which is just the file the --intit command generated.

The core of my problem is that any expect I use that results in a failed test, no matter how trivial takes an absurd amount of time to complete. I accidentally left it running as above overnight and it eventually completed in 14 hours.

But with a passing test Jest is absolutely fine and completes rapidly.expect(2 + 2).toBe(4); for example runs perfectly. On the failed tests I see the data come back from the socket as expected in the time expected. Its only when the expect is hit that it stalls out. So I don't believe the problem is in the socket setup or some sort of communication problem.

I've tried the config based solutions I've found to no effect - eg Jest - Simple tests are slow

This is being run on my local windows machine from a terminal I'm fully starting and stopping for each test on my IDE.

tracer tong
  • 543
  • 6
  • 16

1 Answers1

0

OK so now I see the problem, I needed a try catch.

Can't quite believe none of the examples or docs I looked at as much as hinted this is necessary to handle something so basic.

test("should work", (done) => {
        clientSocket.on("redacted", (message: string) => {
            try {
                expect(2 + 2).toBe(56);
                //expect(message === "foobar").toEqual(true);
                done();
            } catch (err) {
                done(err)
            }

        });
        clientSocket.emit("redacted", "world");
    });
tracer tong
  • 543
  • 6
  • 16