Skip to content

Commit 0cc31db

Browse files
authored
Pass url to Redis contructor when not using namespaces (#49)
* pass url to instansiate redis when not using namespaces * update unit test
1 parent b1c4022 commit 0cc31db

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
language: node_js
22

33
node_js:
4+
- "14"
45
- "12"
56
- "10"
6-
- "8"
7-
- "6"
87

98
services:
109
- redis-server

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function fastifyRedis (fastify, options, next) {
4848
} else {
4949
if (!client) {
5050
try {
51-
client = new Redis(options)
51+
if (redisUrl) {
52+
client = new Redis(redisUrl, options)
53+
} else {
54+
client = new Redis(options)
55+
}
5256
} catch (err) {
5357
return next(err)
5458
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"test": "npm run lint && npm run unit && npm run typescript",
99
"lint": "standard",
10-
"unit": "tap test.js",
10+
"unit": "tap test/test.js",
1111
"typescript": "tsc --project ./test/types/tsconfig.json",
1212
"redis": "docker run -p 6379:6379 --rm redis:5"
1313
},
@@ -33,6 +33,7 @@
3333
"@types/ioredis": "^4.0.18",
3434
"@types/node": "^12.11.1",
3535
"fastify": "^2.5.0",
36+
"proxyquire": "^2.1.3",
3637
"redis": "^2.8.0",
3738
"standard": "^14.0.2",
3839
"tap": "^12.7.0"

test/test.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

33
const t = require('tap')
4+
const proxyquire = require('proxyquire')
45
const test = t.test
56
const Fastify = require('fastify')
6-
const fastifyRedis = require('../index')
7+
const fastifyRedis = require('..')
78

89
t.beforeEach((done) => {
910
const fastify = Fastify()
@@ -38,26 +39,28 @@ test('fastify.redis should exist', (t) => {
3839
})
3940

4041
test('fastify.redis should support url', (t) => {
41-
t.plan(4)
42+
t.plan(3)
4243
const fastify = Fastify()
4344

45+
const fastifyRedis = proxyquire('..', {
46+
ioredis: function Redis (path, options) {
47+
t.equal(path, 'redis://127.0.0.1')
48+
t.deepEqual(options, {
49+
otherOption: 'foo'
50+
})
51+
this.quit = () => {}
52+
return this
53+
}
54+
})
55+
4456
fastify.register(fastifyRedis, {
4557
url: 'redis://127.0.0.1',
4658
otherOption: 'foo'
4759
})
4860

4961
fastify.ready((err) => {
5062
t.error(err)
51-
52-
fastify.redis.set('key', 'value', (err) => {
53-
t.error(err)
54-
fastify.redis.get('key', (err, val) => {
55-
t.error(err)
56-
t.equal(val, 'value')
57-
58-
fastify.close()
59-
})
60-
})
63+
fastify.close()
6164
})
6265
})
6366

0 commit comments

Comments
 (0)