-
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: Remove node specific dependencies and code to better support te…
…sting in browser environments * feat: removed filter-console dependency and fallback if process.env is not available (#624) * fix: protect import helpers for setting env variables and comment why try/catch is being used BREAKING CHANGE: `suppressErrorOutput` will now work when explicitly called, even if the `RHTL_DISABLE_ERROR_FILTERING` env variable has been set Fixes #617
- Loading branch information
Showing
25 changed files
with
559 additions
and
188 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 |
---|---|---|
@@ -1 +1,8 @@ | ||
process.env.RHTL_DISABLE_ERROR_FILTERING = true | ||
try { | ||
process.env.RHTL_DISABLE_ERROR_FILTERING = true | ||
} catch { | ||
// falling back in the case that process.env.RHTL_DISABLE_ERROR_FILTERING cannot be accessed (e.g. browser environment) | ||
console.warn( | ||
'Could not disable error filtering as process.env.RHTL_DISABLE_ERROR_FILTERING could not be accessed.' | ||
) | ||
} |
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 +1,8 @@ | ||
process.env.RHTL_SKIP_AUTO_CLEANUP = true | ||
try { | ||
process.env.RHTL_SKIP_AUTO_CLEANUP = true | ||
} catch { | ||
// falling back in the case that process.env.RHTL_SKIP_AUTO_CLEANUP cannot be accessed (e.g. browser environment) | ||
console.warn( | ||
'Could not skip auto cleanup as process.env.RHTL_SKIP_AUTO_CLEANUP could not be accessed.' | ||
) | ||
} |
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.