-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: removed filter-console dependency and fallback if process.env i…
…s not available Fixes #617
- Loading branch information
Showing
23 changed files
with
541 additions
and
186 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
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,40 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
// This verifies that if process.env is unavailable | ||
// then we still auto-wire up the afterEach for folks | ||
describe('skip auto cleanup (no process.env) tests', () => { | ||
const originalEnv = process.env | ||
let cleanupCalled = false | ||
let renderHook: ReactHooksRenderer['renderHook'] | ||
|
||
beforeAll(() => { | ||
process.env = { | ||
...process.env, | ||
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { | ||
throw new Error('expected') | ||
} | ||
} | ||
renderHook = (require('..') as ReactHooksRenderer).renderHook | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = originalEnv | ||
}) | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// This verifies that if process.env is unavailable | ||
// then we still auto-wire up the afterEach for folks | ||
describe('error output suppression (no process.env) tests', () => { | ||
const originalEnv = process.env | ||
const originalConsoleError = console.error | ||
|
||
beforeAll(() => { | ||
process.env = { | ||
...process.env, | ||
get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { | ||
throw new Error('expected') | ||
} | ||
} | ||
require('..') | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = originalEnv | ||
}) | ||
|
||
test('should not patch console.error', () => { | ||
expect(console.error).not.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,75 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
describe('error output suppression tests', () => { | ||
test('should not suppress relevant errors', () => { | ||
const consoleError = console.error | ||
console.error = jest.fn() | ||
|
||
const { suppressErrorOutput } = require('..') as ReactHooksRenderer | ||
|
||
try { | ||
const restoreConsole = suppressErrorOutput() | ||
|
||
console.error('expected') | ||
console.error(new Error('expected')) | ||
console.error('expected with args', new Error('expected')) | ||
|
||
restoreConsole() | ||
|
||
expect(console.error).toBeCalledWith('expected') | ||
expect(console.error).toBeCalledWith(new Error('expected')) | ||
expect(console.error).toBeCalledWith('expected with args', new Error('expected')) | ||
expect(console.error).toBeCalledTimes(3) | ||
} finally { | ||
console.error = consoleError | ||
} | ||
}) | ||
|
||
test('should allow console.error to be mocked', async () => { | ||
const { renderHook, act } = require('..') as ReactHooksRenderer | ||
const consoleError = console.error | ||
console.error = jest.fn() | ||
|
||
try { | ||
const { rerender, unmount } = renderHook( | ||
(stage) => { | ||
useEffect(() => { | ||
console.error(`expected in effect`) | ||
return () => { | ||
console.error(`expected in unmount`) | ||
} | ||
}, []) | ||
console.error(`expected in ${stage}`) | ||
}, | ||
{ | ||
initialProps: 'render' | ||
} | ||
) | ||
|
||
act(() => { | ||
console.error('expected in act') | ||
}) | ||
|
||
await act(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
console.error('expected in async act') | ||
}) | ||
|
||
rerender('rerender') | ||
|
||
unmount() | ||
|
||
expect(console.error).toBeCalledWith('expected in render') | ||
expect(console.error).toBeCalledWith('expected in effect') | ||
expect(console.error).toBeCalledWith('expected in act') | ||
expect(console.error).toBeCalledWith('expected in async act') | ||
expect(console.error).toBeCalledWith('expected in rerender') | ||
expect(console.error).toBeCalledWith('expected in unmount') | ||
expect(console.error).toBeCalledTimes(6) | ||
} finally { | ||
console.error = consoleError | ||
} | ||
}) | ||
}) |
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,40 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { ReactHooksRenderer } from '../../types/react' | ||
|
||
// This verifies that if process.env is unavailable | ||
// then we still auto-wire up the afterEach for folks | ||
describe('skip auto cleanup (no process.env) tests', () => { | ||
const originalEnv = process.env | ||
let cleanupCalled = false | ||
let renderHook: ReactHooksRenderer['renderHook'] | ||
|
||
beforeAll(() => { | ||
process.env = { | ||
...process.env, | ||
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { | ||
throw new Error('expected') | ||
} | ||
} | ||
renderHook = (require('..') as ReactHooksRenderer).renderHook | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = originalEnv | ||
}) | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(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
Oops, something went wrong.