nestjs-firebase-auth icon indicating copy to clipboard operation
nestjs-firebase-auth copied to clipboard

How to test controllers with FIREBASE_ADMIN_INJECT inject?

Open ziw opened this issue 4 years ago • 1 comments
trafficstars

hi, what's the proper way to inject FIREBASE_ADMIN_INJECT in unit tests? I have a controller

@Controller('users')
export class UsersController {
  constructor(
    @Inject(FIREBASE_ADMIN_INJECT) private firebaseAdmin: FirebaseAdminSDK,
  ) {}
}

and in its unit test I'm getting error Nest can't resolve dependencies of the UsersController (?). Please make sure that the argument FIREBASE_ADMIN_INJECT at index [0] is available in the RootTestModule context.

ziw avatar Jun 24 '21 00:06 ziw

@ziw would love to know if you ever figured this out. I'm writing e2e tests and have tried both overrideProviders:

const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    })
      .overrideProvider(FIREBASE_ADMIN_INJECT)
      .useValue({})
      .compile();

    app = moduleFixture.createNestApplication();

and passing providers:

const MockFirebaseProvider = {
  provide: FIREBASE_ADMIN_INJECT,
  useFactory: async () => {
    return {};
  },
};

const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
      providers: [MockFirebaseProvider],
    })
      .compile();

    app = moduleFixture.createNestApplication();

but in both cases I get a similar error to you:

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

    Potential solutions:
    - If FIREBASE_ADMIN_INJECT is a provider, is it part of the current AnimalsModule?
    - If FIREBASE_ADMIN_INJECT is exported from a separate @Module, is that module imported within AnimalsModule?
 

yogaraptor avatar Dec 01 '21 01:12 yogaraptor