Group Group Group Group Group Group Group Group Group

Failure when running tests

I’m getting this error every time I try to run my test using Command+U or running a particular test function:

This is the stack trace:

I don’t understand why this failure is occurring. What could be the cause.

That means that something failed in .testable() causing it to throw an error. Change setUp to setUpWithError and don’t try! and you should get a valid error message

I’m getting the same error after using setUpWithError

This is my testable() implementation

I’m not sure what’s causing configure(app) to fail. Here’s the configure(_:) function

public func configure(_ app: Application) throws {
    // Allow external connections. Comment this line to disallow.
//    app.http.server.configuration.hostname = ""

    // Serve files from /Public folder

    let databaseName: String
    let databasePort: Int
    if app.environment == .testing {
        databaseName = "vapor-test"
        if let testPort = Environment.get("DATABASE_PORT") {
            databasePort = Int(testPort) ?? 5433
        } else {
            databasePort = 5433
    } else {
        databaseName = "vapor_database"
        databasePort = 5432
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: databasePort,
        username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
        password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
        database: Environment.get("DATABASE_NAME") ?? databaseName
    ), as: .psql)
    app.logger.logLevel = .debug
    try app.autoMigrate().wait()


    // register routes
    try routes(app)

    // enable SendGrid to send emails

Stepping through the code in the debugger, the app reaches the line try app.autoMigrate().wait() inside configure(_:) and then throws immediately and the fatal error occurs. Here is the console output:

Test Suite 'All tests' started at 2021-06-02 20:29:53.114
Test Suite 'AppTests.xctest' started at 2021-06-02 20:29:53.115
Test Suite 'AcronymTests' started at 2021-06-02 20:29:53.115
Test Case '-[AppTests.AcronymTests testAcronymCanBeSavedWithAPI]' started.
2021-06-02T20:29:53+0300 debug codes.vapor.application : database-id=psql No available connections on this event loop, creating a new one
Vapor/Application.swift:155: Fatal error: Application.shutdown() was not called before Application deinitialized.
2021-06-02 20:29:53.144758+0300 xctest[53932:2077296] Vapor/Application.swift:155: Fatal error: Application.shutdown() was not called before Application deinitialized.

Any hint is much appreciated

Sometimes I get this kind of error when I forget to start the docker container for the TEST database.

1 Like

Yes it’s likely it can’t connect to the DB. You can also wrap the try in a do/catch to print out the error

1 Like

@thartwich @0xtim Thank you very much for the help. Apparently, I created the database with the wrong name vapor_test instead if vapor-test :sweat_smile:

2021-06-03T22:20:58+0300 critical codes.vapor.application : server: database "vapor-test" does not exist (InitPostgres)

Removing the Docker container and running a new one with the correct name solved it.