Skip to content

Commit 9c013e1

Browse files
authored
Update Jest and test setup for @testing-library/react (#2288)
* test: add jest-watch-typeahead plugin * refactor: remove imports to @testing-library/jest-dom * test: update jest to v29 * chore: remove babel-polyfill calls * refactor: remove manual cleanup calls from @testing-library/react * Apply suggestions from code review * Update src/__tests__/ActionList.test.tsx
1 parent 579b4dd commit 9c013e1

File tree

99 files changed

+36924
-30584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+36924
-30584
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
testMatch: ['<rootDir>/(src|codemods)/**/*.test.[jt]s?(x)', '!**/*.types.test.[jt]s?(x)'],
1212
transformIgnorePatterns: [
1313
'node_modules/(?!@github/combobox-nav|@koddsson/textarea-caret|@github/markdown-toolbar-element)'
14-
]
14+
],
15+
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname']
1516
}

package-lock.json

Lines changed: 31851 additions & 25356 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"@storybook/addon-links": "^6.5.9",
126126
"@storybook/jest": "0.0.10",
127127
"@storybook/react": "6.5.9",
128-
"@storybook/test-runner": "0.4.0",
128+
"@storybook/test-runner": "0.7.0",
129129
"@storybook/testing-library": "0.0.13",
130130
"@testing-library/dom": "8.13.0",
131131
"@testing-library/jest-dom": "5.16.4",
@@ -170,10 +170,12 @@
170170
"eslint-plugin-react-hooks": "4.2.0",
171171
"front-matter": "4.0.2",
172172
"husky": "7.0.4",
173-
"jest": "27.4.5",
173+
"jest": "29.0.1",
174174
"jest-axe": "5.0.1",
175+
"jest-environment-jsdom": "29.0.1",
175176
"jest-matchmedia-mock": "1.1.0",
176177
"jest-styled-components": "6.3.4",
178+
"jest-watch-typeahead": "2.1.1",
177179
"jscodeshift": "0.13.0",
178180
"lint-staged": "12.1.2",
179181
"lodash.isempty": "4.4.0",

src/__tests__/ActionList.test.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import {cleanup, render as HTMLRender, waitFor, fireEvent} from '@testing-library/react'
2-
import 'babel-polyfill'
1+
import {render as HTMLRender, waitFor, fireEvent} from '@testing-library/react'
32
import {axe, toHaveNoViolations} from 'jest-axe'
43
import React from 'react'
54
import theme from '../theme'
65
import {ActionList} from '../ActionList'
76
import {behavesAsComponent, checkExports, checkStoriesForAxeViolations} from '../utils/testing'
87
import {BaseStyles, ThemeProvider, SSRProvider} from '..'
9-
import '@testing-library/jest-dom'
108
expect.extend(toHaveNoViolations)
119

