Skip to content

Commit 79b5559

Browse files
test: Update tests to be compatible with Jest 27 (#639)
* chore(deps-dev): bump kcd-scripts from 10.0.0 to 11.1.0 Bumps [kcd-scripts](https://github.com/kentcdodds/kcd-scripts) from 10.0.0 to 11.1.0. - [Release notes](https://github.com/kentcdodds/kcd-scripts/releases) - [Changelog](https://github.com/kentcdodds/kcd-scripts/blob/main/CHANGELOG.md) - [Commits](kentcdodds/kcd-scripts@v10.0.0...v11.1.0) --- updated-dependencies: - dependency-name: kcd-scripts dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * test: Update tests to be compatible with Jest 27 Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 3bf9e5c commit 79b5559

36 files changed

+141
-286
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"docz-theme-default": "1.2.0",
6262
"docz-utils": "2.3.0",
6363
"eslint": "7.28.0",
64-
"kcd-scripts": "10.0.0",
64+
"kcd-scripts": "11.1.0",
6565
"prettier": "^2.2.1",
6666
"react": "17.0.2",
6767
"react-dom": "^17.0.1",

Diff for: src/__tests__/defaultRenderer.pure.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
import { ReactHooksRenderer } from '../types/react'
3+
4+
describe('default renderer', () => {
5+
beforeEach(() => {
6+
jest.resetModules()
7+
})
8+
9+
test('should resolve native renderer as default renderer', () => {
10+
const expectedRenderer = require('../native/pure') as ReactHooksRenderer
11+
const actualRenderer = require('../pure') as ReactHooksRenderer
12+
13+
expect(actualRenderer).toEqual(expectedRenderer)
14+
})
15+
16+
test('should resolve dom renderer as default renderer', () => {
17+
jest.doMock('react-test-renderer', () => {
18+
throw new Error('missing dependency')
19+
})
20+
21+
const expectedRenderer = require('../dom/pure') as ReactHooksRenderer
22+
const actualRenderer = require('../pure') as ReactHooksRenderer
23+
24+
expect(actualRenderer).toEqual(expectedRenderer)
25+
})
26+
27+
test('should throw error if a default renderer cannot be resolved', () => {
28+
jest.doMock('react-test-renderer', () => {
29+
throw new Error('missing dependency')
30+
})
31+
32+
jest.doMock('react-dom', () => {
33+
throw new Error('missing dependency')
34+
})
35+
36+
const expectedMessage =
37+
"Could not auto-detect a React renderer. Are you sure you've installed one of the following\n - react-dom\n - react-test-renderer\nIf you are using a bundler, please update your imports to use a specific renderer.\nFor instructions see: https://react-hooks-testing-library.com/installation#being-specific"
38+
39+
expect(() => require('../pure')).toThrowError(new Error(expectedMessage))
40+
})
41+
})

Diff for: src/__tests__/defaultRenderer.test.ts

+2-35
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,8 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
import { ReactHooksRenderer } from '../types/react'
1+
import * as actualRenderer from '..'
2+
import * as expectedRenderer from '../native'
33

44
describe('default renderer', () => {
5-
beforeEach(() => {
6-
jest.resetModules()
7-
})
8-
95
test('should resolve native renderer as default renderer', () => {
10-
const expectedRenderer = require('../native/pure') as ReactHooksRenderer
11-
const actualRenderer = require('..') as ReactHooksRenderer
12-
136
expect(actualRenderer).toEqual(expectedRenderer)
147
})
15-
16-
test('should resolve dom renderer as default renderer', () => {
17-
jest.doMock('react-test-renderer', () => {
18-
throw new Error('missing dependency')
19-
})
20-
21-
const expectedRenderer = require('../dom/pure') as ReactHooksRenderer
22-
const actualRenderer = require('..') as ReactHooksRenderer
23-
24-
expect(actualRenderer).toEqual(expectedRenderer)
25-
})
26-
27-
test('should throw error if a default renderer cannot be resolved', () => {
28-
jest.doMock('react-test-renderer', () => {
29-
throw new Error('missing dependency')
30-
})
31-
32-
jest.doMock('react-dom', () => {
33-
throw new Error('missing dependency')
34-
})
35-
36-
const expectedMessage =
37-
"Could not auto-detect a React renderer. Are you sure you've installed one of the following\n - react-dom\n - react-test-renderer\nIf you are using a bundler, please update your imports to use a specific renderer.\nFor instructions see: https://react-hooks-testing-library.com/installation#being-specific"
38-
39-
expect(() => require('..')).toThrowError(new Error(expectedMessage))
40-
})
418
})

Diff for: src/dom/__tests__/autoCleanup.disabled.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ import { ReactHooksRenderer } from '../../types/react'
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (disabled) tests', () => {
88
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
13-
renderHook = (require('..') as ReactHooksRenderer).renderHook
14-
})
9+
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
10+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
1511

