Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!