95 lines
3.6 KiB
TypeScript
95 lines
3.6 KiB
TypeScript
import { describe, expect, test, vi } from "vitest";
|
|
import { queueNames } from "./names.js";
|
|
|
|
// Track every BullMQ primitive constructed through the factory wrappers so we
|
|
// can assert the thin pass-through layer forwards arguments verbatim. Declared
|
|
// via vi.hoisted so it is initialised before the hoisted vi.mock factory runs.
|
|
const { constructions } = vi.hoisted(() => ({
|
|
constructions: [] as { cls: string; args: unknown[] }[],
|
|
}));
|
|
|
|
vi.mock("bullmq", () => {
|
|
class Queue {
|
|
constructor(...args: unknown[]) {
|
|
constructions.push({ cls: "Queue", args });
|
|
}
|
|
}
|
|
class QueueEvents {
|
|
constructor(...args: unknown[]) {
|
|
constructions.push({ cls: "QueueEvents", args });
|
|
}
|
|
}
|
|
class Worker {
|
|
constructor(...args: unknown[]) {
|
|
constructions.push({ cls: "Worker", args });
|
|
}
|
|
}
|
|
return { Queue, QueueEvents, Worker };
|
|
});
|
|
|
|
// Imported after the hoisted mock so the ESM factory pulls the mocked bullmq.
|
|
import { createQueue, createQueueEvents, createWorker } from "./factory.js";
|
|
|
|
describe("ESM runtime factory (factory.ts -> runtime.ts)", () => {
|
|
test("createQueue merges defaultJobOptions into the BullMQ Queue options", () => {
|
|
const options = { connection: { host: "localhost", port: 6379 } } as never;
|
|
const queue = createQueue(queueNames.email, options);
|
|
|
|
expect(queue).toBeDefined();
|
|
const last = constructions.at(-1);
|
|
expect(last?.cls).toBe("Queue");
|
|
expect(last?.args[0]).toBe(queueNames.email);
|
|
expect(last?.args[1]).toMatchObject({
|
|
defaultJobOptions: { attempts: 3 },
|
|
connection: { host: "localhost", port: 6379 },
|
|
});
|
|
});
|
|
|
|
test("createQueueEvents forwards the queue name (options optional)", () => {
|
|
const queueEvents = createQueueEvents(queueNames.notifications);
|
|
|
|
expect(queueEvents).toBeDefined();
|
|
const last = constructions.at(-1);
|
|
expect(last?.cls).toBe("QueueEvents");
|
|
expect(last?.args[0]).toBe(queueNames.notifications);
|
|
});
|
|
|
|
test("createWorker forwards the queue name, processor and options", () => {
|
|
const processor = async () => "done";
|
|
const worker = createWorker(queueNames.fileScan, processor as never, { connection: {} } as never);
|
|
|
|
expect(worker).toBeDefined();
|
|
const last = constructions.at(-1);
|
|
expect(last?.cls).toBe("Worker");
|
|
expect(last?.args[0]).toBe(queueNames.fileScan);
|
|
expect(last?.args[1]).toBe(processor);
|
|
});
|
|
});
|
|
|
|
describe("CJS runtime entry (runtime.cjs)", () => {
|
|
// The CJS entry is loaded natively (Node `require`), so vi.mock("bullmq")
|
|
// does not intercept it. We assert the module-level export contract without
|
|
// invoking the factories — calling them would construct a real BullMQ
|
|
// primitive and open a Redis connection the test cannot satisfy.
|
|
test("exports the same factory surface and the retry defaults", async () => {
|
|
const mod = (await import("./runtime.cjs")) as Record<string, unknown>;
|
|
const cjs = ((mod.default as Record<string, unknown> | undefined) ?? mod) as {
|
|
createQueue: unknown;
|
|
createQueueEvents: unknown;
|
|
createWorker: unknown;
|
|
defaultJobOptions: {
|
|
attempts: number;
|
|
removeOnComplete: number;
|
|
removeOnFail: number;
|
|
backoff: { delay: number; type: string };
|
|
};
|
|
};
|
|
|
|
expect(typeof cjs.createQueue).toBe("function");
|
|
expect(typeof cjs.createQueueEvents).toBe("function");
|
|
expect(typeof cjs.createWorker).toBe("function");
|
|
expect(cjs.defaultJobOptions).toMatchObject({ attempts: 3, removeOnComplete: 100, removeOnFail: 250 });
|
|
expect(cjs.defaultJobOptions.backoff).toMatchObject({ delay: 1000, type: "exponential" });
|
|
});
|
|
});
|