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;