import { describe, test, beforeEach, expect, afterEach } from "vitest";
import type { StartedRedisContainer } from "@testcontainers/redis";
import { RedisContainer } from "@testcontainers/redis";
import Redis from "ioredis";
import { SuperJsonSerde, TimeSpan, RedisPubSubEventBusAdapter, eventBusTestSuite, serde } from "@daiso-tech/core";
const timeout = TimeSpan.fromMinutes(2);
describe("class: EventBus", () => {
let dispatcherClient: Redis;
let listenerClient: Redis;
let startedContainer: StartedRedisContainer;
const serde = new SuperJsonSerde();
beforeEach(async () => {
startedContainer = await new RedisContainer().start();
dispatcherClient = new Redis(startedContainer.getConnectionUrl());
listenerClient = new Redis(startedContainer.getConnectionUrl());
}, timeout.toMilliseconds());
afterEach(async () => {
await dispatcherClient.quit();
await listenerClient.quit();
await startedContainer.stop();
}, timeout.toMilliseconds());
eventBusTestSuite({
createEventBusA: () =>
new EventBus(
new RedisPubSubEventBusAdapter({
dispatcherClient,
listenerClient,
serde,
}),
{ rootGroup: "@a" }
),
createEventBusB: () =>
new EventBus(
new RedisPubSubEventBusAdapter({
dispatcherClient,
listenerClient,
serde,
}),
{ rootGroup: "@b" }
),
serde,
test,
beforeEach,
expect,
describe,
});
});
The eventBusTestSuite function simplifies the process of testing your custom implementation of IEventBus with vitest.