-
Notifications
You must be signed in to change notification settings - Fork 230
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
Fixes #546
- Loading branch information
Showing
15 changed files
with
310 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import filterConsole from 'filter-console' | ||
|
||
function enableErrorOutputSuppression() { | ||
if (!process.env.RHTL_DISABLE_ERROR_FILTERING) { | ||
const restoreConsole = filterConsole( | ||
[ | ||
/^The above error occurred in the <TestComponent> component:/, // error boundary output | ||
/^Error: Uncaught .+/ // jsdom output | ||
], | ||
{ | ||
methods: ['error'] | ||
} | ||
) | ||
|
||
// Automatically registers restoration in supported testing frameworks | ||
if (typeof afterAll === 'function') { | ||
afterAll(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { renderHook } from '..' | ||
|
||
describe('error output suppression (disabled) tests', () => { | ||
function useError(throwError?: boolean) { | ||
if (throwError) { | ||
throw new Error('expected') | ||
} | ||
return true | ||
} | ||
|
||
const originalConsoleError = console.error | ||
const mockConsoleError = jest.fn() | ||
|
||
beforeAll(() => { | ||
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' | ||
}) | ||
|
||
beforeEach(() => { | ||
console.error = mockConsoleError | ||
}) | ||
|
||
afterEach(() => { | ||
console.error = originalConsoleError | ||
}) | ||
|
||
test('should not suppress error output', () => { | ||
const { result } = renderHook(() => useError(true)) | ||
|
||
expect(result.error).toEqual(Error('expected')) | ||
expect(mockConsoleError).toBeCalledWith( | ||
expect.stringMatching(/^Error: Uncaught \[Error: expected\]/), | ||
expect.any(Error) | ||
) | ||
expect(mockConsoleError).toBeCalledWith( | ||
expect.stringMatching(/^The above error occurred in the <TestComponent> component:/) | ||
) | ||
expect(mockConsoleError).toBeCalledTimes(2) | ||
}) | ||
}) |
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,35 @@ | ||
import { renderHook } from '../pure' | ||
|
||
describe('error output suppression (pure) tests', () => { | ||
function useError(throwError?: boolean) { | ||
if (throwError) { | ||
throw new Error('expected') | ||
} | ||
return true | ||
} | ||
|
||
const originalConsoleError = console.error | ||
const mockConsoleError = jest.fn() | ||
|
||
beforeEach(() => { | ||
console.error = mockConsoleError | ||
}) | ||
|
||
afterEach(() => { | ||
console.error = originalConsoleError | ||
}) | ||
|
||
test('should not suppress error output', () => { | ||
const { result } = renderHook(() => useError(true)) | ||
|
||
expect(result.error).toEqual(Error('expected')) | ||
expect(mockConsoleError).toBeCalledWith( | ||
expect.stringMatching(/^Error: Uncaught \[Error: expected\]/), | ||
expect.any(Error) | ||
) | ||
expect(mockConsoleError).toBeCalledWith( | ||
expect.stringMatching(/^The above error occurred in the <TestComponent> component:/) | ||
) | ||
expect(mockConsoleError).toBeCalledTimes(2) | ||
}) | ||
}) |
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' |
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,5 +1,7 @@ | ||
import { autoRegisterCleanup } from './core/cleanup' | ||
import { enableErrorOutputSuppression } from './core/console' | ||
|
||
autoRegisterCleanup() | ||
enableErrorOutputSuppression() | ||
|
||
export * from './pure' |
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,5 +1,7 @@ | ||
import { autoRegisterCleanup } from '../core/cleanup' | ||
import { enableErrorOutputSuppression } from '../core/console' | ||
|
||
autoRegisterCleanup() | ||
enableErrorOutputSuppression() | ||
|
||
export * from './pure' |
Oops, something went wrong.