Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: only suppress console.error for non-pure imports (#549)
* fix: only suppress console.error for non-pure imports * refactor: remove unused promise util * chore: fix tests for error suppression to * docs: update docs to with more detail on side effects * refactor: only add suppression in beforeEach block and move restoration to afterEach * chore: refactor error suppression tests to require in setup so hooks can actually be registered * chore: added additional tests to ensure pure imports don't add side effects * refactor: clean up unnecessary additional types in internal console suppression function * docs: remove link in API reference docs Fixes #546
- Loading branch information
Showing
34 changed files
with
551 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
process.env.RHTL_DISABLE_ERROR_FILTERING = true |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import filterConsole from 'filter-console' | ||
|
||
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 | ||
|
||
beforeEach(() => { | ||
restoreConsole = filterConsole( | ||
[ | ||
/^The above error occurred in the <TestComponent> component:/, // error boundary output | ||
/^Error: Uncaught .+/ // jsdom output | ||
], | ||
{ | ||
methods: ['error'] | ||
} | ||
) | ||
}) | ||
|
||
afterEach(() => restoreConsole?.()) | ||
} | ||
} | ||
|
||
export { enableErrorOutputSuppression } |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect } from 'react' | ||
|
||
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('skip auto cleanup (pure) tests', () => { | ||
let cleanupCalled = false | ||
let renderHook: ReactHooksRenderer['renderHook'] | ||
|
||
beforeAll(() => { | ||
renderHook = (require('../pure') as ReactHooksRenderer).renderHook | ||
}) | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(false) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This verifies that if RHTL_DISABLE_ERROR_FILTERING is set | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (disabled) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
beforeAll(() => { | ||
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
require('..') | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// This verifies that if afterEach is unavailable | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (noAfterEach) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
beforeAll(() => { | ||
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type | ||
afterEach = false | ||
require('..') | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// This verifies that if afterEach is unavailable | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('error output suppression (noBeforeEach) tests', () => { | ||
const originalConsoleError = console.error | ||
|
||
beforeAll(() => { | ||
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type | ||
beforeEach = false | ||
require('..') | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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 | ||
|
||
beforeAll(() => { | ||
require('../pure') | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).toBe(originalConsoleError) | ||
}) | ||
}) | ||
|
||
export {} |
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,5 +1,7 @@ | ||
import { autoRegisterCleanup } from '../core/cleanup' | ||
import { enableErrorOutputSuppression } from '../core/console' | ||
|
||
autoRegisterCleanup() | ||
enableErrorOutputSuppression() | ||
|
||
export * from './pure' |
Oops, something went wrong.