1612
test('first', () => {
1713
const hookWithCleanup = () => {

Diff for: src/dom/__tests__/autoCleanup.noAfterEach.test.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import { ReactHooksRenderer } from '../../types/react'
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (no afterEach) tests', () => {
88
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
13-
afterEach = false
14-
renderHook = (require('..') as ReactHooksRenderer).renderHook
15-
})
9+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
10+
afterEach = false
11+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
1612

1713
test('first', () => {
1814
const hookWithCleanup = () => {

Diff for: src/dom/__tests__/autoCleanup.noProcessEnv.test.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ import { ReactHooksRenderer } from '../../types/react'
44

55
// This verifies that if process.env is unavailable
66
// then we still auto-wire up the afterEach for folks
7-
describe('skip auto cleanup (no process.env) tests', () => {
8-
const originalEnv = process.env
7+
describe('auto cleanup (no process.env) tests', () => {
98
let cleanupCalled = false
10-
let renderHook: ReactHooksRenderer['renderHook']
11-
12-
beforeAll(() => {
13-
process.env = {
14-
...process.env,
15-
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined {
16-
throw new Error('expected')
17-
}
9+
process.env = {
10+
...process.env,
11+
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined {
12+
throw new Error('expected')
1813
}
19-
renderHook = (require('..') as ReactHooksRenderer).renderHook
20-
})
21-
22-
afterAll(() => {
23-
process.env = originalEnv
24-
})
14+
}
15+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
2516

2617
test('first', () => {
2718
const hookWithCleanup = () => {

Diff for: src/dom/__tests__/autoCleanup.pure.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { useEffect } from 'react'
2-
3-
import { ReactHooksRenderer } from '../../types/react'
2+
import { renderHook } from '../pure'
43

54
// This verifies that if pure imports are used
65
// then we DON'T auto-wire up the afterEach for folks
76
describe('skip auto cleanup (pure) tests', () => {
87
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
renderHook = (require('../pure') as ReactHooksRenderer).renderHook
13-
})
148

159
test('first', () => {
1610
const hookWithCleanup = () => {

Diff for: src/dom/__tests__/errorHook.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect } from 'react'
2-
import { renderHook, act } from '..'
2+
import { renderHook } from '..'
33

44
describe('error hook tests', () => {
55
function useError(throwError?: boolean) {

Diff for: src/dom/__tests__/errorSuppression.disabled.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// then we DON'T auto-wire up the afterEach for folks
33
describe('error output suppression (disabled) tests', () => {
44
const originalConsoleError = console.error
5-
6-
beforeAll(() => {
7-
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true'
8-
require('..')
9-
})
5+
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true'
6+
require('..')
107

118
test('should not patch console.error', () => {
129
expect(console.error).toBe(originalConsoleError)

Diff for: src/dom/__tests__/errorSuppression.noAfterEach.test.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// then we DON'T auto-wire up the afterEach for folks
33
describe('error output suppression (noAfterEach) tests', () => {
44
const originalConsoleError = console.error
5-
6-
beforeAll(() => {
7-
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
8-
afterEach = false
9-
require('..')
10-
})
5+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
6+
afterEach = false
7+
require('..')
118

129
test('should not patch console.error', () => {
1310
expect(console.error).toBe(originalConsoleError)

Diff for: src/dom/__tests__/errorSuppression.noBeforeEach.test.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// then we DON'T auto-wire up the afterEach for folks
33
describe('error output suppression (noBeforeEach) tests', () => {
44
const originalConsoleError = console.error
5-
6-
beforeAll(() => {
7-
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type
8-
beforeEach = false
9-
require('..')
10-
})
5+
// @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type
6+
beforeEach = false
7+
require('..')
118

129
test('should not patch console.error', () => {
1310
expect(console.error).toBe(originalConsoleError)

Diff for: src/dom/__tests__/errorSuppression.noProcessEnv.test.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
// This verifies that if process.env is unavailable
22
// then we still auto-wire up the afterEach for folks
33
describe('error output suppression (no process.env) tests', () => {
4-
const originalEnv = process.env
54
const originalConsoleError = console.error
6-
7-
beforeAll(() => {
8-
process.env = {
9-
...process.env,
10-
get RHTL_DISABLE_ERROR_FILTERING(): string | undefined {
11-
throw new Error('expected')
12-
}
5+
process.env = {
6+
...process.env,
7+
get RHTL_DISABLE_ERROR_FILTERING(): string | undefined {
8+
throw new Error('expected')
139
}
14-
require('..')
15-
})
16-
17-
afterAll(() => {
18-
process.env = originalEnv
19-
})
10+
}
11+
require('..')
2012

2113
test('should not patch console.error', () => {
2214
expect(console.error).not.toBe(originalConsoleError)

Diff for: src/dom/__tests__/errorSuppression.pure.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import { ReactHooksRenderer } from '../../types/react'
1+
import { suppressErrorOutput } from '../pure'
22

33
// This verifies that if pure imports are used
44
// then we DON'T auto-wire up the afterEach for folks
55
describe('error output suppression (pure) tests', () => {
66
const originalConsoleError = console.error
77

8-
let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput']
9-
10-
beforeAll(() => {
11-
suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput
12-
})
13-
148
test('should not patch console.error', () => {
159
expect(console.error).toBe(originalConsoleError)
1610
})

Diff for: src/dom/__tests__/errorSuppression.test.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { useEffect } from 'react'
2-
3-
import { ReactHooksRenderer } from '../../types/react'
2+
import { renderHook, act, suppressErrorOutput } from '..'
43

54
describe('error output suppression tests', () => {
5+
const consoleError = console.error
6+
67
test('should not suppress relevant errors', () => {
7-
const consoleError = console.error
88
console.error = jest.fn()
9-
10-
const { suppressErrorOutput } = require('..') as ReactHooksRenderer
11-
129
try {
1310
const restoreConsole = suppressErrorOutput()
1411

@@ -28,8 +25,6 @@ describe('error output suppression tests', () => {
2825
})
2926

3027
test('should allow console.error to be mocked', async () => {
31-
const { renderHook, act } = require('..') as ReactHooksRenderer
32-
const consoleError = console.error
3328
console.error = jest.fn()
3429

3530
try {

Diff for: src/native/__tests__/autoCleanup.disabled.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ import { ReactHooksRenderer } from '../../types/react'
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (disabled) tests', () => {
88
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
13-
renderHook = (require('..') as ReactHooksRenderer).renderHook
14-
})
9+
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true'
10+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
1511

1612
test('first', () => {
1713
const hookWithCleanup = () => {

Diff for: src/native/__tests__/autoCleanup.noAfterEach.test.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import { ReactHooksRenderer } from '../../types/react'
66
// then we DON'T auto-wire up the afterEach for folks
77
describe('skip auto cleanup (no afterEach) tests', () => {
88
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
13-
afterEach = false
14-
renderHook = (require('..') as ReactHooksRenderer).renderHook
15-
})
9+
// @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type
10+
afterEach = false
11+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
1612

1713
test('first', () => {
1814
const hookWithCleanup = () => {

Diff for: src/native/__tests__/autoCleanup.noProcessEnv.test.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ import { ReactHooksRenderer } from '../../types/react'
44

55
// This verifies that if process.env is unavailable
66
// then we still auto-wire up the afterEach for folks
7-
describe('skip auto cleanup (no process.env) tests', () => {
8-
const originalEnv = process.env
7+
describe('auto cleanup (no process.env) tests', () => {
98
let cleanupCalled = false
10-
let renderHook: ReactHooksRenderer['renderHook']
11-
12-
beforeAll(() => {
13-
process.env = {
14-
...process.env,
15-
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined {
16-
throw new Error('expected')
17-
}
9+
process.env = {
10+
...process.env,
11+
get RHTL_SKIP_AUTO_CLEANUP(): string | undefined {
12+
throw new Error('expected')
1813
}
19-
renderHook = (require('..') as ReactHooksRenderer).renderHook
20-
})
21-
22-
afterAll(() => {
23-
process.env = originalEnv
24-
})
14+
}
15+
const renderHook = (require('..') as ReactHooksRenderer).renderHook
2516

2617
test('first', () => {
2718
const hookWithCleanup = () => {

Diff for: src/native/__tests__/autoCleanup.pure.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { useEffect } from 'react'
2-
3-
import { ReactHooksRenderer } from '../../types/react'
2+
import { renderHook } from '../pure'
43

54
// This verifies that if pure imports are used
65
// then we DON'T auto-wire up the afterEach for folks
76
describe('skip auto cleanup (pure) tests', () => {
87
let cleanupCalled = false
9-
let renderHook: ReactHooksRenderer['renderHook']
10-
11-
beforeAll(() => {
12-
renderHook = (require('../pure') as ReactHooksRenderer).renderHook
13-
})
148

159
test('first', () => {
1610
const hookWithCleanup = () => {

0 commit comments

Comments
 (0)