# Critters Next.js Test Suite

This directory contains automated tests for the `@critters/next` package. The test suite is built using Jest and provides coverage for the main functionality of the library.

## Test Coverage Overview

The following components of the library are covered by tests:

1. **Utility Functions** (`utils.test.ts`)

   - String sanitization and formatting
   - Error handling
   - Environment detection
   - Cryptographic signatures

2. **Configuration** (`config.test.ts`)

   - Configuration initialization
   - Singleton pattern validation
   - Configuration getters and setters
   - Feature flag management

3. **Error Reporter** (`reporter.test.ts`)

   - Error report creation and submission
   - Error handling for API communication
   - Hook integration (before/after reporting)
   - Disabled state handling

4. **React Components** (`components.test.ts`)
   - Error boundary functionality
   - Component wrapper behavior
   - Error reporting integration
   - Fallback UI rendering

## Running the Tests

To run the tests, use the following command:

```bash
npm test
```

For watch mode during development:

```bash
npm run test:watch
```

To view coverage report:

```bash
npm run test:coverage
```

## Test Structure

Each test file focuses on testing a specific module:

- `__tests__/utils.test.ts`: Tests for utility functions
- `__tests__/config.test.ts`: Tests for the configuration system
- `__tests__/reporter.test.ts`: Tests for the error reporting functionality
- `__tests__/components.test.tsx`: Tests for React components

## Known Issues and Future Improvements

1. Some tests are currently failing due to mocking issues that need to be addressed:

   - Server-side crypto detection
   - React component state handling
   - API response mocking

2. Coverage improvements needed for:

   - Middleware functionality
   - Collector modules
   - Index exports

3. The coverage thresholds are temporarily set low (10%) to allow for progressive implementation. The target coverage should be:
   - Statements: 80%
   - Branches: 70%
   - Functions: 80%
   - Lines: 80%

## Contributing to Tests

When adding new tests, follow these guidelines:

1. Create test cases that isolate functionality as much as possible
2. Use proper mocking to avoid dependencies on external services
3. Follow the existing test structure and naming conventions
4. Ensure test descriptions clearly explain what is being tested
5. Address edge cases and error scenarios

## Mock Structure

The test suite uses Jest's mocking capabilities:

- Module mocks in individual test files
- Global mocks in the `setup.js` file
- Environment simulation for browser/server contexts

To enhance mock coverage, update the mock implementations in the respective test files or add new globals to the `setup.js` file.
