🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

fastify-redis-mock

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-redis-mock

Plugin to share a common Redis mock connection across Fastify.

1.1.0
latest
Source
npm
Version published
Weekly downloads
1K
-13.72%
Maintainers
1
Weekly downloads
 
Created
Source

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:

  • fastify-redis

License

Licensed under MIT.

Keywords

mock

FAQs

Package last updated on 12 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts