#376·reselect

Regression when using lodash for an unbounded cache and reselect 4.0

Author: nicolasernyCreated Oct 4, 2018Updated Mar 18, 2026
LabelsCaching

I have the following unit test inspired from the reselect documentation (Use memoize function from lodash for an unbounded cache section)

javascript
import { createSelectorCreator } from "reselect";
import memoize from "lodash.memoize";

// generic hash function provided by reselect to create custom selector
const hashFn = (...args) =>
  args.reduce((acc, val) => acc + "-" + JSON.stringify(val), "");

const unlimitedCacheSelectorCreator = createSelectorCreator(memoize, hashFn);

describe("unlimitedCacheSelectorCreator", () => {
  const selectorCreatorTest = [
    { id: 0, expected: ["test0"], recomputations: 1 },
    { id: 2, expected: ["test2"], recomputations: 2 },
    { id: 0, expected: ["test0"], recomputations: 2 },
    { id: 1, expected: ["test1"], recomputations: 3 },
    { id: 0, expected: ["test0"], recomputations: 3 },
    { id: 2, expected: ["test2"], recomputations: 3 },
    { id: 1, expected: ["test1"], recomputations: 3 }
  ];

  const getId = (_, id) => id;
  const stateExample = [
    { id: 0, name: "test0" },
    { id: 1, name: "test1" },
    { id: 2, name: "test2" }
  ];

  const selector = unlimitedCacheSelectorCreator(
    [state => state, getId],
    (stateValue, id) =>
      stateValue.filter(val => val.id === id).map(val => val.name)
  );

  selectorCreatorTest.forEach(test => {
    it("Selector should get values using an unlimited cache", () => {
      expect(selector(stateExample, test.id)).toEqual(test.expected);

      expect(selector.recomputations()).toEqual(test.recomputations);
    });
  });
});

All the tests passed with reselect 3.0.1. After upgrading to reselect 4.0, 6 tests keep on failing. It seems that the hash function is called once. Therefore the selector always returns the same value (instead of the right value). Is there anything wrong with my code ?