nestjs-sentry icon indicating copy to clipboard operation
nestjs-sentry copied to clipboard

How to inject into another module?

Open schw4rzlicht opened this issue 3 years ago • 4 comments

Hey there!

I am currently importing the module in my AppModule and configure it there. When I try to DI the SentryService into another module's service using @InjectSentry(), I only get this error

Error: Nest can't resolve dependencies of the SomeService ([...], ?). Please make sure that the argument Symbol(SentryToken) at index [11] is available in the SomeModule context.

despite SomeModule imports the SentryModule from this package.

Is it even possible to import the Sentry module without reconfiguring Sentry for the current module which would make it a little bit verbose imho?

Thanks for your help!

schw4rzlicht avatar Jul 26 '22 09:07 schw4rzlicht

@schw4rzlicht I'm having a similar issue and I was wondering if you (or anyone else) has found a solution? I'm having trouble primarily with testing modules that contain the sentry client

booi avatar Oct 16 '22 06:10 booi

It seems like @xiifain might know what's going on?

booi avatar Oct 16 '22 08:10 booi

do you have a minimal reproducible repo for it @schw4rzlicht.

Regarding the testing context, you can import the SentryModule like this example service.spec.ts. @booi

describe('Service', () => {

  let service: TestService;

  const config: SentryModuleOptions = {
    dsn: '',
    debug: true,
    environment: 'development',
    logLevels: ['debug'],
  };

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [SentryModule.forRoot({ ...config })],
      providers: [
        <your providers>
      ]
    }).compile();

    service = module.get<TestService>(TestService);
  });
}

xiifain avatar Oct 16 '22 09:10 xiifain

@xiifain Thanks for the clarification! I didn't realize I needed to import the sentry module with configuration but it makes sense. I pulled the actual call into it's own file so I could import it in multiple places. Thanks!

booi avatar Oct 18 '22 07:10 booi