Skip to content

Commit 7de27d2

Browse files
committed
feat(test): Switch to @testing-library/react (#73)
Want to switch over from `enzyme` to `@testing-library/react` for testing React components. - Removed `enzyme` (and related packages) - Added [`@testing-library/react`](https://testing-library.com/docs/react-testing-library/intro) (and related packages) - Upgraded `jest`, `@types/jest` & `babel-jest` - Including [`@testing-library/jest-dom/extend-expect`](https://github.com/testing-library/jest-dom) in jest spec setup to get additional `expect()` matchers BREAKING CHANGE: `enzyme` is no longer supported for React component testing
1 parent b21fd36 commit 7de27d2

File tree

8 files changed

+3074
-1075
lines changed

8 files changed

+3074
-1075
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ The CLI has been tested to work in Node 8+.
107107
- [TypeScript](https://www.typescriptlang.org/) (type-checking)
108108
- [Babel](https://babeljs.io/) (transpiling)
109109
- [Jest](https://jestjs.io/en) (testing & code coverage)
110-
- [Enzyme](https://github.com/airbnb/enzyme) (testing React components)
110+
- [React testing library](https://testing-library.com/docs/react-testing-library/intro) (testing React components)
111111
- [ESLint](http://eslint.org/) (linting)
112112
- [Yargs](https://github.com/yargs/yargs) (command line argument parsing)
113113

integration-tests/src/react/__tests__/Button.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react'
2-
import { mount } from 'enzyme'
2+
import { render, fireEvent } from '@testing-library/react'
33

44
import Button from '../Button'
55

66
test('calls onClick prop when <button> is clicked', () => {
77
const onClick = jest.fn()
8-
const button = mount(<Button onClick={onClick}>Go!</Button>)
8+
const { getByText } = render(<Button onClick={onClick}>Go!</Button>)
99

10-
button.simulate('click')
10+
fireEvent.click(getByText('Go!'))
1111

1212
expect(onClick).toHaveBeenCalledWith()
1313
expect(onClick).toHaveBeenCalledTimes(1)
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
import React from 'react'
2-
import { mount } from 'enzyme'
3-
import { act } from 'react-dom/test-utils'
2+
import { render, fireEvent } from '@testing-library/react'
43

54
import Counter from '../Counter'
6-
import Button from '../Button'
75

86
test('starts off at 0 by default', () => {
9-
const counter = mount(<Counter />)
7+
const { queryByText } = render(<Counter />)
108

11-
expect(counter).toIncludeText('You clicked 0 times')
9+
expect(queryByText('You clicked 0 times')).toBeInTheDocument()
1210
})
1311

1412
test('can have a different initial value', () => {
15-
const counter = mount(<Counter initialCount={14} />)
13+
const { queryByText } = render(<Counter initialCount={14} />)
1614

17-
expect(counter).toIncludeText('You clicked 14 times')
15+
expect(queryByText('You clicked 14 times')).toBeInTheDocument()
1816
})
1917

2018
test('updates count when Button is clicked', () => {
21-
const counter = mount(<Counter initialCount={5} />)
22-
const button = counter.find(Button)
23-
const buttonOnClick = button.prop('onClick')
24-
25-
if (buttonOnClick) {
26-
// simulate clicking the button by trigger onClick prop of <Button />
27-
act(() => {
28-
buttonOnClick()
29-
})
30-
}
31-
32-
expect(counter).toIncludeText('You clicked 6 times')
19+
const { queryByText, getByText } = render(<Counter initialCount={5} />)
20+
21+
fireEvent.click(getByText('Click me'))
22+
23+
expect(queryByText('You clicked 6 times')).toBeInTheDocument()
3324
})

0 commit comments

Comments
 (0)