Skip to content

Commit

Permalink
Improve error message for bad dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Oct 20, 2021
1 parent 8ac6fd2 commit f38c8d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ function getDependencies(funcs: unknown[]) {
const dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs

if (!dependencies.every(dep => typeof dep === 'function')) {
const dependencyTypes = dependencies.map(dep => typeof dep).join(', ')
const dependencyTypes = dependencies
.map(dep =>
typeof dep === 'function'
? `function ${dep.name || 'unnamed'}()`
: typeof dep
)
.join(', ')

throw new Error(
'Selector creators expect all input-selectors to be functions, ' +
`instead received the following types: [${dependencyTypes}]`
`createSelector expects all input-selectors to be functions, but received the following types: [${dependencyTypes}]`
)
}

Expand Down Expand Up @@ -76,7 +82,7 @@ export function createSelectorCreator<

if (typeof resultFunc !== 'function') {
throw new Error(
`createSelector expected an output function after the inputs, but received: [${typeof resultFunc}]`
`createSelector expects an output function after the inputs, but received: [${typeof resultFunc}]`
)
}

Expand Down
15 changes: 11 additions & 4 deletions test/test_selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,24 @@ describe('Basic selector behavior', () => {

test('basic selector invalid input selector', () => {
expect(() =>
// @ts-ignore
createSelector(
// @ts-ignore
(state: StateAB) => state.a,
function input2(state: StateAB) {
return state.b
},
'not a function',
(a, b) => a + b
)
).toThrow(/input-selectors to be functions.*function, string/)
).toThrow(
'createSelector expects all input-selectors to be functions, but received the following types: [function unnamed(), function input2(), string]'
)

expect(() =>
// @ts-ignore
createSelector((state: StateAB) => state.a, 'not a function')
).toThrow(
'createSelector expected an output function after the inputs, but received: [string]'
'createSelector expects an output function after the inputs, but received: [string]'
)
})

Expand Down Expand Up @@ -662,7 +667,9 @@ describe('createStructureSelector', () => {
// @ts-expect-error
c: 'd'
})
).toThrow(/input-selectors to be functions.*function, string/)
).toThrow(
'createSelector expects all input-selectors to be functions, but received the following types: [function a(), string]'
)
})

test('structured selector with custom selector creator', () => {
Expand Down

0 comments on commit f38c8d5

Please sign in to comment.