reduxjs / reselect

Selector library for Redux
MIT License
19.04k stars 670 forks source link

Unable to use jest with reselect #468

Closed clems36 closed 3 years ago

clems36 commented 4 years ago

Hi,

I'm trying to run my jest tests but this error appears multiple times in random test files that don't even include selectors: Selector creators expect all input-selectors to be functions, instead received the following types: [undefined].

Now i've encountered this issue before while running my react-native app but I never had it while running my tests. The way I had solved it was by declaring my selectors in a different order. Obviously I can't do that again for my tests because that would most likely mess it up for my app.

What I don't understand is why

Mocking createSelector is not an option because some of my tests actually test selectors.

I'm running on reselect version 4.0.0.

Cheers!

markerikson commented 4 years ago

Without seeing any actual code, I would assume it has something to do with dependencies between files and declaration orders.

clems36 commented 4 years ago

Thanks for your answer! I have noticed that the test files that are failing always import a function from a file that imports a selector, or imports another file that imports a selector, etc ... But the test file itself does not use the selector.

For instance, the following is happening: Inside helper.test.js i'm importing normalizePlan and testing it as follows:

import { normalizePlan } from '../helpers';

describe('Test redux user helpers', () => {
  it('normalizes plan names', () => {
    expect(normalizePlan('myPlan')).toEqual(
      'myPlan',
    );
  });
});

and inside of helpers.js (where normalizePlan is declared), i'm importing getActivityStartingDate which is used in another function of that file, so it has nothing to do with normalizePlan. Also, getActivityStartingDate is not even a selector but it is declared in a file that contains selectors that use createSelector, which seems to be the issue.

clems36 commented 4 years ago

I ended up finding the selector that caused the issue and mocking it, which I didn't want to do in the first place. This is not really a solution imo.

markerikson commented 3 years ago

It's still unclear what the actual problem is here, so I'm going to close this.

areksvk commented 2 years ago

I had the same problem and had to solve it by mocking the imported files. Not very ideal.

markerikson commented 2 years ago

@areksvk comments that just say "I have the same problem" really aren't helpful :(

Actual code examples and repos that demonstrate the problem are the only way we can try to understand what's going on and provide help.

areksvk commented 2 years ago

@markerikson My bad. I have one dummy test:

import * as service from './heatmap.service';

describe('dummy test', () => {
    expect(1 + 2).toBe(3);
});

But it is failing with this error:

Selector creators expect all input-selectors to be functions, instead received the following types: [function, undefined]

      38 | );
      39 |
    > 40 | const getArrayWithAvailableGranularities = reselect.createSelector(
         |                                                     ^
      41 |     [getArray, granularitiesSelectors.getArray], (indicators, granularities) => _.map(indicators, (indicator) =>
      42 |         _.assign({}, indicator, {
      43 |             availableGranularities: _.map(_.filter(granularities, (granularity) =>

      at getDependencies (node_modules/reselect/lib/index.js:53:11)
      at Object.createSelector (node_modules/reselect/lib/index.js:71:24)
      at Object.<anonymous> (app/js/redux/entities/indicatorsSelectors.service.ts:40:53)
      at Object.<anonymous> (app/js/redux/selectors/filterGroupSelectors.service.ts:8:1)
      at Object.<anonymous> (app/js/redux/filterGroupReducers.service.ts:2:1)
      at Object.<anonymous> (app/js/redux/combineReducers.service.ts:12:1)
      at Object.<anonymous> (app/js/config/reduxStore.config.ts:3:1)
      at Object.<anonymous> (app/js/remote/bootstrap/bootstrap.service.js:5:1)
      at Object.<anonymous> (app/js/remote/rest/rest.service.js:2:1)
      at Object.<anonymous> (app/js/remote/rest/metadataRest.service.js:2:1)
      at Object.<anonymous> (app/js/common/metadata/dataset.service.ts:3:1)
      at Object.<anonymous> (app/js/redux/entities/granularitiesSelectors.service.ts:1:1)
      at Object.<anonymous> (app/js/components/map/mapActions.service.js:1:1)
      at Object.<anonymous> (app/js/components/map/map.service.js:3:1)
      at Object.<anonymous> (app/js/components/map/heatmap.service.ts:1:1)
      at Object.<anonymous> (app/js/components/map/heatmap.service.test.js:1:1)

getArrayWithAvailableGranularities is selector from indicatorsSelectors.service.ts, and uses getArray selector from granularitiesSelectors.

But this error appears only while running tests, not while running app.

theahmadzai commented 9 months ago

Any solution?