nestjsnestjs-testing

Get provider injected with key in a NetJS Test


I have a provider which I inject using a key:

export const myProvider = {
    provide: SOME_PROVIDER,
    useFactory: () => {
      return SomeWrappedInstance;
    },
  };

In my tests I want to access this provider which I inject with @Inject(SOME_PROVIDER), how can I do this?

const moduleRef = await Test.createTestingModule({
      imports: [],
      providers: [myProvider]
    }).compile();

Solution

  • it's easy as doing:

    const myProviderValue = moduleRef.get(SOME_PROVIDER)
    //    ^? SomeWrappedInstance