Testing materials
Last updated ago - September 3, 2023Testing introduction
Why testing is important
- Catch bugs before they appear in production
- Testing forces you to write cleaner code, because clean code is easier to test
- Increase confidence in application (making sure your changes are not breaking code in other places)
- Testing can serve as documentation
- Speeding up QA time
- In a very large codebase you cannot predict or test manually if your change in code will break something, automated testing helps with checking if your changes are valid
Structure of a test, the AAA pattern
- Arrange - set up the test data, test conditions, and test environment
- Act - run logic that should be tested (eg. execute function), click a button
- Assert - compare the expected result with the actual result
Types of tests
- Unit tests - testing an isolated function
- Integration tests - testing a component that uses other components, testing things working together
- End to end tests - testing user interaction with UI
What to test and not to test?
Don’t test
- External libraries
- External API’s
- Your backend code when testing frontend code
- Above can be summarized with: don’t test code you have no possibility of fixing
You should test
- Code that you have written in current project
- Only one thing or feature per test example: input validation
How to pick elements to test
Test element selectors from best to worst, you want to test your application in a way the user is interacting with it (this is approach presented by React Testing Library authors).
- Accessible by Everyone
- getByRole
- getByLabelText
- getByPlaceholderText
- getByText
- Accessible by Screen readers
- getByAltText
- getByTitle
- Last resort methods (if it’s not possible to use other means) = getByTestId
On the other hand, authors of Cypress library encourage the use of as many test id selectors as possible.
Free testing courses
- Net Ninja - React Testing Library tutorial series
- Jack Herrington - Testing with Jest and Wallaby
- Jack Herrington - TypeScript/React Testing: Components, Hooks, Custom Hooks, Redux and Zustand
Free Polish testing courses
Frontlive - Jest & React Testing Library: seria kursów na blogu
Paid testing courses
More about testing
Jest JavaScript Testing Framework
Vitest an alternative to Jest
Why choose Vitest
- built in typescript support
- chai and jest compatible assertions
- works out of the box with super fast build tool: Vite
Vitest materials
thisdot.co - Testing with Vitest by Ignacio Falk
Protip: You can use Vitest like Jest, just some minor adjustments are needed for it to work. You can watch Jest tutorials to learn Vitest
Import modules for testing as globals in config
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
export default defineConfig({
plugins: [svelte({ hot: !process.env.VITEST })],
test: {
globals: true,
environment: 'jsdom',
},
})
End to end testing
Testing principles
What to test? Why test? Which type of test to write?
- Kent C. Dodds - The Testing Trophy and Testing Classifications
- Kent C. Dodds - Static vs Unit vs Integration vs E2E Testing for Frontend Apps
- Fireship - Test-Driven Development // Fun TDD Introduction with JavaScript
- Jack Herrington - Test Driven Development: The best way to code that I almost never use
- Kent C. Dodds - AHA Testing (how to test wisely)