diff --git a/tests/react/error.test.tsx b/tests/react/error.test.tsx index 2267e0745e..4f821dafa9 100644 --- a/tests/react/error.test.tsx +++ b/tests/react/error.test.tsx @@ -1,23 +1,33 @@ import { Component, StrictMode, Suspense, useEffect, useState } from 'react' import type { ReactNode } from 'react' import { fireEvent, render, waitFor } from '@testing-library/react' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + SpyInstance, + afterEach, + beforeEach, + describe, + expect, + it, + vi, +} from 'vitest' import { useAtom } from 'jotai/react' import { atom } from 'jotai/vanilla' -const consoleError = console.error const errorMessages: string[] = [] +let consoleErrorSpy: SpyInstance beforeEach(() => { errorMessages.splice(0) - console.error = vi.fn((err: string) => { - const match = /^(.*?)(\n|$)/.exec(err) - if (match?.[1]) { - errorMessages.push(match[1]) - } - }) + consoleErrorSpy = vi + .spyOn(console, 'error') + .mockImplementation((err: string) => { + const match = /^(.*?)(\n|$)/.exec(err) + if (match?.[1]) { + errorMessages.push(match[1]) + } + }) }) afterEach(() => { - console.error = consoleError + consoleErrorSpy.mockRestore() }) class ErrorBoundary extends Component< diff --git a/tests/react/vanilla-utils/atomWithObservable.test.tsx b/tests/react/vanilla-utils/atomWithObservable.test.tsx index c2bda7809a..a33efa447a 100644 --- a/tests/react/vanilla-utils/atomWithObservable.test.tsx +++ b/tests/react/vanilla-utils/atomWithObservable.test.tsx @@ -2,12 +2,21 @@ import { Component, StrictMode, Suspense, useState } from 'react' import type { ReactElement, ReactNode } from 'react' import { act, fireEvent, render, waitFor } from '@testing-library/react' import { BehaviorSubject, Observable, Subject, delay, map, of } from 'rxjs' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + SpyInstance, + afterEach, + beforeEach, + describe, + expect, + it, + vi, +} from 'vitest' import { fromValue, makeSubject, pipe, toObservable } from 'wonka' import { useAtom, useAtomValue, useSetAtom } from 'jotai/react' import { atom, createStore } from 'jotai/vanilla' import { atomWithObservable } from 'jotai/vanilla/utils' +let consoleErrorSpy: SpyInstance beforeEach(() => { vi.useFakeTimers({ shouldAdvanceTime: true }) // A workaround for missing performance.mark after using fake timers @@ -17,11 +26,26 @@ beforeEach(() => { performance.clearMarks = (() => {}) as any performance.clearMeasures = (() => {}) as any } + // suppress ErrorBoundary error log + const consoleError = console.error + consoleErrorSpy = vi + .spyOn(console, 'error') + .mockImplementation((message, ...optionalParams: unknown[]) => { + if ( + typeof message === 'string' && + (message.includes('Error: Uncaught [Error: Test Error]') || + message.includes('at ErrorBoundary')) + ) { + return + } + return consoleError(message, ...optionalParams) + }) }) afterEach(() => { vi.runAllTimers() vi.useRealTimers() + consoleErrorSpy.mockRestore() }) class ErrorBoundary extends Component<