Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ioredis-mock

stipsan2.1mMIT8.9.0TypeScript support: definitely-typed

This library emulates ioredis by performing all operations in-memory.

fakeredis, in-memory, ioredis, ioredis-mock, mock-ioredis, mock-redis, redis, redis fake, redis inmemory, redis js, redis memory, redis mock, redis standalone, redis test, redis testing, redis-js, redis-mock, redisjs

readme

ioredis-mock · npm npm version Redis Compatibility: 66% semantic-release

This library emulates ioredis by performing all operations in-memory. The best way to do integration testing against redis and ioredis is on a real redis-server instance. However, there are cases where mocking the redis-server is a better option.

Cases like:

  • Your workflow already use a local redis-server instance for the dev server.
  • You're on a platform without an official redis release, that's even worse than using an emulator.
  • You're running tests on a CI, setting it up is complicated. If you combine it with CI that also run selenium acceptance testing it's even more complicated, as two redis-server instances on the same CI build is hard.
  • The GitHub repo have bots that run the testing suite and is limited through npm package.json install scripts and can't fire up servers. (Having Renovatebot notifying you when a new release of ioredis is out and wether your code breaks or not is awesome).

Check the compatibility table for supported redis commands.

Usage (try it in your browser with RunKit)

const Redis = require('ioredis-mock')
const redis = new Redis({
  // `options.data` does not exist in `ioredis`, only `ioredis-mock`
  data: {
    user_next: '3',
    emails: {
      'clark@daily.planet': '1',
      'bruce@wayne.enterprises': '2',
    },
    'user:1': { id: '1', username: 'superman', email: 'clark@daily.planet' },
    'user:2': { id: '2', username: 'batman', email: 'bruce@wayne.enterprises' },
  },
})
// Basically use it just like ioredis

Browser usage (Experimental)

There's a browser build available. You can import it directly (import Redis from 'ioredis-mock/browser.js'), or use it on unpkg.com:

import Redis from 'https://unpkg.com/ioredis-mock'

const redis = new Redis()
redis.set('foo', 'bar')
console.log(await redis.get('foo'))

Breaking Changes

from v7 to v8

ioredis@v4 support dropped

ioredis@v5 is the new baseline. Stay on ioredis-mock@v7 until you're ready to upgrade to ioredis@v5.

PromiseContainer has been removed.

Support for third-party Promise libraries is dropped. The native Promise library will always be used.

from v6 to v7

createConnectedClient is removed

Replace it with .duplicate() or use another new Redis instance.

Dropped support for Node v10

It's been EOL since Apr, 2021 and it's recommended to upgrade to v14.x LTS.

ioredis-mock/jest.js is removed

ioredis-mock is no longer doing a import { Command } from 'ioredis' internally, it's now doing a direct import import Command from 'ioredis/built/command' and thus the jest.js workaround is no longer needed:

-jest.mock('ioredis', () => require('ioredis-mock/jest'))
+jest.mock('ioredis', () => require('ioredis-mock'))

from v5 to v6

Before v6, each instance of ioredis-mock lived in isolation:

const Redis = require('ioredis-mock')
const redis1 = new Redis()
const redis2 = new Redis()

await redis1.set('foo', 'bar')
console.log(await redis1.get('foo'), await redis2.get('foo')) // 'bar', null

In v6 the internals were rewritten to behave more like real life redis, if the host and port is the same, the context is now shared:

const Redis = require('ioredis-mock')
const redis1 = new Redis()
const redis2 = new Redis()
const redis3 = new Redis(6380) // 6379 is the default port

await redis1.set('foo', 'bar')
console.log(
  await redis1.get('foo'), // 'bar'
  await redis2.get('foo'), // 'bar'
  await redis3.get('foo') // null
)

And since ioredis-mock now persist data between instances, you'll likely need to run flushall between testing suites:

const Redis = require('ioredis-mock')

afterEach(done => {
  new Redis().flushall().then(() => done())
})

Pub/Sub channels

