- Unit Testing
- If all building blocks works, the overall app works
- Integration Testing
- Even if all units work standalone, the combination could fail
- End-to-End (E2E) Testing
- Real ursers use your app and its features, not individual units
All of those type of tests should be combined when is doing tests.
- E2E Tests
- Integration Tests
- Unit Tests
A framework / philosophy for writing tests
- Write a failing test first
- Implement the code to make the test succeed
- Refactor
- Getting started
- Project Setup & Required Tools
- Testing Fundamentals
- Writing Good Tests
- Advanced Testing Concepts
- Mocks & Spies
- Diving Deeper into Mocks & Spies
- Tests & The DOM
- Adjusting the Development Environment
- Required Tools
- An Example Setup
- Alternatives
- Test Runner
- Runs your tests (i.e, the testing code)
- Automatically detects testing code
- Displays results
- e.g., Jest, Karma
Expect results
- Assertion Library
- Used to define expected outcomes
- Checks whether expectations are met
- Supports all kinds of expectations and modes (sync / async)
- e.g., Jest, Chai
ECMAScript on Jest have a experimental support an alternative is Vitest
- Understanding Unit Testing Fundamentals
- Creating Unit Tests
- AAA - Arrange, Act, Assert
- What To Test & Organizing Tests
How it works? Write the test function that the first argument is the description of what the test will do and the second one is the test itself.
Example:
import {it} from 'vitest'
it('the description', () => {})
- Arrange: Define the testing environment and values
- Act: Run the actual code / function that should be tested
- Assert: Evaluate the produced value / result and compare it to the expected value / result
Keep Your Tests Simple!
Writing good tests is an iterative process
Note Technically, expect() does not return true or false. Instead, it throws an error, if the expection is not met. The test runner treats thrown errors as failed tests and tests that do not throw as passed.
It's ok to write more than one test with the same result, but it can be arranged in the same test to avoid repetition.
Important: ONLY TEST YOUR CODE
Follow Arrange-Act-Assert
- Only test one thing
- Focus on essence of a test when arranging
- Keep your number of assertions ("expects") low
Splitting functions for easier testing and better code.
Integration Tests