Testing
Our testing strategy and how we test our code.
We use well-known testing libraries like Vitest and React Testing Library to ensure our frontend applications are reliable and stable. Below, you’ll find our testing practices and tips for writing effective tests.
Running Tests
To run the entire test suite, use the following command. This executes all tests
located in the __tests__ directory across all packages and applications:
pnpm testFor testing a specific package or application, specify it with the --filter
option:
pnpm test --filter <package-name>Test Organization
We organize our tests as follows to keep our codebase clean:
- Separate Test Directory: Tests are placed in a
__tests__directory, separate from the source code(src), maintaining a clear distinction between production and test code. - Centralized Test Configurations: All test configurations are centralized in
@akinon/vitest-configpackage.
Test Coverage
We aim for robust test coverage using Istanbul, a code coverage tool, with a benchmark of at least 80% coverage. This ensures that critical areas of our codebase are well-tested.
To get a test coverage report for a package, use the following command.
pnpm test:coverage --filter <package-name>A coverage report is created in the coverage directory within the package
directory.
Tips for Writing Effective Tests
Here are some tips for making the most of Vitest when writing tests:
- Isolate Tests: Keep tests independent to avoid interactions and simplify troubleshooting.
- Descriptive Test Names: Choose clear and meaningful names for tests to help understand their purpose and diagnose failures easily.
- Arrange, Act, Assert (AAA): Use this pattern to structure your tests—set up the initial state (Arrange), perform actions (Act), and check the outcomes (Assert).
- Use Mocks and Stubs: Incorporate mocks and stubs to isolate components from external dependencies during testing.
- Parameterized Tests: Apply parameterized tests to run the same test with different inputs.
- Async/Await: Utilize async/await for handling asynchronous operations in your tests to keep assertions clean and straightforward.
Continuous Integration
We take testing seriously by implementing a pre-commit Git hook and a Bitbucket Pipeline to ensure that tests pass before code is merged. This automated process helps maintain a high standard of code quality and reliability throughout our development workflow.