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