tududi/backend/tests
Chris bdc6e1e71f
API Access (#495)
* Scaffold API docs

* fixup! Scaffold API docs

* Add versioning

* Scaffold API Access

* fixup! Scaffold API Access

* fixup! fixup! Scaffold API Access

* Remove DEBUG logs, add tests

* fixup! Remove DEBUG logs, add tests

* Add rate limiter

* Only enable swagger in development

* Update docs

* fixup! Update docs

* fixup! fixup! Update docs

* Fix issue with markdown table headers
2025-11-07 15:19:30 +02:00
..
helpers Lint and formatting fixes 2025-07-23 12:22:06 +03:00
integration API Access (#495) 2025-11-07 15:19:30 +02:00
mocks Add nanoid 2025-08-04 23:50:53 +03:00
unit Universal search (#412) 2025-10-22 22:00:45 +03:00
README.md Linting cleanup (#99) 2025-07-01 11:40:09 +03:00

Backend Test Suite

This directory contains the test suite for the tududi backend Express application.

Structure

tests/
├── unit/              # Unit tests for individual components
│   ├── models/        # Model tests
│   ├── middleware/    # Middleware tests
│   └── services/      # Service tests
├── integration/       # Integration tests for API endpoints
├── fixtures/          # Test data fixtures
└── helpers/           # Test utilities and helpers

Running Tests

All Tests

npm test

Unit Tests Only

npm run test:unit

Integration Tests Only

npm run test:integration

Watch Mode (for development)

npm run test:watch

Coverage Report

npm run test:coverage

Test Environment

Tests run in a separate test environment with:

  • In-memory SQLite database (isolated from development data)
  • Test-specific configuration from .env.test
  • Automatic database cleanup between tests

Writing Tests

Unit Tests

  • Test individual functions, models, or middleware in isolation
  • Mock external dependencies
  • Focus on business logic and edge cases

Integration Tests

  • Test complete API endpoints
  • Use authenticated requests where needed
  • Test real database interactions
  • Verify response formats and status codes

Test Utilities

  • tests/helpers/testUtils.js provides utilities for creating test data
  • tests/helpers/setup.js handles database setup and cleanup
  • Use createTestUser() for creating authenticated test users

Best Practices

  1. Isolation: Each test should be independent and not rely on other tests
  2. Cleanup: Database is automatically cleaned between tests
  3. Authentication: Use test utilities for creating authenticated requests
  4. Descriptive Names: Test names should clearly describe what is being tested
  5. Coverage: Aim for high test coverage of critical business logic

Dependencies

  • Jest: Test framework
  • Supertest: HTTP testing library for integration tests
  • cross-env: Cross-platform environment variable setting