Skip to content

Commit 1d6604c

Browse files
authored
feat(tests): better test tools and examples (#3)
* feat(tests): add jest-dom/vitest for extended dom matchers support * feat(tests): update selectors * feat(tests): code review Co-authored-by: Matthieu Izoulet [email protected]
1 parent 44cad96 commit 1d6604c

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
},
3131
"devDependencies": {
3232
"@testing-library/dom": "^10.4.0",
33-
"@testing-library/jest-dom": "^6.4.8",
33+
"@testing-library/jest-dom": "^6.5.0",
3434
"@testing-library/react": "^16.0.0",
35+
"@testing-library/user-event": "^14.5.2",
3536
"@types/react": "^18.3.3",
3637
"@types/react-dom": "^18.3.0",
3738
"@typescript-eslint/eslint-plugin": "^7.18.0",

setup-tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom/vitest'

src/app.test.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
import { render, screen, cleanup, fireEvent } from '@testing-library/react'
1+
import { render, screen, cleanup } from '@testing-library/react'
22
import { expect, test, afterEach } from 'vitest'
3+
import userEvent from '@testing-library/user-event'
34

45
import App from './app'
56

67
// Vitest API Reference: https://vitest.dev/api/
7-
// Testing Library Events: https://testing-library.com/docs/dom-testing-library/api-events
8+
// Testing Library User Events: https://testing-library.com/docs/user-event/intro/
89
// Testing Library Queries: https://testing-library.com/docs/queries/about
910

1011
afterEach(cleanup)
1112

1213
test('renders app component', () => {
1314
render(<App />)
1415

15-
expect(screen.getByTestId('title').textContent).toEqual('Hello there 👋')
16+
expect(screen.getByRole('heading')).toBeInTheDocument()
17+
expect(screen.getByRole('heading').textContent).toEqual('Hello there 👋')
1618
})
1719

18-
test('increments and decrements counter', () => {
20+
test('increments and decrements counter', async () => {
21+
const user = userEvent.setup()
1922
render(<App />)
2023

21-
expect(screen.getByTestId('count').textContent).toEqual('0')
24+
expect(screen.getByRole('status')).toBeInTheDocument()
25+
expect(screen.getByRole('status').textContent).toEqual('0')
2226

23-
fireEvent.click(screen.getByTestId('inc'))
24-
expect(screen.getByTestId('count').textContent).toEqual('1')
27+
expect(screen.getByRole('button', { name: '+' })).toBeEnabled()
28+
await user.click(screen.getByRole('button', { name: '+' }))
29+
expect(screen.getByRole('status').getAttribute('disabled'))
2530

26-
fireEvent.click(screen.getByTestId('dec'))
27-
expect(screen.getByTestId('count').textContent).toEqual('0')
31+
expect(screen.getByRole('button', { name: '-' })).toBeEnabled()
32+
await user.click(screen.getByRole('button', { name: '-' }))
33+
expect(screen.getByRole('status').textContent).toEqual('0')
2834
})

src/app.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ function App() {
2424

2525
return (
2626
<Container>
27-
<h2 data-testid="title">Hello there 👋</h2>
28-
<h2 data-testid="count">{count}</h2>
27+
<h2 role="heading">Hello there 👋</h2>
28+
<h2 role="status">{count}</h2>
2929
<Actions>
30-
<button data-testid="inc" onClick={() => setCount((prev) => prev + 1)}>
30+
<button type="button" onClick={() => setCount((prev) => prev + 1)}>
3131
+
3232
</button>
33-
<button data-testid="dec" onClick={() => setCount((prev) => prev - 1)}>
33+
<button type="button" onClick={() => setCount((prev) => prev - 1)}>
3434
-
3535
</button>
3636
</Actions>

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"target": "ES2020",
55
"useDefineForClassFields": true,
66
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7-
"types": ["vitest/globals"],
7+
"types": ["vitest/globals", "@testing-library/jest-dom/vitest"],
88
"module": "ESNext",
99
"skipLibCheck": true,
1010

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export default defineConfig({
66
plugins: [react()],
77
test: {
88
environment: 'jsdom',
9+
setupFiles: ['./setup-tests.ts'],
910
}
1011
})

0 commit comments

Comments
 (0)