samedi 5 septembre 2020

A worker process has failed to exit gracefully and has been force exited

I'm trying to write an jestJS e2e test for my nestJs backend.

So this is my basic config for the Test. But I'll get the warning:

A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks.

I do not see the missing part, so I need some help to identify where the warning comes from:

test.e2e.spec.ts

import { Test, TestingModule } from '@nestjs/testing'
import { GraphQLModule } from '@nestjs/graphql'
import { DatabaseModule } from './database.module'

import { MyModule } from './anything.module'

describe('Module', () => {
    let app

    beforeAll(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
        imports: [
            MyModule,
            DatabaseModule,
            GraphQLModule.forRoot({
                typePaths: ['./**/*.graphql']
            })
        ]
        }).compile()

        app = moduleFixture.createNestApplication()
        await app.init()
    })
    afterAll(async () => {
        await app.close()
    })

    test('addDiagnosis', () => {
        expect(true).toBe(true)
    })
})

Maybe the problem is in the DatabaseModule? Does the mongodb client have to be closed?

database.module

import { Module } from '@nestjs/common'
import { MongoClient, Db, Logger } from 'mongodb'

@Module({
providers: [
    {
    provide: 'DATABASE_CONNECTION',
    useFactory: async (): Promise<Db> => {
        const mongo = 'mongodb://localhost:27017'
        const database = 'testing'

        try {
            Logger.setLevel('debug')

            const client = await MongoClient.connect(mongo, {
                useNewUrlParser: true,
                useUnifiedTopology: true,
            });

            const db = client.db(database)
            return db
        } catch (error) {
            throw error
        }
    }
    },
],
exports: ['DATABASE_CONNECTION'],
})
export class DatabaseModule {}

Aucun commentaire:

Enregistrer un commentaire