fastify-redis-mock
v1.1.0
Published
Plugin to share a common Redis mock connection across Fastify.
Downloads
2,551
Maintainers
Readme
fastify-redis-mock
Fastify Redis mock connection plugin, with this you can share the same Redis mock connection in every part of your server.
This package is useful if you wish to unittest your Fastify project, without the need to run Redis itself.
Full credit goes to the Fastify team for building fastify-redis, and stipsan for building ioredis-mock.
For documentation, please refer to either the Fastify-redis docs, or ioredis-mock docs.
Install
npm i fastify-redis-mock --save
Usage
Add it to your project with register
and you are done!
You can access the Redis mock client via fastify.redis
.
const fastify = require('fastify')
const redis = require('fastify-redis')
const redisMock = require('fastify-redis-mock')
// Register the mock for e.g. unit tests
const redisInstance = process.env.ENVIRONMENT === 'unittest' ? redisMock : redis;
fastify.register(redisInstance, [options])
fastify.get('/foo', (req, reply) => {
const { redis } = fastify
redis.get(req.query.key, (err, val) => {
reply.send(err || val)
})
})
fastify.post('/foo', (req, reply) => {
const { redis } = fastify
redis.set(req.body.key, req.body.value, (err) => {
reply.send(err || { status: 'ok' })
})
})
fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
Acknowledgements
This project is copied from:
License
Licensed under MIT.