Skip to Content
dimah-s3v2.8.1
Plugins

Testing

Test hooks and plugins end to end on an in-memory S3, with a client wired to the handler, using @dimah-s3/server/testing.

@dimah-s3/server/testing runs a real dimahS3() instance on an S3 that lives in memory, with the browser client calling its handler in process. Use it to test your hooks, guards, and plugins end to end, with no bucket and no server. It runs on Node.js, in any test runner.

audit.test.ts
import { route } from "@dimah-s3/server";
import { createTestInstance } from "@dimah-s3/server/testing";
import { expect, it } from "vitest";
import { audit } from "./audit";
import { auditClient } from "./audit/client";

it("records confirmed uploads", async () => {
  const { s3, client, upload } = createTestInstance({
    plugins: [audit()],
    clientPlugins: [auditClient()],
    routes: { invoices: route({ upload: true, delete: true }) },
  });

  const { key } = await upload({
    route: "invoices",
    name: "march.pdf",
    body: "pdf",
  });
  await client.delete({ route: "invoices", key });

  const { events } = await client.audit.listEvents();
  expect(events.map((event) => event.type)).toEqual(["delete", "upload"]);
  await expect(s3.audit.events()).resolves.toHaveLength(2);
});

Test instance

createTestInstance(config) takes the dimahS3() options without client, and clientPlugins for the client. bucket defaults to "test-bucket", and secret to a test value.

PropertyDescription
s3The instance: s3.api, s3.put, and each plugin context, typed by your plugins and routes
clientThe @dimah-s3/core browser client with your client plugins, calling s3.handler
storageThe in-memory S3. Read storage.get(key), or storage.seed(key, body) an object
uploadUploads one file the way the browser does

Every request goes through the router, the guards, and the plugin endpoints, the same as in production. Pass headers per call to act as a user:

await client.catalog({ headers: { authorization: "Bearer test" } });
await upload(
  { route: "invoices", name: "a.pdf", body: "a" },
  { headers: { "x-user": "1" } },
);

Uploads

upload(input, options?) presigns, stores the bytes in storage with the presigned headers or form fields, and confirms. Your upload hooks run as they do in production, and it resolves with the confirmed object or rejects with the step's APIError. It sends the file's SHA-256, so it works on a route that requires checksums.

PropTypeDefaultDescription
routestringrequiredThe route to upload to
namestringrequiredFile name
bodystring | Uint8Array | ArrayBufferrequiredThe bytes
typestring—MIME type the client declares
sizenumberthe body'sSize the client declares. Another value tests a size mismatch
clientMetadataRecord<string, string>—Client extras for the hooks
await expect(
  upload({ route: "invoices", name: "a.pdf", body: "hello", size: 4 }),
).rejects.toMatchObject({ code: "UPLOAD_SIZE_MISMATCH" });

Parts

Build the instance yourself when you need another setup:

import { dimahS3 } from "@dimah-s3/server";
import {
  createMemoryS3,
  createTestClient,
  uploadTestFile,
} from "@dimah-s3/server/testing";

const storage = createMemoryS3({ bucket: "files" });
const s3 = dimahS3({ client: storage.client, bucket: "files", secret, routes });
const client = createTestClient(s3, { plugins: [auditClient()] });

await uploadTestFile(
  { client, storage },
  { route: "uploads", name: "a.txt", body: "hi" },
);
ExportDescription
createMemoryS3({ bucket? })An S3Client that keeps objects and multipart uploads in memory, with S3's checksum, metadata, If-Match, and range rules
createTestClient(s3, options?)createS3Client with fetch routed to s3.handler. Takes plugins, basePath, and headers
uploadTestFile({ client, storage }, input)The upload helper above, for an instance you built

The memory client signs presigned URLs but never fetches them. Spy on storage.client.send to inject an S3 failure.

Frequently asked questions

On this page