Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

@fedify/testing

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.

Installation

deno add @fedify/testing
npm install @fedify/testing
pnpm add @fedify/testing
yarn add @fedify/testing

Usage

MockFederation

The 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);

MockContext

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);

Helper functions

The package also exports helper functions for creating various context types:

  • createContext(): Creates a basic context
  • createRequestContext(): Creates a request context
  • createInboxContext(): Creates an inbox context

Store conformance tests

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();
    },
  ));

Features

  • Track sent activities with metadata
  • Simulate activity reception
  • Configure custom URI templates
  • Test queue-based activity processing
  • Mock document loaders and context loaders