@dudousxd/nestjs-media-testing
In-memory stores, storageOf(), and reusable conformance suites for disks and stores.
Test doubles and contract suites. The disk for tests is @nestjs/storage's InMemoryDisk; this package adds the media side.
In-memory doubles
import { InMemoryDisk, StorageModule } from '@nestjs/storage';
import { InMemoryMediaStore, InMemoryUploadSessionStore } from '@dudousxd/nestjs-media-testing';
StorageModule.forRoot({ disks: { mem: new InMemoryDisk() } });
MediaModule.forRoot({
store: new InMemoryMediaStore(),
uploadSessions: new InMemoryUploadSessionStore(),
});To build the core engines by hand, without Nest, storageOf() gives you a Storage over plain disks:
import { storageOf } from '@dudousxd/nestjs-media-testing';
const disk = new InMemoryDisk();
const library = new MediaLibrary({ storage: storageOf({ local: disk }), store: new InMemoryMediaStore() });
await library.attach({ /* ... */ });
expect(disk.keys()).toEqual(['Post/1/gallery/<id>/photo.png']);Conformance suites
import {
describeMediaDiskConformance,
runMediaStoreConformance,
} from '@dudousxd/nestjs-media-testing';
describeMediaDiskConformance('MyDisk', () => new MyDisk(/* … */));
describeMediaDiskConformance('S3Disk on MinIO', () => new S3Disk(options), {
multipart: () => s3Multipart(options), // a function: built after beforeAll started the container
});
runMediaStoreConformance('MyStore', () => new MyStore(/* … */));describeMediaDiskConformance(name, makeDisk, { multipart?, prefix? }) checks what nestjs-media relies on from a StorageDisk (@nestjs/storage tests its own disks): streamed writes of unknown length and the size put() reports, contentLength mismatches writing nothing, inclusive and suffix ranges, StorageFileNotFoundError, batch delete, flat key-ordered listing folded into folders and paged without repeats, and streamed copies. With multipart, it also checks the side-car against the disk: parts assembled at a key the disk reads back with its content type, abort, presigned part URLs, and listFolders agreeing with the fold. Every test writes under its own random prefix, so a shared bucket is fine.
runMediaStoreConformance exercises the MediaStore contract — ordering, nextOrder, owner filtering, and more.
The repo runs the disk suite against InMemoryDisk, LocalDisk, and S3Disk + s3Multipart() on MinIO; the four ORM stores run the store suite (in-memory, sqlite, and real Postgres).
See Custom store. A custom disk subclasses @nestjs/storage's StorageDisk (docs).