1210
function SimpleActionList(): JSX.Element {
@@ -69,7 +67,6 @@ describe('ActionList', () => {
6967
const {container} = HTMLRender(<SimpleActionList />)
7068
const results = await axe(container)
7169
expect(results).toHaveNoViolations()
72-
cleanup()
7370
})
7471

7572
it('should fire onSelect on click and keypress', async () => {
@@ -95,8 +92,6 @@ describe('ActionList', () => {
9592

9693
expect(options[0]).toHaveAttribute('aria-selected', 'false')
9794
expect(options[1]).toHaveAttribute('aria-selected', 'true')
98-
99-
cleanup()
10095
})
10196

10297
it('should skip onSelect on disabled items', async () => {
@@ -115,8 +110,6 @@ describe('ActionList', () => {
115110

116111
expect(options[0]).toHaveAttribute('aria-selected', 'true')
117112
expect(options[2]).toHaveAttribute('aria-selected', 'false')
118-
119-
cleanup()
120113
})
121114

122115
it('should throw when selected is provided without a selectionVariant on parent', async () => {
@@ -133,7 +126,6 @@ describe('ActionList', () => {
133126
)
134127
}).toThrow('For Item to be selected, ActionList or ActionList.Group needs to have a selectionVariant defined')
135128

136-
cleanup()
137129
mockError.mockRestore()
138130
})
139131

@@ -149,8 +141,6 @@ describe('ActionList', () => {
149141
fireEvent.click(option)
150142
fireEvent.keyPress(option, {key: 'Enter', charCode: 13})
151143
expect(option).toBeInTheDocument()
152-
153-
cleanup()
154144
})
155145

156146
checkStoriesForAxeViolations('ActionList/fixtures')

src/__tests__/ActionMenu.test.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import {cleanup, render as HTMLRender, waitFor} from '@testing-library/react'
1+
import {render as HTMLRender, waitFor} from '@testing-library/react'
22
import userEvent from '@testing-library/user-event'
3-
import 'babel-polyfill'
43
import {axe, toHaveNoViolations} from 'jest-axe'
54
import React from 'react'
65
import theme from '../theme'
76
import {ActionMenu, ActionList, BaseStyles, ThemeProvider, SSRProvider} from '..'
87
import {behavesAsComponent, checkExports, checkStoriesForAxeViolations} from '../utils/testing'
98
import {SingleSelection, MixedSelection} from '../stories/ActionMenu/examples.stories'
10-
import '@testing-library/jest-dom'
119
expect.extend(toHaveNoViolations)
1210

1311
function Example(): JSX.Element {
@@ -55,7 +53,6 @@ describe('ActionMenu', () => {
5553
await user.click(button)
5654

5755
expect(component.getByRole('menu')).toBeInTheDocument()
58-
cleanup()
5956
})
6057

6158
it('should open Menu on MenuButton keypress', async () => {
@@ -67,7 +64,6 @@ describe('ActionMenu', () => {
6764
await user.keyboard('{Enter}')
6865

6966
expect(component.getByRole('menu')).toBeInTheDocument()
70-
cleanup()
7167
})
7268

7369
it('should close Menu on selecting an action with click', async () => {
@@ -81,7 +77,6 @@ describe('ActionMenu', () => {
8177
await user.click(menuItems[0])
8278

8379
expect(component.queryByRole('menu')).toBeNull()
84-
cleanup()
8580
})
8681

8782
it('should close Menu on selecting an action with Enter', async () => {
@@ -96,7 +91,6 @@ describe('ActionMenu', () => {
9691
await user.keyboard('{Enter}')
9792

9893
expect(component.queryByRole('menu')).toBeNull()
99-
cleanup()
10094
})
10195

10296
it('should not close Menu if event is prevented', async () => {
@@ -111,8 +105,6 @@ describe('ActionMenu', () => {
111105

112106
// menu should still be open
113107
expect(component.getByRole('menu')).toBeInTheDocument()
114-
115-
cleanup()
116108
})
117109

118110
it('should be able to select an Item with selectionVariant', async () => {
@@ -133,7 +125,6 @@ describe('ActionMenu', () => {
133125
// open menu again and check if the first option is checked
134126
await user.click(button)
135127
expect(component.getAllByRole('menuitemradio')[0]).toHaveAttribute('aria-checked', 'true')
136-
cleanup()
137128
})
138129

139130
it('should assign the right roles with groups & mixed selectionVariant', async () => {
@@ -149,8 +140,6 @@ describe('ActionMenu', () => {
149140

150141
expect(component.getByLabelText('Status')).toHaveAttribute('role', 'menuitemradio')
151142
expect(component.getByLabelText('Clear Group by')).toHaveAttribute('role', 'menuitem')
152-
153-
cleanup()
154143
})
155144

156145
it('should keep focus on Button when menu is opened with click', async () => {
@@ -164,8 +153,6 @@ describe('ActionMenu', () => {
164153
await user.click(button)
165154
expect(component.queryByRole('menu')).toBeInTheDocument()
166155
expect(document.activeElement).toEqual(button)
167-
168-
cleanup()
169156
})
170157

171158
it('should select first element when ArrowDown is pressed after opening Menu with click', async () => {
@@ -181,7 +168,6 @@ describe('ActionMenu', () => {
181168
await user.keyboard('{ArrowDown}')
182169

183170
expect(component.getAllByRole('menuitem')[0]).toEqual(document.activeElement)
184-
cleanup()
185171
})
186172

187173
it('should select last element when ArrowUp is pressed after opening Menu with click', async () => {
@@ -199,7 +185,6 @@ describe('ActionMenu', () => {
199185
await user.keyboard('{ArrowUp}')
200186

201187
expect(component.getAllByRole('menuitem').pop()).toEqual(document.activeElement)
202-
cleanup()
203188
})
204189

205190
it('should close the menu if Tab is pressed and move to next element', async () => {
@@ -225,15 +210,12 @@ describe('ActionMenu', () => {
225210
expect(document.activeElement).toEqual(component.getByPlaceholderText('next focusable element'))
226211
expect(component.queryByRole('menu')).not.toBeInTheDocument()
227212
})
228-
229-
cleanup()
230213
})
231214

232215
it('should have no axe violations', async () => {
233216
const {container} = HTMLRender(<Example />)
234217
const results = await axe(container)
235218
expect(results).toHaveNoViolations()
236-
cleanup()
237219
})
238220

239221
checkStoriesForAxeViolations('ActionMenu/fixtures')

src/__tests__/AnchoredOverlay.test.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, {useCallback, useState} from 'react'
22
import {AnchoredOverlay} from '../AnchoredOverlay'
33
import {behavesAsComponent, checkExports} from '../utils/testing'
4-
import {render as HTMLRender, cleanup, fireEvent} from '@testing-library/react'
4+
import {render as HTMLRender, fireEvent} from '@testing-library/react'
55
import {axe, toHaveNoViolations} from 'jest-axe'
6-
import 'babel-polyfill'
76
import {SSRProvider} from '../index'
87
import {Button} from '../deprecated'
98
import theme from '../theme'
@@ -71,14 +70,12 @@ describe('AnchoredOverlay', () => {
7170
const {container} = HTMLRender(<AnchoredOverlayTestComponent initiallyOpen={true}></AnchoredOverlayTestComponent>)
7271
const results = await axe(container)
7372
expect(results).toHaveNoViolations()
74-
cleanup()
7573
})
7674

7775
it('should have no axe violations when closed', async () => {
7876
const {container} = HTMLRender(<AnchoredOverlayTestComponent></AnchoredOverlayTestComponent>)
7977
const results = await axe(container)
8078
expect(results).toHaveNoViolations()
81-
cleanup()
8279
})
8380

8481
it('should call onOpen when the anchor is clicked', () => {

src/__tests__/Autocomplete.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react'
22
import {render} from '../utils/testing'
33
import {render as HTMLRender, fireEvent, waitFor} from '@testing-library/react'
44
import {toHaveNoViolations} from 'jest-axe'
5-
import 'babel-polyfill'
65
import Autocomplete, {AutocompleteInputProps} from '../Autocomplete'
76
import {SSRProvider} from '../index'
87
import theme from '../theme'

src/__tests__/Avatar.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React from 'react'
22
import {Avatar} from '..'
33
import theme from '../theme'
44
import {px, render, behavesAsComponent, checkExports} from '../utils/testing'
5-
import {render as HTMLRender, cleanup} from '@testing-library/react'
5+
import {render as HTMLRender} from '@testing-library/react'
66
import {axe, toHaveNoViolations} from 'jest-axe'
7-
import 'babel-polyfill'
7+
88
expect.extend(toHaveNoViolations)
99

1010
describe('Avatar', () => {
@@ -18,7 +18,6 @@ describe('Avatar', () => {
1818
const {container} = HTMLRender(<Avatar src="primer.png" />)
1919
const results = await axe(container)
2020
expect(results).toHaveNoViolations()
21-
cleanup()
2221
})
2322

2423
it('renders small by default', () => {

src/__tests__/AvatarStack.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
22
import {AvatarStack} from '..'
33
import {render, behavesAsComponent, checkExports} from '../utils/testing'
4-
import {render as HTMLRender, cleanup} from '@testing-library/react'
4+
import {render as HTMLRender} from '@testing-library/react'
55
import {axe, toHaveNoViolations} from 'jest-axe'
6-
import 'babel-polyfill'
6+
77
expect.extend(toHaveNoViolations)
88

99
const avatarComp = (
@@ -39,7 +39,6 @@ describe('Avatar', () => {
3939
const {container} = HTMLRender(avatarComp)
4040
const results = await axe(container)
4141
expect(results).toHaveNoViolations()
42-
cleanup()
4342
})
4443

4544
it('respects alignRight props', () => {

src/__tests__/BorderBox.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React from 'react'
22
import theme from '../theme'
33
import {BorderBox} from '../deprecated'
44
import {render, behavesAsComponent, checkExports} from '../utils/testing'
5-
import {render as HTMLRender, cleanup} from '@testing-library/react'
5+
import {render as HTMLRender} from '@testing-library/react'
66
import {axe, toHaveNoViolations} from 'jest-axe'
7-
import 'babel-polyfill'
7+
88
expect.extend(toHaveNoViolations)
99

1010
describe('BorderBox', () => {
@@ -18,7 +18,6 @@ describe('BorderBox', () => {
1818
const {container} = HTMLRender(<BorderBox />)
1919
const results = await axe(container)
2020
expect(results).toHaveNoViolations()
21-
cleanup()
2221
})
2322

2423
it('renders borders', () => {

0 commit comments

Comments
 (0)