-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export helper to manually suppress error output (#571)
* feat(console): export helper to manually suppress error output Fixes #564 * docs: added docs for `suppressErrorOutput` * fix(types): fix type of `suppressErrorOutput` to make result callable to restore console * test: add tests for `suppressErrorOutput` export * test: fix error suppression disabled tests for other renderers
- Loading branch information
Showing
14 changed files
with
112 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
// This verifies that if pure imports are used | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (pure) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] | ||
|
||
beforeAll(() => { | ||
require('../pure') | ||
suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} | ||
test('should manually patch console.error', () => { | ||
const restore = suppressErrorOutput() | ||
|
||
try { | ||
expect(console.error).not.toBe(originalConsoleError) | ||
} finally { | ||
restore() | ||
} | ||
|
||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
// This verifies that if pure imports are used | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (pure) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] | ||
|
||
beforeAll(() => { | ||
require('../pure') | ||
suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} | ||
test('should manually patch console.error', () => { | ||
const restore = suppressErrorOutput() | ||
|
||
try { | ||
expect(console.error).not.toBe(originalConsoleError) | ||
} finally { | ||
restore() | ||
} | ||
|
||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
// This verifies that if pure imports are used | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (pure) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] | ||
|
||
beforeAll(() => { | ||
require('../pure') | ||
suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} | ||
test('should manually patch console.error', () => { | ||
const restore = suppressErrorOutput() | ||
|
||
try { | ||
expect(console.error).not.toBe(originalConsoleError) | ||
} finally { | ||
restore() | ||
} | ||
|
||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters