Skip to content

Commit 83b4c33

Browse files
committed
Fix console spy related issues in injectEndpoints.test.tsx
1 parent 2ccd167 commit 83b4c33

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { noop } from '@internal/listenerMiddleware/utils'
12
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
2-
import { vi } from 'vitest'
33

44
const api = createApi({
55
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
66
endpoints: () => ({}),
77
})
88

99
describe('injectEndpoints', () => {
10-
test("query: overridding with `overrideEndpoints`='throw' throws an error", async () => {
10+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
11+
12+
afterEach(() => {
13+
consoleErrorSpy.mockClear()
14+
})
15+
16+
afterAll(() => {
17+
consoleErrorSpy.mockRestore()
18+
vi.unstubAllEnvs()
19+
})
20+
21+
test("query: overriding with `overrideEndpoints`='throw' throws an error", async () => {
1122
const extended = api.injectEndpoints({
1223
endpoints: (build) => ({
1324
injected: build.query<unknown, string>({
@@ -32,10 +43,8 @@ describe('injectEndpoints', () => {
3243
)
3344
})
3445

35-
test('query: overridding an endpoint with `overrideEndpoints`=false does nothing in production', async () => {
36-
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {})
37-
38-
process.env.NODE_ENV = 'development'
46+
test('query: overriding an endpoint with `overrideEndpoints`=false does nothing in production', async () => {
47+
vi.stubEnv('NODE_ENV', 'development')
3948

4049
const extended = api.injectEndpoints({
4150
endpoints: (build) => ({
@@ -54,15 +63,13 @@ describe('injectEndpoints', () => {
5463
}),
5564
})
5665

57-
expect(consoleMock).toHaveBeenCalledWith(
66+
expect(consoleErrorSpy).toHaveBeenCalledWith(
5867
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``,
5968
)
6069
})
6170

62-
test('query: overridding with `overrideEndpoints`=false logs an error in development', async () => {
63-
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {})
64-
65-
process.env.NODE_ENV = 'production'
71+
test('query: overriding with `overrideEndpoints`=false logs an error in development', async () => {
72+
vi.stubEnv('NODE_ENV', 'production')
6673

6774
const extended = api.injectEndpoints({
6875
endpoints: (build) => ({
@@ -81,6 +88,6 @@ describe('injectEndpoints', () => {
8188
}),
8289
})
8390

84-
expect(consoleMock).not.toHaveBeenCalled()
91+
expect(consoleErrorSpy).not.toHaveBeenCalled()
8592
})
8693
})

0 commit comments

Comments
 (0)