nexus
Testing

Testing Nexus Applications

Test application code that consumes Nexus through a NexusInstance.

@nexus-js/testing provides user-level unit test utilities for application code that consumes Nexus through a NexusInstance.

Use createMockNexus() when a component, hook, service-consuming module, or cleanup path needs an injectable Nexus instance without starting real runtime contexts, endpoints, transports, or adapters.

Install

pnpm add -D @nexus-js/testing

Main Path

import { createMockNexus } from "@nexus-js/testing";
import { UserToken, type UserService } from "../shared/user";

const mock = createMockNexus();

const userService: UserService = {
  async getUser(id) {
    return { id, name: "Ada" };
  },
};

mock.service(UserToken, userService);

const connection = await mock.nexus.connect({
  target: { context: "background" },
});
const user = connection.get(UserToken);

await expect(user.getUser("u1")).resolves.toEqual({ id: "u1", name: "Ada" });

React Components

import { NexusProvider } from "@nexus-js/react";
import { createMockNexus } from "@nexus-js/testing";

const mock = createMockNexus();
mock.service(UserToken, userService);

const wrapper = ({ children }: { children: React.ReactNode }) => (
  <NexusProvider nexus={mock.nexus}>{children}</NexusProvider>
);

What This Covers

  • application code that calls nexus.connect(...) and gets services from connections
  • React components and hooks that receive Nexus through NexusProvider
  • setup code that calls configure(...)
  • cleanup code that calls release(...)
  • error paths for connection or service acquisition failures
  • assertions against recorded calls

What This Does Not Cover

  • endpoint or transport behavior
  • connection handshakes
  • adapter auth, origin, socket, tab, frame, or runtime-port behavior
  • real disconnect, reconnect, reload, or daemon restart timing
  • connection lifecycle, collection snapshots, or identity subscriptions

Use adapter or integration tests for those behaviors.

Edit this page on GitHub