Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda