fastify-redis-mock
Version:
Plugin to share a common Redis mock connection across Fastify.
325 lines (263 loc) • 6.81 kB
JavaScript
const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const fastifyRedis = require('./index')
t.beforeEach(async () => {
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
await fastify.ready()
await fastify.redis.flushall()
await fastify.close()
})
test('fastify.redis should exist', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.error(err)
t.ok(fastify.redis)
fastify.close()
})
})
test('fastify.redis should be the redis client', (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
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()
})
})
})
})
test('fastify.redis.test namespace should exist', (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test'
})
fastify.ready((err) => {
t.error(err)
t.ok(fastify.redis)
t.ok(fastify.redis.test)
fastify.close()
})
})
test('fastify.redis.test should be the redis client', (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test'
})
fastify.ready((err) => {
t.error(err)
fastify.redis.test.set('key_namespace', 'value_namespace', (err) => {
t.error(err)
fastify.redis.test.get('key_namespace', (err, val) => {
t.error(err)
t.equal(val, 'value_namespace')
fastify.close()
})
})
})
})
test('promises support', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.error(err)
fastify.redis
.set('key', 'value')
.then(() => {
return fastify.redis.get('key')
})
.then((val) => {
t.equal(val, 'value')
fastify.close()
})
.catch((err) => t.fail(err))
})
})
test('custom client', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, { client: redis })
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis, redis)
fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
fastify.close(function (err) {
t.error(err)
fastify.redis.quit(function (err) {
t.error(err)
})
})
})
})
})
})
test('custom client gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, { client: redis, closeClient: true })
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis, redis)
fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
const origQuit = fastify.redis.quit
fastify.redis.quit = (cb) => {
t.pass('redis client closed')
origQuit.call(fastify.redis, cb)
}
fastify.close(function (err) {
t.error(err)
})
})
})
})
})
test('custom client inside a namespace', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, {
namespace: 'test',
client: redis
})
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis.test, redis)
fastify.redis.test.set('key', 'value', (err) => {
t.error(err)
fastify.redis.test.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
fastify.close(function (err) {
t.error(err)
fastify.redis.test.quit(function (err) {
t.error(err)
})
})
})
})
})
})
test('custom client inside a namespace gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, {
namespace: 'test',
client: redis,
closeClient: true
})
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis.test, redis)
fastify.redis.test.set('key', 'value', (err) => {
t.error(err)
fastify.redis.test.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
const origQuit = fastify.redis.test.quit
fastify.redis.test.quit = (cb) => {
t.pass('redis client closed')
origQuit.call(fastify.redis.test, cb)
}
fastify.close(function (err) {
t.error(err)
})
})
})
})
})
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
t.plan(1)
const namespace = 'test'
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(fastifyRedis, {
host: '127.0.0.1',
namespace
})
.register(fastifyRedis, {
host: '127.0.0.1',
namespace
})
fastify.ready((err) => {
t.equal(err.message, `Redis '${namespace}' instance namespace has already been registered`)
})
})
test('Should throw when trying to register multiple instances without giving a namespace', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(fastifyRedis, {
host: '127.0.0.1'
})
.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.equal(err.message, 'fastify-redis has already been registered')
})
})
test('Should not throw within different contexts', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify.register(function (instance, options, next) {
instance.register(fastifyRedis, {
host: '127.0.0.1'
})
next()
})
fastify.register(function (instance, options, next) {
instance
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test1'
})
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test2'
})
next()
})
fastify.ready((error) => {
t.error(error)
})
})