Test
NocoBase provides a complete set of testing tools to help you quickly verify the correctness of database logic, API interfaces, and feature implementations during plugin development.
Why Write Tests
Benefits of writing automated tests in plugin development:
- Quickly verify database models, APIs, and business logic are correct
- Avoid regression errors (automatically detect plugin compatibility after core upgrades)
- Support continuous integration (CI) environments to automatically run tests
- Support testing plugin functionality without starting the complete service
Test Environment Basics
NocoBase provides two core testing tools:
Using createMockDatabase for Database Testing
createMockDatabase is suitable for testing functionality directly related to databases, such as model definitions, field types, relationships, CRUD operations, etc.
Basic Example
Testing CRUD Operations
Testing Model Associations
Using createMockServer for API Testing
createMockServer automatically creates a complete application instance including database, plugins, and API routes, suitable for testing plugin interfaces.
Basic Example
Testing API Queries and Updates
Simulating Login Status or Permission Testing
You can enable the auth plugin when creating MockServer, then use the login interface to obtain token or session:
You can also use the simpler login() method:
Organizing Test Files in Plugins
It's recommended to store server-side logic-related test files in the plugin's ./src/server/__tests__ folder.
Running Tests
Related Links
- Plugin — Plugin lifecycle and core APIs
- Collections — Collection definition and configuration
- Database — Database operations and Repository API
- Server Development Overview — Overview of all server modules
- Plugin Development Overview — General introduction to plugin development

