Skip to content

Commit 7f69223

Browse files
committed
test: silence error console in tests where known errors are thrown
1 parent 31f6b2d commit 7f69223

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,9 @@ describe('useQuery', () => {
28882888
})
28892889

28902890
it('should set status to error if queryFn throws', async () => {
2891+
const consoleMock = vi
2892+
.spyOn(console, 'error')
2893+
.mockImplementation(() => undefined)
28912894
const key = queryKey()
28922895

28932896
function Page() {
@@ -2911,6 +2914,8 @@ describe('useQuery', () => {
29112914

29122915
await waitFor(() => rendered.getByText('error'))
29132916
await waitFor(() => rendered.getByText('Error test jaylen'))
2917+
2918+
consoleMock.mockRestore()
29142919
})
29152920

29162921
it('should throw error if queryFn throws and throwOnError is in use', async () => {

packages/solid-query/src/__tests__/createMutation.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ describe('createMutation', () => {
6464
})
6565

6666
it('should be able to reset `error`', async () => {
67+
const consoleMock = vi
68+
.spyOn(console, 'error')
69+
.mockImplementation(() => undefined)
70+
6771
function Page() {
6872
const mutation = createMutation<string, Error>(() => ({
6973
mutationFn: () => {
@@ -105,6 +109,8 @@ describe('createMutation', () => {
105109
await waitFor(() => {
106110
expect(screen.queryByRole('heading')).toBeNull()
107111
})
112+
113+
consoleMock.mockRestore()
108114
})
109115

110116
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
@@ -781,6 +787,10 @@ describe('createMutation', () => {
781787
})
782788

783789
it('should be able to throw an error when throwOnError is set to true', async () => {
790+
const consoleMock = vi
791+
.spyOn(console, 'error')
792+
.mockImplementation(() => undefined)
793+
784794
function Page() {
785795
const mutation = createMutation<string, Error>(() => ({
786796
mutationFn: () => {
@@ -817,9 +827,15 @@ describe('createMutation', () => {
817827
await waitFor(() => {
818828
expect(screen.queryByText('error')).not.toBeNull()
819829
})
830+
831+
consoleMock.mockRestore()
820832
})
821833

822834
it('should be able to throw an error when throwOnError is a function that returns true', async () => {
835+
const consoleMock = vi
836+
.spyOn(console, 'error')
837+
.mockImplementation(() => undefined)
838+
823839
let boundary = false
824840
function Page() {
825841
const mutation = createMutation<string, Error>(() => ({
@@ -867,6 +883,8 @@ describe('createMutation', () => {
867883
await waitFor(() => {
868884
expect(screen.queryByText('error boundary')).not.toBeNull()
869885
})
886+
887+
consoleMock.mockRestore()
870888
})
871889

872890
it('should pass meta to mutation', async () => {

packages/solid-query/src/__tests__/createQuery.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,10 @@ describe('createQuery', () => {
24462446
it('should set status to error if queryFn throws', async () => {
24472447
const key = queryKey()
24482448

2449+
const consoleMock = vi
2450+
.spyOn(console, 'error')
2451+
.mockImplementation(() => undefined)
2452+
24492453
function Page() {
24502454
const state = createQuery(() => ({
24512455
queryKey: key,
@@ -2471,11 +2475,17 @@ describe('createQuery', () => {
24712475

24722476
await waitFor(() => screen.getByText('error'))
24732477
await waitFor(() => screen.getByText('Error test jaylen'))
2478+
2479+
consoleMock.mockRestore()
24742480
})
24752481

24762482
it('should throw error if queryFn throws and throwOnError is in use', async () => {
24772483
const key = queryKey()
24782484

2485+
const consoleMock = vi
2486+
.spyOn(console, 'error')
2487+
.mockImplementation(() => undefined)
2488+
24792489
function Page() {
24802490
const state = createQuery(() => ({
24812491
queryKey: key,
@@ -2501,6 +2511,8 @@ describe('createQuery', () => {
25012511
))
25022512

25032513
await waitFor(() => screen.getByText('error boundary'))
2514+
2515+
consoleMock.mockRestore()
25042516
})
25052517

25062518
it('should update with data if we observe no properties and throwOnError', async () => {

packages/solid-query/src/__tests__/suspense.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ describe("useQuery's in Suspense mode", () => {
453453
it('should throw errors to the error boundary by default', async () => {
454454
const key = queryKey()
455455

456+
const consoleMock = vi
457+
.spyOn(console, 'error')
458+
.mockImplementation(() => undefined)
459+
456460
function Page() {
457461
const state = createQuery(() => ({
458462
queryKey: key,
@@ -497,6 +501,8 @@ describe("useQuery's in Suspense mode", () => {
497501

498502
await waitFor(() => screen.getByText('Loading...'))
499503
await waitFor(() => screen.getByText('error boundary'))
504+
505+
consoleMock.mockRestore()
500506
})
501507

502508
it('should not throw errors to the error boundary when throwOnError: false', async () => {
@@ -551,6 +557,10 @@ describe("useQuery's in Suspense mode", () => {
551557
it('should throw errors to the error boundary when a throwOnError function returns true', async () => {
552558
const key = queryKey()
553559

560+
const consoleMock = vi
561+
.spyOn(console, 'error')
562+
.mockImplementation(() => undefined)
563+
554564
function Page() {
555565
const state = createQuery(() => ({
556566
queryKey: key,
@@ -595,6 +605,8 @@ describe("useQuery's in Suspense mode", () => {
595605

596606
await waitFor(() => screen.getByText('Loading...'))
597607
await waitFor(() => screen.getByText('error boundary'))
608+
609+
consoleMock.mockRestore()
598610
})
599611

600612
it('should not throw errors to the error boundary when a throwOnError function returns false', async () => {
@@ -696,6 +708,10 @@ describe("useQuery's in Suspense mode", () => {
696708
it('should error catched in error boundary without infinite loop', async () => {
697709
const key = queryKey()
698710

711+
const consoleMock = vi
712+
.spyOn(console, 'error')
713+
.mockImplementation(() => undefined)
714+
699715
let succeed = true
700716

701717
function Page() {
@@ -756,11 +772,17 @@ describe("useQuery's in Suspense mode", () => {
756772
fireEvent.click(screen.getByLabelText('fail'))
757773
// render error boundary fallback (error boundary)
758774
await waitFor(() => screen.getByText('error boundary'))
775+
776+
consoleMock.mockRestore()
759777
})
760778

761779
it('should error catched in error boundary without infinite loop when query keys changed', async () => {
762780
let succeed = true
763781

782+
const consoleMock = vi
783+
.spyOn(console, 'error')
784+
.mockImplementation(() => undefined)
785+
764786
function Page() {
765787
const [key, setKey] = createSignal(0)
766788

@@ -814,9 +836,15 @@ describe("useQuery's in Suspense mode", () => {
814836
fireEvent.click(screen.getByLabelText('fail'))
815837
// render error boundary fallback (error boundary)
816838
await waitFor(() => screen.getByText('error boundary'))
839+
840+
consoleMock.mockRestore()
817841
})
818842

819843
it('should error catched in error boundary without infinite loop when enabled changed', async () => {
844+
const consoleMock = vi
845+
.spyOn(console, 'error')
846+
.mockImplementation(() => undefined)
847+
820848
function Page() {
821849
const queryKeys = '1'
822850
const [enabled, setEnabled] = createSignal(false)
@@ -874,6 +902,8 @@ describe("useQuery's in Suspense mode", () => {
874902

875903
// render error boundary fallback (error boundary)
876904
await waitFor(() => screen.getByText('error boundary'))
905+
906+
consoleMock.mockRestore()
877907
})
878908

879909
it('should render the correct amount of times in Suspense mode when gcTime is set to 0', async () => {

0 commit comments

Comments
 (0)