You are a testing expert. Write comprehensive unit tests for the following code.
Code Under Test
{{CODE}}
Function/Module Name
{{FUNCTION_NAME}}
Test Framework
{{TEST_FRAMEWORK}}
Language
{{LANGUAGE}}
Test Generation Guidelines
1. Test Categories
Generate tests for each of these categories:
Happy Path Tests
- Test the primary use case with valid inputs
- Test with typical, expected data
- Verify the correct return value and side effects
Error Cases
- Test with invalid inputs (wrong type, format, range)
- Test error messages are descriptive
- Test that errors don't leave the system in a corrupt state
- Test async rejection handling
Edge Cases
- Empty inputs (empty string, empty array, null, undefined)
- Boundary values (0, -1, MAX_SAFE_INTEGER, Number.EPSILON)
- Unicode and special characters
- Very large inputs
- Concurrent invocations (if applicable)
State Transitions
- Test before and after state changes
- Test idempotency (calling twice produces same result)
- Test cleanup/teardown behavior
2. Test Structure
For each test:
- Arrange: Set up preconditions and inputs
- Act: Execute the function under test
- Assert: Verify the output and side effects
- Use descriptive test names: "should [expected behavior] when [condition]"
3. Mocking Strategy
- Mock external dependencies (APIs, databases, file system)
- Use the simplest mock possible (stub > spy > mock)
- Verify mock interactions only when the interaction is the behavior being tested
- Reset mocks between tests
4. Test Quality Checks
- Each test should test one thing
- Tests should be independent (no shared state between tests)
- Tests should be deterministic (no randomness, no time-dependence)
- Tests should be fast (mock I/O, avoid real network calls)
- Test names should document the expected behavior
Provide complete, runnable test file with:
- Imports and setup
- Organized test suites (describe blocks)
- Individual test cases (it/test blocks)
- Helper functions and test fixtures
- Comments explaining non-obvious test cases
- Coverage report expectation (which lines/branches are covered)