AdonisJS provides a comprehensive set of testing utilities to make integration and unit testing seamless. TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/adonisjs/core/llms.txt
Use this file to discover all available pages before exploring further.
TestUtils class offers helpers for HTTP testing, context creation, and cookie handling.
Overview
The testing utilities are automatically available through the IoC container and provide:- HTTP context creation for unit tests
- HTTP server management for integration tests
- Cookie client for testing cookie-based functionality
- Macroable interface for custom extensions
Getting Started
Access test utilities from the application container:Creating HTTP Contexts
Create HTTP context instances for testing controllers, middleware, and other HTTP-related code:Custom Request and Response
Provide customIncomingMessage and ServerResponse objects:
HTTP Server Testing
Start the AdonisJS HTTP server for integration testing:With Custom Server
Provide a custom HTTP/HTTPS server callback:Environment Variables
The server respectsHOST and PORT environment variables:
Cookie Testing
Thecookies client helps test cookie-based functionality:
API Reference
TestUtils
The application service instance
Flag indicating if test utilities have been booted
Cookie client instance for handling cookies in tests
Methods
Boots the test utilities. Must be called before using other methods. Requires the app to be booted with all container bindings.
Returns an instance of HTTP server testing utilities.
Creates an HTTP context instance for testing.Parameters:
options.req- Custom IncomingMessage object (optional)options.res- Custom ServerResponse object (optional)
HttpServerUtils
Starts the HTTP server and returns a cleanup function.Parameters:
serverCallback- Optional callback to create a custom HTTP/HTTPS server
Extending Test Utils
TestUtils extends Macroable, allowing you to add custom methods:
Best Practices
Always call
boot() on test utils before using other methods. The boot method initializes the cookie client and other dependencies.