Registry / testing / jest-redis

jest-redis

JSON →
library0.1.0jsnpmunverified

A Jest preset that automatically starts an in-memory Redis server for testing using redis-memory-server. As of v0.1.0, it sets up a global Redis instance, provides process.env.REDIS_URL or global.__REDIS_URL__, and supports custom server options, shared databases across workers, and Jest watch mode. Key differentiator: seamless integration with Jest's test lifecycle, eliminating manual server setup/teardown. Release cadence: initial release, no updates since June 2019.

npm install jest-redis
INSTALL
IMPORT
SIG · JEST-REDIS
J
jest-redis
testingjavascriptv0.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

jest.config.js
module.exports = { preset: 'jest-redis' }
module.exports = { preset: 'jest-redis', ...require('jest-redis') }
The preset is configured in jest.config.js, not imported in test files. The preset handles global setup and teardown.
global.__REDIS_URL__
const client = new Redis(global.__REDIS_URL__)
const client = new Redis(process.env.REDIS_URL)
global.__REDIS_URL__ is preferred as it works with useSharedDBForAllJestWorkers. process.env.REDIS_URL is the default but may not work with shared DB.
jest-redis.config.js
module.exports = { redisMemoryServerOptions: ..., useSharedDBForAllJestWorkers: false, redisURLEnvName: 'REDIS_URL' }
// No wrong pattern, but configuration is optional.
This file is optional; if absent, defaults are used. Must be placed in project root.

Configures Jest to use the jest-redis preset, which starts an in-memory Redis server, and demonstrates basic Redis operations in a test using the global Redis URL.

// jest.config.js module.exports = { preset: 'jest-redis', watchPathIgnorePatterns: ['redisGlobals.json'] } // __tests__/redis.test.js const Redis = require('ioredis'); describe('Redis', () => { let client; beforeAll(async () => { client = new Redis(global.__REDIS_URL__); await client.connect(); }); afterAll(async () => { await client.disconnect(); }); it('should set and get value', async () => { await client.set('test', 'value'); const val = await client.get('test'); expect(val).toBe('value'); }); });
Debug
Known issues
gotchaJest watch mode generates a redisGlobals.json file that can cause unwanted infinite re-runs if not ignored.
fix
Add 'redisGlobals' to watchPathIgnorePatterns in jest.config.js.
affects: >=0.1.0
gotchaprocess.env.REDIS_URL may not reflect the correct URL when useSharedDBForAllJestWorkers is enabled; global.__REDIS_URL__ is always safe.
fix
Use global.__REDIS_URL__ instead of process.env.REDIS_URL.
affects: >=0.1.0
gotchaThe redis-memory-server binary download can fail in CI environments without network access or proper caching.
fix
Set REDIS_MEMORY_SERVER_DOWNLOAD_DIR environment variable or binary.downloadDir to a cached location.
affects: >=0.1.0
gotchaThe preset does not auto-start Redis if redisMemoryServerOptions.autoStart is set to false; the server must be started manually.
fix
Either set autoStart: true or start the Redis server manually via global.__REDIS_SERVER__.start() in setupFiles.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot find module 'redis-memory-server'
redis-memory-server is a peer dependency and was not installed.
fix
Run: npm install --save-dev redis-memory-server (or yarn add --dev redis-memory-server)
TypeError: Redis is not a constructor
Using ioredis v5+ which changed to class-based export; new Redis() should work but 'new Redis' is undefined if import is wrong.
fix
Ensure ioredis is installed: npm install ioredis; use: const Redis = require('ioredis'); const client = new Redis(global.__REDIS_URL__);
Jest: 'preset' must be a string or an object
preset is defined in jest.config.js as something other than a string, e.g., an object.
fix
Set module.exports = { preset: 'jest-redis' }
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
redis-memory-serverrequiredRequired peer dependency that provides the actual in-memory Redis server binary.
Agent activity
6 hits · last 30 days
node
6
Resources
jest-redis — npm install jest-redis · libregistry