Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock service for unit tests #15

Open
ghost opened this issue Feb 5, 2022 · 1 comment
Open

Mock service for unit tests #15

ghost opened this issue Feb 5, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Feb 5, 2022

Hi, Great package.

I am having some trouble mocking the service for my unit tests. It seems like, if I provide the SendGridModule in the imports it ignores the provider override and actually sends the email. If I do not provide the SendGridModule this is the error

Nest can't resolve dependencies of the MailerService (?, ConfigService). Please make sure that the argument SendGridToken at index [0] is available in the RootTestModule context.

const mockSendGridService = {
  send: jest.fn().mockResolvedValue(null)
};

const mockConfigService = {
  get: jest.fn().mockReturnValue('')
};

describe('MailerService', () => {
  let service: MailerService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        MailerService,
        { provide: SendGridService, useValue: mockSendGridService },
        { provide: ConfigService, useValue: mockConfigService }
      ]
    }).compile();

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

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

can you tell me where im going wrong?

Thanks

@jquinteros-applaudo
Copy link

Hey, you need to pass SENDGRID_TOKEN const from nestjs-sendgrid package in the provide property when creating your testing module. And just in case, pass the same value when getting the provider inside the beforeEach function

tl;dr -> The DI identifier/token for the SendGrid service is not the class, it's the constant SENDGRID_TOKEN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant