Skip to content

Commit 0337345

Browse files
committed
refactor: only add suppression in beforeEach block and move restoration to afterEach
1 parent 698579c commit 0337345

19 files changed

+143
-101
lines changed

src/core/console.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import filterConsole from 'filter-console'
22

33
function enableErrorOutputSuppression() {
4-
if (!process.env.RHTL_DISABLE_ERROR_FILTERING) {
5-
const restoreConsole = filterConsole(
6-
[
7-
/^The above error occurred in the <TestComponent> component:/, // error boundary output
8-
/^Error: Uncaught .+/ // jsdom output
9-
],
10-
{
11-
methods: ['error']
12-
}
13-
)
4+
// Automatically registers console error suppression and restoration in supported testing frameworks
5+
if (
6+
typeof beforeEach === 'function' &&
7+
typeof afterEach === 'function' &&
8+
!process.env.RHTL_DISABLE_ERROR_FILTERING
9+
) {
10+
let restoreConsole: (() => void) | null = null
1411

15-
// Automatically registers restoration in supported testing frameworks
16-
if (typeof afterAll === 'function') {
17-
afterAll(async () => {
18-
await new Promise((resolve) => setTimeout(resolve, 100))
19-
restoreConsole()
20-
})
21-
}
12+
beforeEach(() => {
13+
restoreConsole = filterConsole(
14+
[
15+
/^The above error occurred in the <TestComponent> component:/, // error boundary output
16+
/^Error: Uncaught .+/ // jsdom output
17+
],
18+
{
19+
methods: ['error']
20+
}
21+
)
22+
})
23+
24+
afterEach(() => restoreConsole?.())
2225
}
2326
}
2427

src/dom/__tests__/autoCleanup.disabled.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('skip auto cleanup (disabled) tests', () => {
1010

1111
beforeAll(() => {
1212
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
13-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1413
renderHook = (require('..') as ReactHooksRenderer).renderHook
1514
})
1615

src/dom/__tests__/autoCleanup.noAfterEach.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import { useEffect } from 'react'
22

33
import { ReactHooksRenderer } from '../../types/react'
44

5-
// This verifies that if RHTL_SKIP_AUTO_CLEANUP is set
5+
// This verifies that if afterEach is unavailable
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (no afterEach) tests', () => {
88
let cleanupCalled = false
99
let renderHook: (arg0: () => void) => void
1010

1111
beforeAll(() => {
1212
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
13-
// eslint-disable-next-line no-global-assign
1413
afterEach = false
15-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1614
renderHook = (require('..') as ReactHooksRenderer).renderHook
1715
})
1816

src/dom/__tests__/errorSuppression.disabled.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This verifies that if RHTL_DISABLE_ERROR_FILTERING is set
2+
// then we DON'T auto-wire up the afterEach for folks
13
describe('error output suppression (disabled) tests', () => {
24
const originalConsoleError = console.error
35

src/dom/__tests__/errorSuppression.noAfterAll.test.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noAfterEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
8+
afterEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noBeforeEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type
8+
beforeEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}

src/native/__tests__/autoCleanup.disabled.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('skip auto cleanup (disabled) tests', () => {
1010

1111
beforeAll(() => {
1212
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
13-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1413
renderHook = (require('..') as ReactHooksRenderer).renderHook
1514
})
1615

src/native/__tests__/autoCleanup.noAfterEach.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import { useEffect } from 'react'
22

33
import { ReactHooksRenderer } from '../../types/react'
44

5-
// This verifies that if RHTL_SKIP_AUTO_CLEANUP is set
5+
// This verifies that if afterEach is unavailable
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (no afterEach) tests', () => {
88
let cleanupCalled = false
99
let renderHook: (arg0: () => void) => void
1010

1111
beforeAll(() => {
1212
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
13-
// eslint-disable-next-line no-global-assign
1413
afterEach = false
15-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1614
renderHook = (require('..') as ReactHooksRenderer).renderHook
1715
})
1816

src/native/__tests__/errorSuppression.disabled.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This verifies that if RHTL_DISABLE_ERROR_FILTERING is set
2+
// then we DON'T auto-wire up the afterEach for folks
13
describe('error output suppression (disabled) tests', () => {
24
const originalConsoleError = console.error
35

src/native/__tests__/errorSuppression.noAfterAll.test.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noAfterEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
8+
afterEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noBeforeEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type
8+
beforeEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}

src/server/__tests__/autoCleanup.disabled.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('skip auto cleanup (disabled) tests', () => {
1010

1111
beforeAll(() => {
1212
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
13-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1413
renderHook = (require('..') as ReactHooksRenderer).renderHook
1514
})
1615

src/server/__tests__/autoCleanup.noAfterEach.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import { useEffect } from 'react'
22

33
import { ReactHooksRenderer } from '../../types/react'
44

5-
// This verifies that if RHTL_SKIP_AUTO_CLEANUP is set
5+
// This verifies that if afterEach is unavailable
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (no afterEach) tests', () => {
88
let cleanupCalled = false
99
let renderHook: (arg0: () => void) => void
1010

1111
beforeAll(() => {
1212
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
13-
// eslint-disable-next-line no-global-assign
1413
afterEach = false
15-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1614
renderHook = (require('..') as ReactHooksRenderer).renderHook
1715
})
1816

src/server/__tests__/errorSuppression.disabled.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This verifies that if RHTL_DISABLE_ERROR_FILTERING is set
2+
// then we DON'T auto-wire up the afterEach for folks
13
describe('error output suppression (disabled) tests', () => {
24
const originalConsoleError = console.error
35

src/server/__tests__/errorSuppression.noAfterAll.test.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noAfterEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
8+
afterEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This verifies that if afterEach is unavailable
2+
// then we DON'T auto-wire up the afterEach for folks
3+
describe('error output suppression (noBeforeEach) tests', () => {
4+
const originalConsoleError = console.error
5+
6+
beforeAll(() => {
7+
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type
8+
beforeEach = false
9+
})
10+
11+
describe('first', () => {
12+
test('should not patch console.error', () => {
13+
require('..')
14+
expect(console.error).toBe(originalConsoleError)
15+
})
16+
})
17+
})
18+
19+
export {}

0 commit comments

Comments
 (0)