Skip to content

Commit ec05907

Browse files
authored
Merge pull request #2850 from reduxjs/feature/middleware-registration-hooks
2 parents b367946 + 150a93e commit ec05907

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,23 @@ export function buildInitiate({
291291
Object.values(runningMutations.get(dispatch) || {}).filter(isNotNullish)
292292
}
293293

294-
function middlewareWarning(getState: () => RootState<{}, string, string>) {
294+
function middlewareWarning(dispatch: Dispatch) {
295295
if (process.env.NODE_ENV !== 'production') {
296296
if ((middlewareWarning as any).triggered) return
297-
const registered =
298-
getState()[api.reducerPath]?.config?.middlewareRegistered
299-
if (registered !== undefined) {
300-
;(middlewareWarning as any).triggered = true
301-
}
302-
if (registered === false) {
297+
const registered:
298+
| ReturnType<typeof api.internalActions.internal_probeSubscription>
299+
| boolean = dispatch(
300+
api.internalActions.internal_probeSubscription({
301+
queryCacheKey: 'DOES_NOT_EXIST',
302+
requestId: 'DUMMY_REQUEST_ID',
303+
})
304+
)
305+
306+
;(middlewareWarning as any).triggered = true
307+
308+
// The RTKQ middleware _should_ always return a boolean for `probeSubscription`
309+
if (typeof registered !== 'boolean') {
310+
// Otherwise, must not have been added
303311
throw new Error(
304312
`Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
305313
You must add the middleware for RTK-Query to function correctly!`
@@ -346,7 +354,7 @@ You must add the middleware for RTK-Query to function correctly!`
346354
const thunkResult = dispatch(thunk)
347355
const stateAfter = selector(getState())
348356

349-
middlewareWarning(getState)
357+
middlewareWarning(dispatch)
350358

351359
const { requestId, abort } = thunkResult
352360

@@ -440,7 +448,7 @@ You must add the middleware for RTK-Query to function correctly!`
440448
fixedCacheKey,
441449
})
442450
const thunkResult = dispatch(thunk)
443-
middlewareWarning(getState)
451+
middlewareWarning(dispatch)
444452
const { requestId, abort, unwrap } = thunkResult
445453
const returnValuePromise = thunkResult
446454
.unwrap()

packages/toolkit/src/query/tests/devWarnings.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,16 @@ describe('missing reducer', () => {
186186
})
187187
})
188188

189-
test('warns only for reducer if everything is missing', async () => {
189+
test('warns for reducer and also throws error if everything is missing', async () => {
190190
const store = configureStore({
191191
reducer: { x: () => 0 },
192192
})
193193
// @ts-expect-error
194194
api1.endpoints.q1.select(undefined)(store.getState())
195-
await store.dispatch(api1.endpoints.q1.initiate(undefined))
195+
const doDispatch = () => {
196+
store.dispatch(api1.endpoints.q1.initiate(undefined))
197+
}
198+
expect(doDispatch).toThrowError(reMatchMissingMiddlewareError)
196199
expect(getLog().log).toBe(
197200
'Error: No data found at `state.api`. Did you forget to add the reducer to the store?'
198201
)

0 commit comments

Comments
 (0)