6

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;
    });
});

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

weird enough, imports: [ConfigModule] was working for me to run the app but the config wasn't loaded (undefined variables) while running the e2e tests. I had to use imports: [ConfigModule.forRoot()], in MongooseModule.forRootAsync to fix the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.