Détail du package

@kronoverse/ioredis-mock

stipsan16MIT4.21.4

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

fakeredis, in-memory, ioredis, ioredis-mock

readme

ioredis-mock · npm npm version Redis Compatibility: 63% 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 Greenkeeper 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)

var Redis = require('ioredis-mock');
var 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

Pub/Sub channels

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

var Redis = require('ioredis-mock');
var redisPubSub = new Redis();
// create a second Redis Mock (connected to redisPubSub)
var redisSync = redisPubSub.createConnectedClient();
redisPubSub.on('message', (channel, message) => {
  expect(channel).toBe('emails');
  expect(message).toBe('clark@daily.planet');
  done();
});
redisPubSub.subscribe('emails');
redisSync.publish('emails', 'clark@daily.planet');

Promises

By default, ioredis-mock uses the native Promise library. If you need (or prefer) bluebird promises, set Redis.Promise:

var Promise = require('bluebird');
var Redis = require('ioredis-mock');

Redis.Promise = Promise;

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:

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

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

var 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

Roadmap

This project started off as just an utility in another project and got open sourced to benefit the rest of the ioredis community. This means there's work to do before it's feature complete:

  • [x] Setup testing suite for the library itself.
  • [x] Refactor to bluebird promises like ioredis, support node style callback
    too.
  • [x] Implement remaining basic features that read/write data.
  • [x] Implement ioredis
    [argument and reply transformers](https://github.com/luin/ioredis#transforming-arguments--replies).
  • [ ] Connection Events
  • [ ] Offline Queue
  • [x] Pub/Sub
  • [ ] Error Handling
  • [ ] Implement remaining commands

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.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

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