Skip to content

You're viewing documentation for a pre-release version. View the latest stable version

Function

withApp(environment:configuration:configReader:logger:services:configure:_:)

Perform a test while handling lifecycle of the application. Feel free to create a custom function like this, tailored to your project.
@discardableResult func withApp<T>(environment: Environment = .testing, configuration: ServerConfiguration = .init(), configReader: ConfigReader = ConfigReader(providers: [CommandLineArgumentsProvider(), EnvironmentVariablesProvider()]), logger: Logger = Logger.current, services: Application.ServiceConfiguration = .init(), configure: ((Application) async throws -> Void)? = nil, _ test: (Application) async throws -> T) async throws -> T

Parameters

configure

A closure where you can register routes, databases, providers, and more.

test

A closure which performs your actual test with the configured application.

Discussion

Usage:

@Test
func helloWorld() async throws {
    try await withApp(configure: configure) { app in
        try await app.testing { client in
            let res = try await client.get("hello")
            #expect(res.status == .ok)
            try #expect(await res.body.requireString() == "Hello, world!")
        })
    }
}