diff --git a/.travis.yml b/.travis.yml index 124b2d2..0385210 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: node_js node_js: + - "14" - "12" - "10" - - "8" - - "6" services: - redis-server diff --git a/index.js b/index.js index 5b08790..e07ed0e 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,11 @@ function fastifyRedis (fastify, options, next) { } else { if (!client) { try { - client = new Redis(options) + if (redisUrl) { + client = new Redis(redisUrl, options) + } else { + client = new Redis(options) + } } catch (err) { return next(err) } diff --git a/package.json b/package.json index 466bf8b..f988ff9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "test": "npm run lint && npm run unit && npm run typescript", "lint": "standard", - "unit": "tap test.js", + "unit": "tap test/test.js", "typescript": "tsc --project ./test/types/tsconfig.json", "redis": "docker run -p 6379:6379 --rm redis:5" }, @@ -33,6 +33,7 @@ "@types/ioredis": "^4.0.18", "@types/node": "^12.11.1", "fastify": "^2.5.0", + "proxyquire": "^2.1.3", "redis": "^2.8.0", "standard": "^14.0.2", "tap": "^12.7.0" diff --git a/test/test.js b/test/test.js index a0205a8..d1cf08e 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,10 @@ 'use strict' const t = require('tap') +const proxyquire = require('proxyquire') const test = t.test const Fastify = require('fastify') -const fastifyRedis = require('../index') +const fastifyRedis = require('..') t.beforeEach((done) => { const fastify = Fastify() @@ -38,9 +39,20 @@ test('fastify.redis should exist', (t) => { }) test('fastify.redis should support url', (t) => { - t.plan(4) + t.plan(3) const fastify = Fastify() + const fastifyRedis = proxyquire('..', { + ioredis: function Redis (path, options) { + t.equal(path, 'redis://127.0.0.1') + t.deepEqual(options, { + otherOption: 'foo' + }) + this.quit = () => {} + return this + } + }) + fastify.register(fastifyRedis, { url: 'redis://127.0.0.1', otherOption: 'foo' @@ -48,16 +60,7 @@ test('fastify.redis should support url', (t) => { fastify.ready((err) => { t.error(err) - - fastify.redis.set('key', 'value', (err) => { - t.error(err) - fastify.redis.get('key', (err, val) => { - t.error(err) - t.equal(val, 'value') - - fastify.close() - }) - }) + fastify.close() }) })