reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Debugging errors in selectors

Open kjeske opened this issue 10 years ago • 3 comments

Today I came across strange problem with debugging my redux app. I started getting exceptions like:

TypeError: Cannot read property 'replaceChild' of null

I spent some time on investigation where is the issue, as the debugger didn't say anything about the real cause (replaceChild error was of course just a result of another thing). After some time I noticed that my selector contained a bug:

export const getSelectedItem = createSelector(
  [getItems, getSelectedItemId],
  (items, itemId) => {
    return items.finds(p => p.id === itemId)
                     ^
                    bug
  }
)

I used finds instead of find. Wow, that was hard to debug and my app is really small right now. I started wondering what will happen in bigger app. I know that in many cases the tests will catch such problems, but sometimes when we are playing with the code it's easy to stuck in a mud for long hours.

Is that just my case or it is common to have such debugging problems?

kjeske avatar Jul 04 '16 19:07 kjeske

I'm going to close this as I don't think there is anything actionable for Reselect here, as this would happen in a non-Reselect selector also. I think bugs manifesting themselves far from their source is just a weakness of dynamic languages. You might want to try unit testing your selectors as you go, or consider using Flow (or Typescript):

/* @flow */
import { createSelector } from 'reselect';

const getItems = state => state.items
const getSelectedItemId = state => state.selectedItemId

const getSelectedItem = createSelector(
  [getItems, getSelectedItemId],
  (items : Array<any>, itemId) => {
    return items.finds(p => p.id === itemId)
  }
)
% flow
src/index.js:10
 10:     return items.finds(p => p.id === itemId)
                      ^^^^^ property `finds`. Property not found in
 10:     return items.finds(p => p.id === itemId)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Array


Found 1 error

ellbee avatar Jul 04 '16 21:07 ellbee

Thanks for the answer!

kjeske avatar Jul 04 '16 21:07 kjeske

@kjeske Please have a look at the #191, do you think that something like that would help?

josepot avatar Nov 11 '16 04:11 josepot