stipsan / ioredis-mock

Emulates ioredis by performing all operations in-memory.
MIT License
339 stars 123 forks source link

.duplicate() does not accept overrides as well as loses options #1207

Closed erictheswift closed 1 year ago

erictheswift commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch ioredis-mock@8.2.2 for the project I'm working on.

Reference implementation of ioredis: https://github.com/luin/ioredis/blob/9e6db7d7fc769ddc99d9dee4a943f141d71c0756/lib/Redis.ts#L337

  duplicate(override?: Partial<RedisOptions>) {
    return new Redis({ ...this.options, ...override });
  }

Here is the diff that solved my problem:

diff --git a/node_modules/ioredis-mock/lib/index.js b/node_modules/ioredis-mock/lib/index.js
index d4797e3..c97daed 100644
--- a/node_modules/ioredis-mock/lib/index.js
+++ b/node_modules/ioredis-mock/lib/index.js
@@ -4555,6 +4556,7 @@ var defaultOptions = {
   constructor(...args) {
     super(), this.batch = void 0, this.connected = !1, this.subscriberMode = !1, this.customCommands = {}, this.shaScripts = {};
     let optionsWithDefault = __spreadValues(__spreadValues({}, defaultOptions), getOptions(...args));
+    this.options = optionsWithDefault;
     if (this.keyData = `${optionsWithDefault.host}:${optionsWithDefault.port}`, !context_default.get(this.keyData)) {
       let context2 = createContext(optionsWithDefault.keyPrefix);
       context_default.set(this.keyData, context2);
@@ -4593,8 +4595,8 @@ var defaultOptions = {
     let pipeline = this.batch;
     return this.batch = void 0, pipeline.exec(callback);
   }
-  duplicate() {
-    let mock = new RedisMock();
+  duplicate(override) {
+    let mock = new RedisMock({ ...this.options, ...override });
     return mock.expires = this.expires, mock.data = this.data, mock.channels = this.channels, mock.patternChannels = this.patternChannels, mock;
   }
   disconnect() {

This issue body was partially generated by patch-package.

stipsan commented 1 year ago

Thnx πŸ˜„ Feel free to send in a PR :relaxed:

erictheswift commented 1 year ago

Thnx πŸ˜„ Feel free to send in a PR ☺️

Sent