Testing in Better Auth
Overview
We use Vitest as our testing framework of choice. Currently, we focus on unit tests, with plans to expand to integration testing in the future. This guide will help you understand our testing philosophy and how to write effective tests.
We expect you to have knowledge of Vitest before you start writing tests. If you're new to Vitest, we recommend you read the Vitest documentation first.
Testing Philosophy
Our approach to testing emphasizes:
- Simplicity over complexity
- Real implementations over mocks
- Co-location of tests with source code
- Minimal test setup
- Clear and readable test cases
Getting Started
Setup
Test Structure
File Organization
We follow a co-location strategy for tests. Place your test files next to the source files they test:
Test File Naming
- Use
.test.ts
extension for test files - Name test files after the module they test
- For TypeScript, use
.test.ts
or.test.tsx
for React components
Writing Tests
Basic Test Structure
This is just example code, and isn't meant to be used as-is. It's meant to demonstrate the structure of a test file.
Using getTestInstance
The test instance provides multiple handy properties that you can use to test your feature.
Only create custom instances when needed
Best Practices
Do's
- Write descriptive test names that explain the expected behavior
- Test both success and failure cases
- Keep tests focused and atomic
- Use setup and teardown when needed
- Test edge cases and boundary conditions
This is just example code, and isn't meant to be used as-is. It's meant to demonstrate the best Practices of testing.
Don'ts
- Don't create unnecessary mocks
- Don't test implementation details
- Don't create separate test folders
- Don't write brittle tests
- Don't test external libraries
Future Plans
- Implement integration testing
- Add end-to-end testing
- Expand API testing coverage
- Add performance testing
- Implement snapshot testing where appropriate
Contributing
When submitting a PR:
- Ensure all tests pass
- Add tests for new features
- Update tests for modified features
- Follow existing test patterns
- Include meaningful assertions
Need help? Feel free to reach out in our Discord server!