Created
February 21, 2024 17:12
-
-
Save rafa-thayto/b235cf0849ca401aa209b4031bf93e9e to your computer and use it in GitHub Desktop.
Simple nodejs testcontainer implementation with Redis and typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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