We also support redis publish/subscribe channels. Like ioredis, you need two clients:

const Redis = require('ioredis-mock')
const redisPub = new Redis()
const redisSub = new Redis()

redisSub.on('message', (channel, message) => {
  console.log(`Received ${message} from ${channel}`)
})
redisSub.subscribe('emails')
redisPub.publish('emails', 'clark@daily.planet')

Lua scripting

You can use the defineCommand to define custom commands using lua or eval to directly execute lua code.

In order to create custom commands, using lua scripting, ioredis exposes the defineCommand method.

You could define a custom command multiply which accepts one key and one argument. A redis key, where you can get the multiplicand, and an argument which will be the multiplicator:

const Redis = require('ioredis-mock')
const redis = new Redis({ data: { k1: 5 } })
const commandDefinition = {
  numberOfKeys: 1,
  lua: 'return redis.call("GET", KEYS[1]) * ARGV[1]',
}
redis.defineCommand('multiply', commandDefinition) // defineCommand(name, definition)
// now we can call our brand new multiply command as an ordinary command
redis.multiply('k1', 10).then(result => {
  expect(result).toBe(5 * 10)
})

You can also achieve the same effect by using the eval command:

const Redis = require('ioredis-mock')
const redis = new Redis({ data: { k1: 5 } })
const result = redis.eval(`return redis.call("GET", "k1") * 10`)
expect(result).toBe(5 * 10)

note we are calling the ordinary redis GET command by using the global redis object's call method.

As a difference from ioredis we currently don't support:

  • dynamic key number by passing the number of keys as the first argument of the command.
  • automatic definition of the custom command buffer companion (i.e. for the custom command multiply the multiplyBuffer which returns values using Buffer.from(...))
  • the evalsha command
  • the script command

Cluster(Experimental)

Work on Cluster support has started, the current implementation is minimal and PRs welcome #359

const Redis = require('ioredis-mock')

const cluster = new Redis.Cluster(['redis://localhost:7001'])
const nodes = cluster.nodes()
expect(nodes.length).toEqual(1)

Roadmap

You can check the roadmap project page, and the compat table, to see how close we are to feature parity with ioredis.

I need a feature not listed here

Just create an issue and tell us all about it or submit a PR with it! 😄

changelog

📓 Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

8.9.0 (2023-09-29)

Features

