1 | redis-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 |
|
11 | The 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 |
|
13 | All operations are performed in-memory, so no Redis installation is required.
|
14 |
|
15 | 100% 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 |
|
28 | The below code demonstrates a example of using the redis-mock client in node.js/io.js
|
29 |
|
30 |
|
31 | ```js
|
32 | var redis = require("redis-mock"),
|
33 | client = redis.createClient();
|
34 | ```
|
35 |
|
36 |
|
37 | # API
|
38 |
|
39 | Currently 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 |
|
165 | If 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...).
|
166 | You can therefore run the tests using `redis` instead of `redis-mock`. To do so:
|
167 |
|
168 | ````bash
|
169 | $ npm test:valid
|
170 | ````
|
171 |
|
172 | You 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
|
176 | redis-mock is work in progress, feel free to report an issue
|
177 |
|
178 | # Example usage
|
179 |
|
180 | ## Jest
|
181 |
|
182 | In order to make sure that your tests use the `redis-mock` instead of the actual `redis` implementation,
|
183 | update your `jest.config.js`, by adding `setupFileAfterEnv`, as follows:
|
184 |
|
185 | ```javascript
|
186 | module.exports = {
|
187 | // other properties...
|
188 | setupFilesAfterEnv: ['./jest.setup.redis-mock.js'],
|
189 | };
|
190 | ```
|
191 |
|
192 | From this point on, jest will always trigger the content of `jest.setup.redis-mock.js` before the execution of all tests.
|
193 |
|
194 | Now, let's create the file `jest.setup.redis-mock.js` in the same directory as `jest.config.js` and paste the following
|
195 | content:
|
196 |
|
197 | ```javascript
|
198 | jest.mock('redis', () => jest.requireActual('redis-mock'));
|
199 | ```
|
200 |
|
201 | This 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 |
|
206 | Copyright (c) 2012 Kristian Faeldt <kristian.faeldt@gmail.com>
|
207 |
|
208 | Permission is hereby granted, free of charge, to any person
|
209 | obtaining a copy of this software and associated documentation
|
210 | files (the "Software"), to deal in the Software without
|
211 | restriction, including without limitation the rights to use,
|
212 | copy, modify, merge, publish, distribute, sublicense, and/or sell
|
213 | copies of the Software, and to permit persons to whom the
|
214 | Software is furnished to do so, subject to the following
|
215 | conditions:
|
216 |
|
217 | The above copyright notice and this permission notice shall be
|
218 | included in all copies or substantial portions of the Software.
|
219 |
|
220 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
221 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
222 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
223 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
224 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
225 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
226 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
227 | OTHER DEALINGS IN THE SOFTWARE.
|