RTP Midi Transport Testing - JoueBien/rtp-midi-transport GitHub Wiki

Testing a client connection

You can set up a client in a test by using setupClient. setupClient does all the hard work of setting up a client in ViTest and cleaning it back up after the test is done for you.

Spies are exposed, so you can to wait for the client to have fully connected to the server before you start the rest of your test.

import { setupClient } from "@joue-bien/rtp-midi-transport/testing";

describe("testing", () => {
  const {
    getCurrentClient,
    clientStartUpOnSpy,
    clientOnSpy,
    connectAndWaitForClockDone,
  } = setupClient();

  it("does a thing", () => {
    const client = getCurrentClient();
    await client.connect();
  });
});

Note: The underlying transport uses setTimeout and clearTimeout internally. If you use fake timers you will need to run time forward for the internal setTimeout's to resolve.

Testing a server connection

You can set up a server in a test by using setupServer. setupServer does all the hard work of setting up a server in ViTest and cleaning it back up after the test is done for you.

The server will automatically accept any clients that tries to connect to it.

Spies are exposed, so you can to wait for certain events to have happened.

describe("testing", () => {
  const { getCurrentServer, serverOnSpy, serverCleanUp } = setupServer();

  it("does a thing", () => {
    const server = getCurrentServer();
    // Optionally remove auto accept client handler on server.
    serverCleanUp.listenAddListenersForAutoOkCleanUp();

    await server.listen();
  });
});

Using sendAndWaitFor