Bug Fixes

  • deps: update redis docker tag to v7.2.0 (#1308) (a5a9914)
  • deps: update redis docker tag to v7.2.1 (#1317) (06e2968)
  • rpoplpush should rotate the list when source and destintation are the same (#1321) (897af68)
  • xread fixes (#1318) (3978302)

8.8.3 (2023-08-15)

Bug Fixes

  • deps: update dependency esbuild to ^0.19.2 (#1306) (c346711)

8.8.2 (2023-08-13)

Bug Fixes

8.8.1 (2023-07-14)

Bug Fixes

  • commands: add lmoveBuffer (9cb31eb)
  • deps: update redis docker tag to v7.0.12 (#1297) (85726d0)

8.8.0 (2023-07-14)

Features

Bug Fixes

  • deps: update dependency semver to v7.5.2 [security] (#1286) (23b0093)

8.7.0 (2023-04-18)

Features

Bug Fixes

  • deps: update redis docker tag to v7.0.11 (#1279) (59ab56d)

8.6.0 (2023-04-11)

Features

Bug Fixes

  • sorted lists should throw WRONGTYPE instead of returning empty arrays (b46b9b8)

8.5.0 (2023-04-08)

Features

8.4.0 (2023-03-28)

Features

Bug Fixes

8.3.0 (2023-03-28)

Features

Bug Fixes

8.2.7 (2023-03-22)

Bug Fixes

8.2.6 (2023-01-30)

Bug Fixes

  • duplicate: preserve options as well as accept overrides (#1234) (284f711)

8.2.5 (2023-01-29)

Bug Fixes

  • info: report correct redis version (7.0.8) (08cab51), closes #1199

8.2.4 (2023-01-29)

Bug Fixes

8.2.3 (2023-01-29)

Bug Fixes

3.11.0 - 2018-07-18

Added

  • zcount command (#460 @jmelion)

3.10.2 - 2018-07-11

Fixed

  • Closer mock of pipeline/multi feature (#458 @BastienAr)

3.10.1 - 2018-06-25

Fixed

  • redis format for xread for multiple streams. (#455 @critocrito)

3.10.0 - 2018-06-24

Added

  • xadd, xlen, xrange, xrevrange and xread for stream support (#449 @critocrito)

3.9.1 - 2018-06-09

Fixed

  • zrange functions sort items with the same score in lexicographical order (#446 @kylewm)

3.9.0 - 2018-06-04

Added

  • zrevrangebyscore commands (#442 @kylewm)
  • zrem and zincrby commands (#443 @kylewm)

3.8.3 - 2018-05-25

Fixed

  • missing case in spop (#433 @BastienAr)

3.8.2 - 2018-05-24

Fixed

  • spop behaviour (#432 @BastienAr)

3.8.1 - 2018-04-12

Fixed

  • flatten (smoosh) args correctly (#405 @BastienAr)

3.8.0 - 2018-04-10

Added

  • zscanStream and hscanStream commands (#393 @BastienAr)

3.7.1 - 2018-04-03

Fixed

  • pexpire, pexpireat, psetex to set correct expire time (#389 @vaskevich)

3.7.0 - 2018-04-01

Added

  • scanStream command (#363 @BastienAr)

3.6.4 - 2018-02-17

Fixed

  • keys no longer returns empty sets, just like real redis (#358)

3.6.3 - 2018-02-06

Fixed

  • srandom not using Set. (#356 @ianmuninio)

3.6.2 - 2018-02-01

Fixed

  • del should return number of keys deleted from the data, not the number of arguments (#355 @donaldjarmstrong)

3.6.1 - 2018-01-06

Fixed

  • Passing null values should not throw exceptions (#353 @kkragenbrink)

3.6.0 - 2018-01-03

Added

  • zrangebyscore command in addition to unsubscribe and quit stubs (#350 @usebaz)

3.5.0 - 2017-12-07

Added

  • subscribe command stub (#348 @yitongding)

3.4.2 - 2017-11-28

Fixed

  • Remove .eslintcache file from package (#347)

3.4.1 - 2017-11-28

Fixed

  • More detailed RunKit example (#346)

3.4.0 - 2017-11-27

Added

  • Argument and Reply Transformers (#342 @DrMegavolt)

3.3.1 - 2017-11-17

Fixed

3.3.0 - 2017-11-16

Added

  • zadd, zrange, zremrevbyrank and zrevrange commands. (#321 @ddunkin)
  • Added support for passing objects and maps to hmset (#337)

3.2.0 - 2017-11-15

Added

  • lrange command. (#335 @sseidametov)
  • scan command. (#334 @DrMegavolt)

3.1.3 - 2017-11-12

Fixes

  • Fixed errors in smembers and srem when no data is set (#332)

3.1.2 - 2017-11-09

Fixes

  • Fixed hexists edge cases. (#331 @wraytw)

3.1.1 - 2017-10-19

Fixes

  • Ignore codeclimate in npm package

3.1.0 - 2017-10-03

Features

  • Added pipeline with method chaining (#312 @funnisimo)

3.0.2 - 2017-10-02

Fixes

  • Added back support for node v0.10.x like ioredis.

3.0.1 - 2017-10-02

Fixes

  • Removed README and RunKit references to old import style.

3.0.0 - 2017-10-02

Changed

  • Use module.exports to be in line with ioredis. (#311 @rexxars)

Fixes

  • Error thrown in sismember() if key does not exist. (#318 @theogravity)

2.4.1 - 2017-10-02

Fixes

  • Allow ioredis 3 as peer dependency. (#317 @aruberto)

2.4.0 - 2017-03-05

Features

  • exec supports a callback argument. (#290)

Fixes

  • Don't modify objects passed to or returned from the internal datastore. (#281 @jeffkenney)
  • Fix hash get commands for missing hashes. (#284 @jeffkenney)

2.3.0 - 2017-02-28

Added

  • Add event emitter inheritance. (#248 @xsellier)
  • Trigger connect & ready events on instantiation. (#280 @rexxars)

Changed

  • Prevent conversion of buffer arguments to strings. (#236 @dpikt)

Misc

  • updated all dependencies to latest stable versions. (@greenkeeperio-bot)

2.2.0 - 2016-10-31

Added

  • buffer operation aliases. (#234 @dpikt)
  • yarn lockfile.

Changed

  • updated redis-commands dependency and updated the compat table with new commands.

2.1.0 - 2016-10-05

Added

  • smove command. (#217)
  • sdiff command. (#216)
  • sunion command. (#215)
  • sinter command. (#214)

Changed

  • added compat table badge to readme. (#213)

2.0.0 - 2016-10-03

Bumping the version to v2 as there are fixes in this release that are breaking changes.

Added

  • flushdb command.

Changed

  • Command arguments is now transformed to strings before being passed to the command itself helping the mock behave more like a real ioredis client.

Fixed

  • append updated to return an integer.
  • dbsize updated to return an integer.
  • decr updated to return an integer.
  • decrby updated to return an integer.
  • expire updated to return an integer.
  • expireat updated to return an integer.
  • hdel updated to return an integer.
  • hexists updated to return an integer.
  • hincrby updated to return an integer.
  • hlen updated to return an integer.
  • hset updated to return an integer.
  • hsetnx updated to return an integer.
  • hstrlen updated to return an integer.
  • incr updated to return an integer.
  • incrby updated to return an integer.
  • lpush updated to return an integer.
  • lpushx updated to return an integer.
  • mget required a single array argument, updated to use multiple arguments (single array support will be added later when ioredis Argument Transformers is properly implemented).
  • msetnx updated to return an integer.
  • persist updated to return an integer.
  • pexpire updated to return an integer.
  • pexpireat updated to return an integer.
  • pttl updated to return an integer.
  • renamenx updated to return an integer.
  • rpush updated to return an integer.
  • rpushx updated to return an integer.
  • scard updated to return an integer.
  • setnx updated to return an integer.
  • sismember updated to return an integer.
  • strlen updated to return an integer.
  • ttl updated to return an integer.

Deprecated

  • hmset no longer accepts passing an object with keys and values directly. This will be added later when ioredis Argument Transformers is properly implemented.

1.15.0 - 2016-10-03

Added

  • type command (#207)

Fixed

  • sadd no longer allow duplicate values (#204 @kesla)
  • hset learned to create a new hash if not existing instead of throwing error (#207)
  • hset updated to return integer reply according to redis spec (#207)

Internal

  • set related commands (sadd, scard, etc) implemented using es6 Set instead of Array (#204 @kesla)
  • hset tests refactored (#207)

1.14.0 - 2016-10-02

Features

  • added scard command (#201)
  • added discard command (#200)

Documentation

  • added CHANGELOG.md file

1.13.0 - 2016-10-02

Features

  • added mset command (#181)
  • added msetnx command (#182)
  • added dbsize command (#183)
  • added lpushx command (#184)
  • added incrbyfloat command (#185)
  • added rpushx command (#186)
  • added hincrbyfloat command (#187)
  • added lpop command (#189)
  • added rpop command (#190)
  • added lindex command (#191)
  • added srandmember command (#192)
  • added lset command (#193)

1.12.0 - 2016-10-02

Features

  • added auth command (#160)
  • added bgrewriteaof command (#161)
  • added save command (#162)
  • added lastsave command (#163)
  • added bgsave command (#164)
  • added getrange command (#165)
  • added echo command (#166)
  • added randomkey command (#167)
  • added role command (#168)
  • added flushall command (#169)
  • added ping command (#170)
  • added persist command (#171)
  • added expireat command (#172)
  • added pttl command (#173)
  • added hkeys command (#174)
  • added setex command (#175)
  • added pexpireat command (#176)
  • added hlen command (#177)
  • added pexpire command (#178)
  • added psetex command (#179)

1.11.0 - 2016-10-01

Features

  • added expire command (#155)
  • added ttl command (#157)

Documentation

  • cleaned up compat table (#158)

Tests

  • circleci now run parallel builds to distribute node v4 - v6 tests between containers, speeding things up a bit

1.10.0 - 2016-10-01

Features

  • added renamenx command (#152)

1.9.0 - 2016-09-26

Features

  • added mget command (#134 @kesla)

Misc

  • updated all dependencies to latest versions (thanks, @greenkeeperio-bot!)

1.8.0 - 2016-08-13

Features

  • added brpoplpush command (#106 @davemcorwin)
  • added llen command (#106 @davemcorwin)
  • added lpush command (#106 @davemcorwin)
  • added lrem command (#106 @davemcorwin)
  • added publish stub (#106 @davemcorwin)
  • added rpoplpush command (#106 @davemcorwin)
  • added rpush command (#106 @davemcorwin)

Fixes

  • hmset command supports objects (#106 @davemcorwin)
  • incr command sets default value '0' if not exists (#106 @davemcorwin)

Misc

  • updated all dependencies to latest versions (thanks, @greenkeeperio-bot!)

1.7.0 - 2016-08-02

Features

  • added keys command (#92 @pivotal-csaa)

Misc

  • updated test and linting dependencies to latest versions (thanks, @greenkeeperio-bot!)

1.6.0 - 2016-07-04

Features

  • added setnx command

Bugfixes

  • hsetnx would report '0' to signal the key was a no-op but in reality did change the existing value.

1.5.0 - 2016-06-28

Features

  • added strlen command (#53)
  • added hdel command (#54)
  • added hexists command (#55)
  • added hincrby command (#56)
  • added hstrlen command (#57)

Documentation

  • Added a note about tonicdev to readme (#52)

1.4.1 - 2016-06-27

Minor changes

  • updated istanbul devDependency

1.4.0 - 2016-06-26

Features

  • added hmget command
  • added rename command
  • added append command

Bugfixes

  • performance penalties for using delete (#45)

1.3.0 - 2016-06-21

Features

  • added exists command.
  • added time command.
  • added getset command.

1.2.0 - 2016-06-16

Features

  • added incrby and decrby commands (#34).

Documentation

  • compat.md now links to redis docs for easy lookup.

1.1.1 - 2016-06-15

  • 30 Fix regression in v1.1.0 that broke the whole module (sorry!).

1.1.0 - 2016-06-14

  • refactored to Bluebird promises.
  • node style callbacks supported, like in ioredis.
  • added del and decr.

1.0.6 - 2016-06-14

  • added compat table.
  • added npm preversion and prepublish scripts to prevent easy maintenance mistakes.

1.0.5 - 2016-06-14

  • updated readme

1.0.4 - 2016-06-12

  • 100% test coverage
  • srem and sadd accepts multiple items
  • added set and get commands

1.0.3 - 2016-06-12

  • setup CircleCI to cover latest stable node releases.
  • incr didn't persist changes to data.

1.0.2 - 2016-06-10

  • Setup travis CI and AppVeyor integrations.
  • Added tests for exec, hget, hset, hsetnx and incr.
  • Setup coveralls and codeclimate integrations.
  • Fixed broken hset implementation.

1.0.1 - 2016-06-09

  • lodash was missing in the package.json dependencies
  • fixes to documentation
  • added tonicExample

1.0.0 - 2016-06-09

Supported operations

  • Integers
  • incr
  • Hashing
  • hset
  • hget
  • hgetall
  • hsetnx
  • hmset
  • hvals
  • Lists
  • sadd
  • srem
  • smembers
  • sismember
  • Transaction
  • multi
  • exec