Skip to content

Commit

Permalink
feat(console): export helper to manually suppress error output
Browse files Browse the repository at this point in the history
Fixes #564
  • Loading branch information
mpeyper committed Mar 1, 2021
1 parent 7f6e4bb commit 4afda1d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
38 changes: 21 additions & 17 deletions src/core/console.ts
@@ -1,28 +1,32 @@
import filterConsole from 'filter-console'

function suppressErrorOutput() {
if (process.env.RHTL_DISABLE_ERROR_FILTERING) {
return () => {}
}

return filterConsole(
[
/^The above error occurred in the <TestComponent> component:/, // error boundary output
/^Error: Uncaught .+/ // jsdom output
],
{
methods: ['error']
}
)
}

function enableErrorOutputSuppression() {
// Automatically registers console error suppression and restoration in supported testing frameworks
if (
typeof beforeEach === 'function' &&
typeof afterEach === 'function' &&
!process.env.RHTL_DISABLE_ERROR_FILTERING
) {
let restoreConsole: () => void
if (typeof beforeEach === 'function' && typeof afterEach === 'function') {
let restoreConsole!: () => void

beforeEach(() => {
restoreConsole = filterConsole(
[
/^The above error occurred in the <TestComponent> component:/, // error boundary output
/^Error: Uncaught .+/ // jsdom output
],
{
methods: ['error']
}
)
restoreConsole = suppressErrorOutput()
})

afterEach(() => restoreConsole?.())
afterEach(() => restoreConsole())
}
}

export { enableErrorOutputSuppression }
export { enableErrorOutputSuppression, suppressErrorOutput }
3 changes: 2 additions & 1 deletion src/core/index.ts
Expand Up @@ -2,6 +2,7 @@ import { CreateRenderer, Renderer, RenderResult, RenderHookOptions } from '../ty

import { asyncUtils } from './asyncUtils'
import { cleanup, addCleanup, removeCleanup } from './cleanup'
import { suppressErrorOutput } from './console'

function resultContainer<TValue>() {
const results: Array<{ value?: TValue; error?: Error }> = []
Expand Down Expand Up @@ -81,4 +82,4 @@ function createRenderHook<
return renderHook
}

export { createRenderHook, cleanup, addCleanup, removeCleanup }
export { createRenderHook, cleanup, addCleanup, removeCleanup, suppressErrorOutput }
2 changes: 1 addition & 1 deletion src/dom/__tests__/errorSuppression.disabled.test.ts
Expand Up @@ -5,10 +5,10 @@ describe('error output suppression (disabled) tests', () => {

beforeAll(() => {
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true'
require('..')
})

test('should not patch console.error', () => {
require('..')
expect(console.error).toBe(originalConsoleError)
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/dom/pure.ts
Expand Up @@ -37,6 +37,6 @@ const renderHook = createRenderHook(createDomRenderer)

export { renderHook, act }

export { cleanup, addCleanup, removeCleanup } from '../core'
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'

export * from '../types/react'
2 changes: 1 addition & 1 deletion src/native/pure.ts
Expand Up @@ -36,6 +36,6 @@ const renderHook = createRenderHook(createNativeRenderer)

export { renderHook, act }

export { cleanup, addCleanup, removeCleanup } from '../core'
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'

export * from '../types/react'
4 changes: 2 additions & 2 deletions src/pure.ts
Expand Up @@ -32,8 +32,8 @@ function getRenderer() {
}
}

const { renderHook, act, cleanup, addCleanup, removeCleanup } = getRenderer()
const { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput } = getRenderer()

export { renderHook, act, cleanup, addCleanup, removeCleanup }
export { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput }

export * from './types/react'
2 changes: 1 addition & 1 deletion src/server/pure.ts
Expand Up @@ -61,6 +61,6 @@ const renderHook = createRenderHook(createServerRenderer)

export { renderHook, act }

export { cleanup, addCleanup, removeCleanup } from '../core'
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'

export * from '../types/react'
1 change: 1 addition & 0 deletions src/types/react.ts
Expand Up @@ -26,6 +26,7 @@ export type ReactHooksRenderer = {
cleanup: () => void
addCleanup: (callback: CleanupCallback) => () => void
removeCleanup: (callback: CleanupCallback) => void
suppressErrorOutput: () => void
}

export * from '.'

0 comments on commit 4afda1d

Please sign in to comment.