tfarras / nestjs-firebase-auth

Firebase Authentication Strategy for NestJS Passport Integration
68 stars 25 forks source link

How to test controllers with FIREBASE_ADMIN_INJECT inject? #5

Open ziw opened 3 years ago

ziw commented 3 years ago

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.

itsravenous commented 2 years ago

@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?