Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@trendyol/jest-testcontainers
Advanced tools
Readme
Jest preset for running docker containers with your tests. Primary purpose is to make it possible to use any database in integration tests. Since it uses docker images, custom database images with different plugins/configurations can be used in the integration tests. Using testcontainers-node under the hood. Inspired by @shelf/jest-mongodb.
Docker should be installed on your system. If running inside a CI pipeline, see FAQ.md.
npm install --save-dev @trendyol/jest-testcontainers
On your jest.config.js
add the project as the preset.
module.exports = {
preset: '@trendyol/jest-testcontainers'
};
Create a file called jest-testcontainers-config.js
and put it to root of your project, where you run npm test. If you would like to put this file somewhere else, you can use the JEST_TESTCONTAINERS_CONFIG_PATH environment variable to define a relative or absolute path. A sample configuration file;
module.exports = {
redis: {
image: 'redis',
tag: 'alpine3.12',
ports: [6379],
env: {
EXAMPLE: 'env',
},
wait: {
type: 'text',
text: 'Ready to accept connections'
}
},
// more: {
// image: 'any-docker-image', // postgresql, mongodb, neo4j etc.
// ports: [1234, 4567], // ports to make accessible in tests
// },
};
Every containers IP and Port info is registered to global variables to be used by your tests.
const redis = require('redis');
const { promisify } = require('util');
describe('testcontainers example suite', () => {
let redisClient;
beforeAll(() => {
const redisConnectionURI = `redis://${global.__TESTCONTAINERS_REDIS_IP__}:${global.__TESTCONTAINERS_REDIS_PORT_6379__}`;
redisClient = redis.createClient(redisConnectionURI);
// if you have declared multiple containers, they will be available to access as well. e.g.
// `global.__TESTCONTAINERS_${CONFIG_KEY}_IP__`
// `global.__TESTCONTAINERS_${CONFIG_KEY}_PORT_${CONFIG_PORT}__`
});
afterAll(() => {
redisClient.quit();
});
it('write should be ok', async () => {
// Arrange
const setAsync = promisify(redisClient.set).bind(redisClient);
// Act
const setResult = await setAsync('test', 73);
// Assert
expect(setResult).toEqual('OK');
});
});
Detailed documentation of the jest-testcontainers-config.js
can be found at DOC.md.
npm run build && npm run example:redis
command to run this example locally.npm run build && npm run example:redis-typescript
to run this example locally.npm run build && npm run example:docker-compose
to run this example locally.Starting with version 2.0.0 containers will not be stopped if Jest is started in watch mode. This greatly improves development productivity if your containers have a slow startup time (ex. Elasticsearch). This comes with the price that you have to be mindful that containers will be reused between watch test executions and you need to do proper cleanup in your after hooks.
If you want to disable this behavior you can set the JEST_TESTCONTAINERS_RESTART_ON_WATCH
environment variable.
Wondering what will happen when those containers are not stopped when Jest is exited - testcontainer's ryuk will take care of them.
This project is licensed under the MIT License
FAQs
Jest preset for starting docker containers that stay up whilist your tests run.
The npm package @trendyol/jest-testcontainers receives a total of 7,548 weekly downloads. As such, @trendyol/jest-testcontainers popularity was classified as popular.
We found that @trendyol/jest-testcontainers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.