{"id":13486769,"url":"https://github.com/stipsan/ioredis-mock","last_synced_at":"2025-05-14T03:08:23.955Z","repository":{"id":9073578,"uuid":"60733133","full_name":"stipsan/ioredis-mock","owner":"stipsan","description":"Emulates ioredis by performing all operations in-memory.","archived":false,"fork":false,"pushed_at":"2025-02-11T20:54:08.000Z","size":7071,"stargazers_count":352,"open_issues_count":85,"forks_count":128,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-03T12:52:21.082Z","etag":null,"topics":["emulator","ioredis","mocking","node","redis","redis-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stipsan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-08T21:50:46.000Z","updated_at":"2025-03-16T08:37:32.000Z","dependencies_parsed_at":"2024-01-02T00:07:22.407Z","dependency_job_id":"2ac65683-260a-44b3-9ac5-adbdb578c2f2","html_url":"https://github.com/stipsan/ioredis-mock","commit_stats":{"total_commits":1284,"total_committers":99,"mean_commits":"12.969696969696969","dds":0.6230529595015577,"last_synced_commit":"6a5895505def87261315053f4fecab42bcc44883"},"previous_names":[],"tags_count":198,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stipsan%2Fioredis-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stipsan%2Fioredis-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stipsan%2Fioredis-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stipsan%2Fioredis-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stipsan","download_url":"https://codeload.github.com/stipsan/ioredis-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261933,"owners_count":21074226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["emulator","ioredis","mocking","node","redis","redis-server"],"created_at":"2024-07-31T18:00:50.889Z","updated_at":"2025-04-10T17:19:32.602Z","avatar_url":"https://github.com/stipsan.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# ioredis-mock \u0026middot; [![npm](https://img.shields.io/npm/dm/ioredis-mock.svg?style=flat-square)](https://npm-stat.com/charts.html?package=ioredis-mock) [![npm version](https://img.shields.io/npm/v/ioredis-mock.svg?style=flat-square)](https://www.npmjs.com/package/ioredis-mock) [![Redis Compatibility: 66%](https://img.shields.io/badge/redis-66%25-orange.svg?style=flat-square)](compat.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)\n\nThis library emulates [ioredis](https://github.com/luin/ioredis) by performing\nall operations in-memory. The best way to do integration testing against redis\nand ioredis is on a real redis-server instance. However, there are cases where\nmocking the redis-server is a better option.\n\nCases like:\n\n- Your workflow already use a local redis-server instance for the dev server.\n- You're on a platform\n  [without an official redis release](https://github.com/MSOpenTech/redis),\n  that's even worse than using an emulator.\n- You're running tests on a CI, setting it up is complicated. If you combine it\n  with CI that also run selenium acceptance testing it's even more complicated,\n  as two redis-server instances on the same CI build is hard.\n- The GitHub repo have bots that run the testing suite and is limited through\n  npm package.json install scripts and can't fire up servers. (Having\n  [Renovatebot](https://renovatebot.com/) notifying you when a new release of\n  ioredis is out and wether your code breaks or not is awesome).\n\nCheck the [compatibility table](compat.md) for supported redis commands.\n\n## Usage ([try it in your browser with RunKit](https://runkit.com/npm/ioredis-mock))\n\n```js\nconst Redis = require('ioredis-mock')\nconst redis = new Redis({\n  // `options.data` does not exist in `ioredis`, only `ioredis-mock`\n  data: {\n    user_next: '3',\n    emails: {\n      'clark@daily.planet': '1',\n      'bruce@wayne.enterprises': '2',\n    },\n    'user:1': { id: '1', username: 'superman', email: 'clark@daily.planet' },\n    'user:2': { id: '2', username: 'batman', email: 'bruce@wayne.enterprises' },\n  },\n})\n// Basically use it just like ioredis\n```\n\n## Browser usage (Experimental)\n\nThere's a browser build available. You can import it directly (`import Redis from 'ioredis-mock/browser.js'`), or use it on unpkg.com:\n\n```js\nimport Redis from 'https://unpkg.com/ioredis-mock'\n\nconst redis = new Redis()\nredis.set('foo', 'bar')\nconsole.log(await redis.get('foo'))\n```\n\n## Breaking Changes\n\n### from v7 to v8\n\n#### `ioredis@v4` support dropped\n\n`ioredis@v5` is the new baseline. Stay on `ioredis-mock@v7` until you're ready to upgrade to `ioredis@v5`.\n\n#### `PromiseContainer` has been removed.\n\nSupport for third-party Promise libraries is dropped. The native Promise library\nwill always be used.\n\n### from v6 to v7\n\n#### `createConnectedClient` is removed\n\nReplace it with `.duplicate()` or use another `new Redis` instance.\n\n#### Dropped support for Node v10\n\n[It's been EOL since Apr, 2021 and it's recommended to upgrade to v14.x LTS.](https://twitter.com/nodejs/status/1388116425361874945)\n\n#### `ioredis-mock/jest.js` is removed\n\n`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](https://github.com/stipsan/ioredis-mock/issues/568) is no longer needed:\n\n```diff\n-jest.mock('ioredis', () =\u003e require('ioredis-mock/jest'))\n+jest.mock('ioredis', () =\u003e require('ioredis-mock'))\n```\n\n### from v5 to v6\n\nBefore v6, each instance of `ioredis-mock` lived in isolation:\n\n```js\nconst Redis = require('ioredis-mock')\nconst redis1 = new Redis()\nconst redis2 = new Redis()\n\nawait redis1.set('foo', 'bar')\nconsole.log(await redis1.get('foo'), await redis2.get('foo')) // 'bar', null\n```\n\nIn v6 the [internals were rewritten](https://github.com/stipsan/ioredis-mock/pull/1110) to behave more like real life redis, if the host and port is the same, the context is now shared:\n\n```js\nconst Redis = require('ioredis-mock')\nconst redis1 = new Redis()\nconst redis2 = new Redis()\nconst redis3 = new Redis(6380) // 6379 is the default port\n\nawait redis1.set('foo', 'bar')\nconsole.log(\n  await redis1.get('foo'), // 'bar'\n  await redis2.get('foo'), // 'bar'\n  await redis3.get('foo') // null\n)\n```\n\nAnd since `ioredis-mock` now persist data between instances, you'll [likely](https://github.com/luin/ioredis/blob/8278ec0a435756c54ba4f98587aec1a913e8b7d3/test/helpers/global.ts#L8) need to run `flushall` between testing suites:\n\n```js\nconst Redis = require('ioredis-mock')\n\nafterEach(done =\u003e {\n  new Redis().flushall().then(() =\u003e done())\n})\n```\n\n## Pub/Sub channels\n\nWe also support redis [publish/subscribe](https://redis.io/topics/pubsub) channels.\nLike [ioredis](https://github.com/luin/ioredis#pubsub), you need two clients:\n\n```js\nconst Redis = require('ioredis-mock')\nconst redisPub = new Redis()\nconst redisSub = new Redis()\n\nredisSub.on('message', (channel, message) =\u003e {\n  console.log(`Received ${message} from ${channel}`)\n})\nredisSub.subscribe('emails')\nredisPub.publish('emails', 'clark@daily.planet')\n```\n\n## Lua scripting\n\nYou can use the `defineCommand` to define custom commands using lua or `eval` to directly execute lua code.\n\nIn order to create custom commands, using [lua](http://lua.org) scripting, [ioredis exposes the defineCommand method](https://github.com/luin/ioredis#lua-scripting).\n\nYou could define a custom command `multiply` which accepts one\nkey and one argument. A redis key, where you can get the multiplicand, and an argument which will be the multiplicator:\n\n```js\nconst Redis = require('ioredis-mock')\nconst redis = new Redis({ data: { k1: 5 } })\nconst commandDefinition = {\n  numberOfKeys: 1,\n  lua: 'return redis.call(\"GET\", KEYS[1]) * ARGV[1]',\n}\nredis.defineCommand('multiply', commandDefinition) // defineCommand(name, definition)\n// now we can call our brand new multiply command as an ordinary command\nredis.multiply('k1', 10).then(result =\u003e {\n  expect(result).toBe(5 * 10)\n})\n```\n\nYou can also achieve the same effect by using the `eval` command:\n\n```js\nconst Redis = require('ioredis-mock')\nconst redis = new Redis({ data: { k1: 5 } })\nconst result = redis.eval(`return redis.call(\"GET\", \"k1\") * 10`)\nexpect(result).toBe(5 * 10)\n```\n\nnote we are calling the ordinary redis `GET` command by using the global `redis` object's `call` method.\n\nAs a difference from ioredis we currently don't support:\n\n- dynamic key number by passing the number of keys as the first argument of the command.\n- automatic definition of the custom command buffer companion (i.e. for the custom command `multiply` the `multiplyBuffer` which returns values using `Buffer.from(...)`)\n- the `evalsha` command\n- the `script` command\n\n## Cluster(Experimental)\n\nWork on Cluster support has started, the current implementation is minimal and PRs welcome #359\n\n```js\nconst Redis = require('ioredis-mock')\n\nconst cluster = new Redis.Cluster(['redis://localhost:7001'])\nconst nodes = cluster.nodes()\nexpect(nodes.length).toEqual(1)\n```\n\n## [Roadmap](https://github.com/users/stipsan/projects/1/views/4)\n\nYou can check the [roadmap project page](https://github.com/users/stipsan/projects/1/views/4), and [the compat table](compat.md), to see how close we are to feature parity with `ioredis`.\n\n## I need a feature not listed here\n\nJust create an issue and tell us all about it or submit a PR with it! 😄\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstipsan%2Fioredis-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstipsan%2Fioredis-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstipsan%2Fioredis-mock/lists"}