Skip to content

Commit 0d02024

Browse files
authored
Merge pull request #2792 from reduxjs/feature/v1.9-more-beta-prep
2 parents 8f48003 + 498ff77 commit 0d02024

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ export function buildInitiate({
300300
;(middlewareWarning as any).triggered = true
301301
}
302302
if (registered === false) {
303-
console.warn(
303+
throw new Error(
304304
`Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
305-
Features like automatic cache collection, automatic refetching etc. will not be available.`
305+
You must add the middleware for RTK-Query to function correctly!`
306306
)
307307
}
308308
}

packages/toolkit/src/query/react/buildHooks.ts

+9
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,15 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
716716
})
717717
)
718718

719+
if (process.env.NODE_ENV !== 'production') {
720+
if (typeof returnedValue !== 'boolean') {
721+
throw new Error(
722+
`Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
723+
You must add the middleware for RTK-Query to function correctly!`
724+
)
725+
}
726+
}
727+
719728
currentRenderHasSubscription = !!returnedValue
720729
}
721730

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

+39-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ import {
1818
expectExactType,
1919
expectType,
2020
setupApiStore,
21+
withProvider,
2122
useRenderCounter,
2223
waitMs,
2324
} from './helpers'
2425
import { server } from './mocks/server'
2526
import type { AnyAction } from 'redux'
2627
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState'
27-
import { createListenerMiddleware, SerializedError } from '@reduxjs/toolkit'
28+
import {
29+
createListenerMiddleware,
30+
SerializedError,
31+
configureStore,
32+
} from '@reduxjs/toolkit'
2833
import { renderHook } from '@testing-library/react'
2934
import { delay } from '../../utils'
3035

@@ -651,6 +656,39 @@ describe('hooks tests', () => {
651656
const res = await resPromise
652657
expect(res.data!.amount).toBeGreaterThan(originalAmount)
653658
})
659+
660+
describe('Hook middleware requirements', () => {
661+
let mock: jest.SpyInstance
662+
663+
beforeEach(() => {
664+
mock = jest.spyOn(console, 'error').mockImplementation(() => {})
665+
})
666+
667+
afterEach(() => {
668+
mock.mockReset()
669+
})
670+
671+
test('Throws error if middleware is not added to the store', async () => {
672+
const store = configureStore({
673+
reducer: {
674+
[api.reducerPath]: api.reducer,
675+
},
676+
})
677+
678+
const doRender = () => {
679+
const { result } = renderHook(
680+
() => api.endpoints.getIncrementedAmount.useQuery(),
681+
{
682+
wrapper: withProvider(store),
683+
}
684+
)
685+
}
686+
687+
expect(doRender).toThrowError(
688+
/Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store/
689+
)
690+
})
691+
})
654692
})
655693

656694
describe('useLazyQuery', () => {

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

+27-19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ beforeEach(() => {
5252
;[api1, api1_2, api2] = createApis()
5353
})
5454

55+
const reMatchMissingMiddlewareError =
56+
/Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store/
57+
5558
describe('missing middleware', () => {
5659
test.each([
5760
['development', true],
@@ -61,13 +64,14 @@ describe('missing middleware', () => {
6164
const store = configureStore({
6265
reducer: { [api1.reducerPath]: api1.reducer },
6366
})
64-
store.dispatch(api1.endpoints.q1.initiate(undefined))
65-
expect(getLog().log).toBe(
66-
shouldWarn
67-
? `Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
68-
Features like automatic cache collection, automatic refetching etc. will not be available.`
69-
: ''
70-
)
67+
const doDispatch = () => {
68+
store.dispatch(api1.endpoints.q1.initiate(undefined))
69+
}
70+
if (shouldWarn) {
71+
expect(doDispatch).toThrowError(reMatchMissingMiddlewareError)
72+
} else {
73+
expect(doDispatch).not.toThrowError()
74+
}
7175
})
7276

7377
test('does not warn if middleware is not missing', () => {
@@ -83,11 +87,12 @@ Features like automatic cache collection, automatic refetching etc. will not be
8387
const store = configureStore({
8488
reducer: { [api1.reducerPath]: api1.reducer },
8589
})
86-
store.dispatch(api1.endpoints.q1.initiate(undefined))
87-
store.dispatch(api1.endpoints.q1.initiate(undefined))
88-
expect(getLog().log)
89-
.toBe(`Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
90-
Features like automatic cache collection, automatic refetching etc. will not be available.`)
90+
const doDispatch = () => {
91+
store.dispatch(api1.endpoints.q1.initiate(undefined))
92+
}
93+
94+
expect(doDispatch).toThrowError(reMatchMissingMiddlewareError)
95+
expect(doDispatch).not.toThrowError()
9196
})
9297

9398
test('warns multiple times for multiple apis', () => {
@@ -97,13 +102,16 @@ Features like automatic cache collection, automatic refetching etc. will not be
97102
[api2.reducerPath]: api2.reducer,
98103
},
99104
})
100-
store.dispatch(api1.endpoints.q1.initiate(undefined))
101-
store.dispatch(api2.endpoints.q1.initiate(undefined))
102-
expect(getLog().log)
103-
.toBe(`Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
104-
Features like automatic cache collection, automatic refetching etc. will not be available.
105-
Warning: Middleware for RTK-Query API at reducerPath "api2" has not been added to the store.
106-
Features like automatic cache collection, automatic refetching etc. will not be available.`)
105+
const doDispatch1 = () => {
106+
store.dispatch(api1.endpoints.q1.initiate(undefined))
107+
}
108+
const doDispatch2 = () => {
109+
store.dispatch(api2.endpoints.q1.initiate(undefined))
110+
}
111+
expect(doDispatch1).toThrowError(reMatchMissingMiddlewareError)
112+
expect(doDispatch2).toThrowError(
113+
/Warning: Middleware for RTK-Query API at reducerPath "api2" has not been added to the store/
114+
)
107115
})
108116
})
109117

0 commit comments

Comments
 (0)