UNPKG

4.86 kBMarkdownView Raw
1redis-mock
2============
3
4[![NPM](https://nodei.co/npm/redis-mock.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/redis-mock/)
5
6![Build status](https://github.com/yeahoffline/redis-mock/workflows/Build/badge.svg?branch=master)
7![node-current](https://img.shields.io/node/v/redis-mock)
8![mock-completeness](https://img.shields.io/badge/Methods%20mocked-46%25%20(222%2F482)-red)
9[![GitHub issues](https://img.shields.io/github/issues/yeahoffline/redis-mock)](https://github.com/yeahoffline/redis-mock/issues)
10
11The goal of the `redis-mock` project is to create a feature-complete mock of [node_redis](https://github.com/NodeRedis/node_redis), which may be used interchangeably when writing unit tests for code that depends on `redis`.
12
13All operations are performed in-memory, so no Redis installation is required.
14
15100% Redis-compatible (see [Cross Verification](#cross-verification))
16
17# Installation
18
19````bash
20$ npm install redis-mock --save-dev
21````
22
23
24## Usage
25
26### node.js/io.js
27
28The below code demonstrates a example of using the redis-mock client in node.js/io.js
29
30
31```js
32var redis = require("redis-mock"),
33 client = redis.createClient();
34```
35
36
37# API
38
39Currently implemented are the following redis commands:
40
41### General
42* createClient
43* duplicate
44* auth
45* end
46* multi
47 * exec
48 * discard
49 * exec_atomic
50* batch
51
52### Events
53* ready
54* connect
55* end
56* quit
57* subscribe
58* unsubscribe
59* message
60* psubscribe
61* punsubscribe
62* pmessage
63
64### Publish/subscribe
65* publish
66* subscribe
67* unsubscribe
68* psubscribe
69* punsubscribe
70
71### Keys
72* del
73* keys
74* scan
75* exists
76* type
77* expire
78* ttl
79* incr
80* incrby
81* incrbyfloat
82* decr
83* decrby
84* rename
85* dbsize
86* renamenx
87
88### Strings
89* get
90* set
91* append
92* getset
93* mget
94* mset
95* msetnx
96* setex
97* setnx
98* ping
99
100### Hashing
101* hset
102* hsetnx
103* hget
104* hexists
105* hdel
106* hlen
107* hgetall
108* hscan
109* hmset
110* hmget
111* hkeys
112* hvals
113* hincrby
114* hincrbyfloat
115
116### Lists
117* llen
118* lpush
119* rpush
120* lpushx
121* rpushx
122* lpop
123* rpop
124* blpop
125* brpop
126* lindex
127* lrange
128* lrem
129* lset
130
131### Sets
132* sadd
133* srem
134* smembers
135* scard
136* sismember
137* sscan
138
139### Sorted Sets
140* zadd
141* zcard
142* zcount
143* zincrby
144* zrange
145* zrangebyscore
146* zrank
147* zrem
148* zremrangebyrank
149* zremrangebyscore
150* zrevrange
151* zrevrangebyscore
152* zrevrank
153* zunionstore (Partial: no support for `WEIGHTS` or `AGGREGATE` yet)
154* zinterstore (Partial: no support for `WEIGHTS` or `AGGREGATE` yet)
155* zscore
156
157### Server
158* flushdb
159* flushall
160* time
161
162
163# Cross verification
164
165If you want to add new tests to the test base it is important that they work too on node_redis (we are creating a mock...).
166You can therefore run the tests using `redis` instead of `redis-mock`. To do so:
167
168````bash
169$ npm test:valid
170````
171
172You will need to have a running instance of `redis` on you machine and our tests use flushdb a lot so make sure you don't have anything important on it.
173
174
175# Roadmap
176redis-mock is work in progress, feel free to report an issue
177
178# Example usage
179
180## Jest
181
182In order to make sure that your tests use the `redis-mock` instead of the actual `redis` implementation,
183update your `jest.config.js`, by adding `setupFileAfterEnv`, as follows:
184
185```javascript
186module.exports = {
187 // other properties...
188 setupFilesAfterEnv: ['./jest.setup.redis-mock.js'],
189};
190```
191
192From this point on, jest will always trigger the content of `jest.setup.redis-mock.js` before the execution of all tests.
193
194Now, let's create the file `jest.setup.redis-mock.js` in the same directory as `jest.config.js` and paste the following
195content:
196
197```javascript
198jest.mock('redis', () => jest.requireActual('redis-mock'));
199```
200
201This will make sure that the actual `redis` is never loaded and whenever any file tries to import/require `redis`,
202`redis-mock` will be returned instead.
203
204## LICENSE - "MIT License"
205
206Copyright (c) 2012 Kristian Faeldt <kristian.faeldt@gmail.com>
207
208Permission is hereby granted, free of charge, to any person
209obtaining a copy of this software and associated documentation
210files (the "Software"), to deal in the Software without
211restriction, including without limitation the rights to use,
212copy, modify, merge, publish, distribute, sublicense, and/or sell
213copies of the Software, and to permit persons to whom the
214Software is furnished to do so, subject to the following
215conditions:
216
217The above copyright notice and this permission notice shall be
218included in all copies or substantial portions of the Software.
219
220THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
221EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
222OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
223NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
224HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
225WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
226FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
227OTHER DEALINGS IN THE SOFTWARE.