Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs: cannot inject ConfigService while testing

I'm having problems setting up a unit test that utilizes the ConfigService to set up TypeORM. The test below fails with the following message:

Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument ConfigService at index [0] is available in the TypeOrmCoreModule context.

I've tried adding the ConfigService as a provider, but with no luck. Any ideas on what I'm doing wrong?

import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';

describe('TypeORM setup', () => {
    beforeEach(async () => {
        await Test.createTestingModule({
            providers: [ConfigService],
            imports: [
                ConfigModule.forRoot(),
                TypeOrmModule.forRootAsync({
                    useFactory: (config: ConfigService) => ({ ...config.get('db') }),
                    inject: [ConfigService],
                }),
            ],
        }).compile();
    });

    it('dummy', () => {
        true === true;
    });
});
like image 773
Safron Avatar asked Sep 01 '26 01:09

Safron


1 Answers

You either need to set { isGlobal: true } as an option for the ConfigModule or you need to add imports: [ConfigModule] to the TypeOrmModule.forRootAsync() configuration

like image 119
Jay McDoniel Avatar answered Sep 02 '26 13:09

Jay McDoniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!