Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addBuiltinCommand support #1285

Open
amanthegreatone opened this issue Jun 22, 2023 · 0 comments
Open

addBuiltinCommand support #1285

amanthegreatone opened this issue Jun 22, 2023 · 0 comments

Comments

@amanthegreatone
Copy link

amanthegreatone commented Jun 22, 2023

I am using "ioredis": "^4.27.5" and have added some built in commands which i now want to use in the ioredis-mock but not able to do so. I am using "ioredis-mock": "^7.2.0"

const Redis = require('ioredis');

const { REDIS_HOST } = require('../config/env.json');
const { checkIfValidJsonAndReturnJson } = require('../helpers/common/string');

const redisOptions = { host: REDIS_HOST, port: 6379, db: process.env.NODE_ENV === 'test' ? 15 : 0 };

// Transforming Replies
Redis.Command.setReplyTransformer('hgetall', result => {
  if (Array.isArray(result)) {
    const obj = {};
    for (let i = 0; i < result.length; i += 2) {
      obj[result[i]] = checkIfValidJsonAndReturnJson(result[i + 1]);
    }
    return obj;
  }
  return result;
});

Redis.Command.setReplyTransformer('hget', result => {
  if (result && typeof result === 'string') {
    result = checkIfValidJsonAndReturnJson(result);
    return result;
  }
  return result;
});

Redis.Command.setReplyTransformer('JSON.GET', result => {
  if (result && typeof result === 'string') {
    result = checkIfValidJsonAndReturnJson(result);
    return result;
  }
  return result;
});

const RedisClient = (() => {
  let client;
  const setRedisClient = nodeProcessName => {
    client = new Redis({ host: redisOptions.host, port: redisOptions.port, db: redisOptions.db });
    client.addBuiltinCommand('JSON.GET');
    client.addBuiltinCommand('JSON.SET');
    client.addBuiltinCommand('JSON.DEL');
    client.addBuiltinCommand('JSON.ARRAPPEND');
    client.on('connect', () => { console.log("connected) });
    return client;
  };
  return {
    getRedisClient: nodeProcessName => {
      if (!client) {
        client = setRedisClient(nodeProcessName);
      }
      return client;
    }
  };
})();

module.exports = {
  redis: nodeProcessName => RedisClient.getRedisClient(nodeProcessName),
  redisOptions
};

in the unit testing file this is throwing an error

const Redis = require('ioredis-mock');
const { checkIfValidJsonAndReturnJson } = require('src/helpers/common/string');

// Transforming Replies
Redis.Command.setReplyTransformer('hgetall', result => {
  if (Array.isArray(result)) {
    const obj = {};
    for (let i = 0; i < result.length; i += 2) {
      obj[result[i]] = checkIfValidJsonAndReturnJson(result[i + 1]);
    }
    return obj;
  }
  return result;
});

Redis.Command.setReplyTransformer('hget', result => {
  if (result && typeof result === 'string') {
    result = checkIfValidJsonAndReturnJson(result);
    return result;
  }
  return result;
});

Redis.Command.setReplyTransformer('JSON.GET', result => {
  if (result && typeof result === 'string') {
    result = checkIfValidJsonAndReturnJson(result);
    return result;
  }
  return result;
});

const redisClient = new Redis();
redisClient.addBuiltinCommand('JSON.GET');
redisClient.addBuiltinCommand('JSON.SET');
redisClient.addBuiltinCommand('JSON.DEL');
redisClient.addBuiltinCommand('JSON.ARRAPPEND');

this is the error i get

TypeError: redisClient.addBuiltinCommand is not a function
    at Object.<anonymous> (/Users/developer/syook/syook-tnt-server/specs/awilixContainerForTests.js:60:13)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module._compile (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/@babel/register/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.newLoader [as .js] (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/@babel/register/node_modules/pirates/lib/index.js:141:7)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Object.<anonymous> (/Users/developer/syook/syook-tnt-server/specs/unit/service/ruleAlertGenerator.test.js:26:23)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module._compile (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/@babel/register/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.newLoader [as .js] (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/@babel/register/node_modules/pirates/lib/index.js:141:7)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:203:29)
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at Loader.import (internal/modules/esm/loader.js:178:24)
    at formattedImport (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/mocha/lib/nodejs/esm-utils.js:9:14)
    at Object.exports.requireOrImport (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/mocha/lib/nodejs/esm-utils.js:42:28)
    at Object.exports.loadFilesAsync (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
    at singleRun (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at Object.exports.handler (/Users/developer/.nvm/versions/node/v14.21.2/lib/node_modules/mocha/lib/cli/run.js:370:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant