Skip to content

Instantly share code, notes, and snippets.

@rafa-thayto
Created February 21, 2024 17:12
Show Gist options
  • Select an option

  • Save rafa-thayto/b235cf0849ca401aa209b4031bf93e9e to your computer and use it in GitHub Desktop.

Select an option

Save rafa-thayto/b235cf0849ca401aa209b4031bf93e9e to your computer and use it in GitHub Desktop.
Simple nodejs testcontainer implementation with Redis and typescript
import { RedisClientType, createClient } from 'redis';
import {
GenericContainer,
Network,
RandomUuid,
StartedTestContainer,
StoppedTestContainer,
} from 'testcontainers';
describe('Redis', () => {
let startedContainer: StartedTestContainer;
let stoppedContainer: StoppedTestContainer;
let redisClient: RedisClientType;
beforeAll(async () => {
const REDIS_PORT = 6379;
const network = await new Network(new RandomUuid()).start();
const container = new GenericContainer('redis/redis-stack-server')
.withExposedPorts(REDIS_PORT)
.withNetwork(network)
.withNetworkMode(network.getName());
startedContainer = await container.start();
const host = startedContainer.getHost();
const port = startedContainer.getMappedPort(REDIS_PORT);
const redisUrl = `redis://${host}:${port}`;
redisClient = createClient({
url: redisUrl,
});
await redisClient.connect();
});
afterAll(async () => {
await redisClient.quit();
stoppedContainer = await startedContainer.stop();
console.log('container', stoppedContainer);
});
it('works', async () => {
await redisClient.set('key', 'val');
expect(await redisClient.get('key')).toBe('val');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment