My App
BoardKitBackend

Testing Utilities

In-memory adapters and mock auth guard for unit testing

The @hfu.digital/boardkit-nestjs package includes testing utilities that let you test without a database.

import {
    InMemoryBoardStorage,
    InMemoryAssetStorage,
    InMemoryEventLogStorage,
    MockAuthGuard,
} from '@hfu.digital/boardkit-nestjs';

InMemoryBoardStorage

A complete BoardStorage implementation backed by Map instances. Supports all operations including boards, pages, elements, members, and share links.

const storage = new InMemoryBoardStorage();

const board = await storage.createBoard({
    name: 'Test Board',
    ownerId: 'user-1',
});

const page = await storage.createPage(board.id, {
    name: 'Page 1',
    order: 0,
});

await storage.upsertElements(page.id, [
    { id: 'el-1', pageId: page.id, type: 'stroke', data: {}, zIndex: 0, createdBy: 'user-1' },
]);

const elements = await storage.getElements(page.id);

Key behaviors:

  • deleteBoard cascades to pages, elements, members, and share links
  • getPages returns pages sorted by order
  • upsertElements creates or updates based on element ID
  • Share link tokens are generated as token-{id} prefixed strings

InMemoryAssetStorage

An AssetStorage implementation that stores file buffers in memory.

const assetStorage = new InMemoryAssetStorage();

const asset = await assetStorage.upload('board-1', fileBuffer, {
    mimeType: 'image/png',
    sizeBytes: 1024,
    uploadedBy: 'user-1',
});

const url = await assetStorage.getUrl(asset.storageKey);
const usage = await assetStorage.getBoardUsage('board-1');

InMemoryEventLogStorage

An EventLogStorage implementation that stores events and snapshots in memory.

const eventLog = new InMemoryEventLogStorage();

const event = await eventLog.append({
    boardId: 'board-1',
    type: 'elementCreated',
    payload: { elementId: 'el-1' },
    userId: 'user-1',
    sequence: 1,
    timestamp: new Date().toISOString(),
});

const events = await eventLog.getEvents('board-1');
const seq = await eventLog.getLatestSequence('board-1');

MockAuthGuard

A BoardAuthGuard implementation that accepts all tokens and returns a test user.

const authGuard = new MockAuthGuard();

const user = await authGuard.validateRequest('any-token');
// { userId: 'test-user', displayName: 'Test User' }

Complete Test Setup

import { Test } from '@nestjs/testing';
import {
    BoardModule,
    InMemoryBoardStorage,
    InMemoryAssetStorage,
    InMemoryEventLogStorage,
    MockAuthGuard,
    BoardService,
} from '@hfu.digital/boardkit-nestjs';

describe('BoardService', () => {
    let boardService: BoardService;

    beforeEach(async () => {
        const module = await Test.createTestingModule({
            imports: [
                BoardModule.register({
                    storage: new InMemoryBoardStorage(),
                    assetStorage: new InMemoryAssetStorage(),
                    eventLogStorage: new InMemoryEventLogStorage(),
                    authGuard: new MockAuthGuard(),
                }),
            ],
        }).compile();

        boardService = module.get(BoardService);
    });

    it('should create a board with a default page', async () => {
        const board = await boardService.createBoard({
            name: 'Test',
            ownerId: 'user-1',
        });
        const pages = await boardService.getPages(board.id);
        expect(pages).toHaveLength(1);
        expect(pages[0].name).toBe('Page 1');
    });
});

On this page