Skip to content

Plugin to share a common Redis mock connection across Fastify.

License

Notifications You must be signed in to change notification settings

Rubendewit/fastify-redis-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Plugin to share a common Redis mock connection across Fastify.

Resources

License

Stars

Watchers

Forks

Packages

No packages published