Testing utilities for Fedify applications.
This package provides mock implementations of the Federation and Context
interfaces to facilitate unit testing of federated applications built with
Fedify.
deno add @fedify/testingnpm install @fedify/testingpnpm add @fedify/testingyarn add @fedify/testingThe MockFederation class provides a mock implementation of the Federation
interface for unit testing:
import { MockFederation } from "@fedify/testing";
import { Create } from "@fedify/vocab";
// Create a mock federation
const federation = new MockFederation<{ userId: string }>();
// Set up inbox listeners
federation
.setInboxListeners("/users/{identifier}/inbox")
.on(Create, async (ctx, activity) => {
console.log("Received:", activity);
});
// Simulate receiving an activity
await federation.receiveActivity(createActivity);
// Check sent activities
const sent = federation.sentActivities;
console.log(sent[0].activity);The MockContext class provides a mock implementation of the Context
interface:
import { MockContext } from "@fedify/testing";
// Create a mock context
const context = new MockContext({
url: new URL("https://example.com"),
data: { userId: "test-user" },
federation: mockFederation
});
// Send an activity
await context.sendActivity(
{ identifier: "alice" },
recipient,
activity
);
// Check sent activities
const sent = context.getSentActivities();
console.log(sent[0].activity);The package also exports helper functions for creating various context types:
createContext(): Creates a basic contextcreateRequestContext(): Creates a request contextcreateInboxContext(): Creates an inbox context
The package provides reusable conformance suites for Fedify storage adapters.
Use testKvStore() to check a KvStore implementation and
testMessageQueue() to check a MessageQueue implementation:
import { test } from "@fedify/fixture";
import { testKvStore, testMessageQueue } from "@fedify/testing";
test("MyKvStore", () =>
testKvStore(
() => new MyKvStore(),
async ({ store }) => await store.close(),
));
test("MyMessageQueue", () =>
testMessageQueue(
() => new MyMessageQueue(),
async ({ mq1, mq2, controller }) => {
controller.abort();
await mq1.close();
await mq2.close();
},
));- Track sent activities with metadata
- Simulate activity reception
- Configure custom URI templates
- Test queue-based activity processing
- Mock document loaders and context loaders