Reorganize tests in prep for adding more #673
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before we add more unit tests to the repository, I am reorganizing things to help us have consistent and maintainable tests, as well as some utilities to make writing tests easier. I know this is a large PR, but it keeps all of current tests and just updates them to follow recommended best practices for Vitest/TypeScript projects.
All tests follow the Arrange-Act-Assert pattern with explicit comments (
// Arrange,// Act,// Assert) to clearly delineate test phases. We prioritize per-test mock setup using helper functions (e.g.,setupDialWebRTCMocks()) overbeforeEach()hooks to make tests self-contained. Parameterized tests usingit.each()reduce duplication, and all mocks are cleaned up inafterEach()withvi.restoreAllMocks(). Critically, we've eliminated all test logic anti-patterns—no loops, conditionals, or mutable state variables—instead using Vitest's declarative chaining API (.mockReturnValueOnce(),.mockImplementationOnce()) for predictable test behavior.We've adopted a three-directory structure:
__tests__/contains test files (*.spec.ts),__fixtures__/contains test data and configuration objects as individual named constants (e.g.,withAccessToken,testCredential), and__mocks__/contains reusable mock factory functions that return fresh instances (e.g.,createMockPeerConnection()). The key distinction is that fixtures are data (configurations, constants, sample values) while mocks are behavior (factory functions that create test doubles with methods). We use stubs when we only need canned responses and verify the resulting state, and mocks when we need to verify specific interactions occurred. Fake timers (vi.useFakeTimers()) ensure deterministic testing of time-dependent code. This separation provides clear boundaries while enabling reusability and preventing state pollution between tests. It also keeps domain directories clean and focused on "business logic" and keeps domain-related test logic in clear and consistent locations.