Created
July 2, 2022 11:48
-
-
Save shawn-sandy/1e53faf8cfbbbbd31ca4edc6d2861f8e to your computer and use it in GitHub Desktop.
Vitest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import { describe, expect, test, it } from 'vitest'; | |
| import { render, screen } from '@testing-library/react'; | |
| import userEvent from '@testing-library/user-event'; | |
| import App from '../App' | |
| describe('App renders correctly', () => { | |
| test('renders without crashing', () => { | |
| render(<App />); | |
| expect(screen).toMatchSnapshot(); | |
| }); | |
| it('renders the correct text', () => { | |
| const { getByText } = render(<App />); | |
| expect(getByText('Hello Vite + React!')).toBeInTheDocument(); | |
| }); | |
| it('renders the button', () => { | |
| const { getByText } = render(<App />); | |
| // screen.debug() | |
| const button = screen.getByRole('button'); | |
| expect(button).toBeInTheDocument(); | |
| expect(button).toHaveProperty('type', 'button'); | |
| expect(getByText(/count is: 0/i)).toBeInTheDocument(); | |
| expect(button).toMatchSnapshot() | |
| }); | |
| it('renders the links', () => { | |
| const { getByText } = render(<App />); | |
| const link = screen.getAllByRole('link'); | |
| expect(link).toHaveLength(2); | |
| }) | |
| it('should increment count on click', async () => { | |
| render(<App />) | |
| userEvent.click(screen.getByRole('button')) | |
| expect(await screen.findByText(/count is: 1/i)).toBeInTheDocument() | |
| }) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment