Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
Jest mock a callback function in an express middleware

I have an Express middleware which publishes a message in a channel (thanks to the redis pub/sub mechanism under the hood); this is done by the zeebeRedisAffinity.publishMessageWithAffinity call. This call triggers a workflow to be executed, and at some point later, a publish message in the same channel is injected, which triggers the call of the callback function cb in which I reply back to the user: res.send().

What I want to do is to mock this mechanism. I want to mock or spyOn the function zeebeRedisAffinity.publishMessageWithAffinity to make sure I get the expected response.

FYI, the library zeebe-node-affinity uses the npm package redis

// middleware.js

import zeebeRedisAffinity from '../zeebeRedisAffinity';

const expressMiddleware = async (req, res, next) => {
    const workflowId = await RedisClient.hGet('userId', 'workflowId');

    await zeebeRedisAffinity.publishMessageWithAffinity({
        correlationKey: '570840504540540',
            messageId: '0564405454054054540540',
            name: 'message-event',
            variables,
            workflowInstanceKey: workflowId,
            cb: (userResponse) => {
                res.send({response: userResponse})
            }
    })
};

// zeebeRedisAffinity.js

import { RedisAffinity } from 'zeebe-node-affinity';
import appConfig from '../appConfig';

const { ZEEBE_GATEWAY, REDIS_HOST, REDIS_AUTH } = appConfig;

const zeebeRedisAffinity = new RedisAffinity(ZEEBE_GATEWAY, {
    host: REDIS_HOST,
    password: REDIS_AUTH,
});

export default zeebeRedisAffinity;

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda