Cache MongoDB binaries in CI
masterTo speed up CI pipelines, you can cache the MongoDB binaries by adding the following path to your CI provider's cache list:
./node_modules/.cache/mongodb-memory-server/mongodb-binaries
repository·master·Indexed 20 days ago
https://github.com/shelfio/jest-mongodbA Jest preset that runs a MongoDB memory server for testing, allowing integration tests against a real, isolated MongoDB instance. It provides a custom TestEnvironment (MongoEnvironment) to automate the lifecycle of the server and injects connection details via global variables like __MONGO_URI__ and __MONGO_DB_NAME__. Supports shared or separate databases for Jest workers and replica set configurations via jest-mongodb-config.js.
To speed up CI pipelines, you can cache the MongoDB binaries by adding the following path to your CI provider's cache list:
./node_modules/.cache/mongodb-memory-server/mongodb-binaries
Install the package as a development dependency using yarn. Note that mongodb is a required peer dependency and must be installed in your project separately.
$ yarn add @shelf/jest-mongodb --devCreate a jest.config.js file and set the preset property to @shelf/jest-mongodb.
Important: If you are using a custom jest.config.js, ensure you remove the testEnvironment property, as it will conflict with the preset.
module.exports = {
preset: '@shelf/jest-mongodb',
};jest-mongodb-config.js file to configure the underlying mongodb-memory-server. This file accepts options compatible with mongodb-memory-server.The MongoEnvironment class (exported as module.exports) is a custom Jest TestEnvironment that automates the lifecycle of a MongoDB instance for your tests.
globalConfig.json (located in the Jest root directory) to determine whether to start a MongoMemoryServer or a MongoMemoryReplSet.mongoUri is not already provided in the global config) and injects two key global variables into the test context:global.__MONGO_URI__: The connection string for the running MongoDB instance.global.__MONGO_DB_NAME__: The name of the database to use (defaults to a random UUID if not specified in globalConfig.json).The package creates a globalConfig.json file in the project root. When running Jest with the --watch flag, changes to this file can trigger an infinite loop. To prevent this, add globalConfig to your watchPathIgnorePatterns in jest.config.js.
// jest.config.js
module.exports = {
watchPathIgnorePatterns: ['globalConfig'],
};To ensure each Jest worker uses its own separate database, set useSharedDBForAllJestWorkers: false in jest-mongodb-config.js. Note that when using this option, the process.env variable is not created.
module.exports = {
mongodbMemoryServerOptions: {
binary: {
skipMD5: true,
},
autoStart: false,
instance: {},
},
useSharedDBForAllJestWorkers: false,
};By default, the library uses process.env.MONGO_URL. To use a different environment variable name, set the mongoURLEnvName field in jest-mongodb-config.js.
module.exports = {
mongodbMemoryServerOptions: {
binary: {
version: '4.0.3',
skipMD5: true,
},
instance: {},
autoStart: false,
},
mongoURLEnvName: 'MONGODB_URI',
};To use the same database instance across all Jest workers, specify a dbName within the instance object in jest-mongodb-config.js.
module.exports = {
mongodbMemoryServerOptions: {
binary: {
version: '4.0.3',
skipMD5: true,
},
instance: {
dbName: 'jest',
},
autoStart: false,
},
};To run MongoDB as a replica set, add a replSet object to mongodbMemoryServerOptions and specify the count and storageEngine fields.
module.exports = {
mongodbMemoryServerOptions: {
binary: {
skipMD5: true,
},
autoStart: false,
instance: {},
replSet: {
count: 3,
storageEngine: 'wiredTiger',
},
},
};While the library sets process.env.MONGO_URL, it is preferable to use global.__MONGO_URI__ when connecting your MongoClient. This ensures compatibility when useSharedDBForAllJestWorkers: false is configured.
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {});
db = await connection.db();
});
afterAll(async () => {
await connection.close();
});
});The Config interface defines the options available for setting up the jest-mongodb environment. This configuration is typically used to control how the in-memory server is instantiated and how workers interact with the database.
Available configuration keys:
mongodbMemoryServerOptions: Options passed directly to mongodb-memory-server (supports both MongoMemoryReplSetOpts and MongoMemoryServerOpts).mongoURLEnvName: The name of the environment variable that will hold the MongoDB URI. Defaults to 'MONGO_URL'.useSharedDBForAllJestWorkers: Determines if all Jest workers should share the same database instance. Defaults to true.export interface Config {
mongodbMemoryServerOptions?: MongoMemoryReplSetOpts | MongoMemoryServerOpts;
/**
* @default 'MONGO_URL'
*/
mongoURLEnvName?: string;
/**
* @default true
*/
useSharedDBForAllJestWorkers?: boolean;
}