Skip to main content
Testing is essential for maintaining reliable Feathers applications. This guide covers testing strategies for services, hooks, real-time events, and complete application workflows.

Test Setup

Feathers applications typically use Mocha for testing, though you can use any testing framework like Jest or Vitest.

Installation

Basic Test Structure

Generated Feathers apps include test files with this structure:

Testing Services

Services can be tested in isolation without starting the HTTP server.

Service Unit Tests

Testing Service Hooks

Testing Hooks

You can test hooks in isolation by creating mock contexts.

Testing Custom Hooks

Testing Authentication

Testing Real-time Events

End-to-End HTTP Testing

Test your REST API endpoints using axios:

Testing with Different Databases

Use a separate test database configuration:

Database Cleanup

Best Practices

Always clean up test data and close connections properly to prevent test pollution and resource leaks.

1. Use Lifecycle Hooks

2. Isolate Tests

Each test should be independent:

3. Test Edge Cases

4. Use Descriptive Test Names

5. Test Error Handling

Running Tests

Add test scripts to your package.json:
Run tests: