Skip to content

Commit

Permalink
chore(tests/utils): suppress error boundary error log
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkx committed Sep 2, 2023
1 parent ccba918 commit 9a679fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 19 additions & 9 deletions tests/react/error.test.tsx
Original file line number Diff line number Diff line change
@@ -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(() => {

Check failure on line 18 in tests/react/error.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Cannot use namespace 'SpyInstance' as a type.

Check failure on line 18 in tests/react/error.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Cannot use namespace 'SpyInstance' as a type.

Check failure on line 18 in tests/react/error.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Cannot use namespace 'SpyInstance' as a type.
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<
Expand Down
26 changes: 25 additions & 1 deletion tests/react/vanilla-utils/atomWithObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<
Expand Down

0 comments on commit 9a679fc

Please sign in to comment.