Skip to content

Commit 1e983cc

Browse files
committed
Convert "middleware not registered" warning into a full error
1 parent 8f48003 commit 1e983cc

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
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/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)