title: How to test email sending with Mailhog on local?
body: <p>Setup mailhog with docker-compose like:</p>
  <pre><code>version: '3'
  services:
    mailhog:
      image: mailhog/mailhog
      ports:
        - 8025:8025
        - 1025:1025
  </code></pre>
  <p>It's possible to access localhost:8025 from browser. Maybe the SMTP server 1025 also works but don't know how to confirm it.</p>
  <p>In a NestJS application, for testing the email code as:</p>
  <pre><code>@Module({
    imports: [NodeMailerModule],
    providers: [MailHogEmailRepository],
    exports: [MailHogEmailRepository],
  })
  class MailHogEmailRepositoryModule {}
  
  @Module({
    imports: [MailHogEmailRepositoryModule],
    providers: [
      {
        provide: EmailRepository,
        useFactory: (
          config: ConfigService,
          mailHog: MailHogEmailRepository,
        ) =&gt; {
          return mailHog;
          }
        },
        inject: [ConfigService, MailHogEmailRepository],
      },
    ],
    exports: [EmailRepository],
  })
  export class EmailRepositoryModule {}
  </code></pre>
  <p><code>MailHogEmailRepository</code> send with <code>nodemailer</code>:</p>
  <pre><code>@Injectable()
  export class MailHogEmailRepository implements EmailRepository {
    constructor(
      @Inject(NodeMailerToken) private readonly nodemailer: Transporter,
    ) {}
  
    async send(email: Email) {
      const options = {
        to: email.to,
        from: email.from,
        subject: email.subject,
      };
      await this.nodemailer.sendMail(options);
    }
  }
  </code></pre>
  <p>nodemailer config:</p>
  <pre><code>import { Module } from '@nestjs/common';
  import { ConfigService } from '@nestjs/config';
  import { createTransport } from 'nodemailer';
  
  export const NodeMailerToken = Symbol('nodemailer');
  
  @Module({
    providers: [
      {
        provide: NodeMailerToken,
        useFactory: (config: ConfigService) =&gt;
          createTransport({
            host: 'localhost',
            port: 1025,
            secure: true,
          }),
        inject: [ConfigService],
      },
    ],
    exports: [NodeMailerToken],
  })
  export class NodeMailerModule {}
  </code></pre>
  <p>In test source, it always timeout:</p>
  <pre><code>import { Test, TestingModule } from '@nestjs/testing';
  import request from 'supertest';
  
  import {
    FastifyAdapter,
    NestFastifyApplication,
  } from '@nestjs/platform-fastify';
  
  describe('Test sender', () =&gt; {
    let app: NestFastifyApplication;
  
    beforeEach(async () =&gt; {
      const moduleFixture: TestingModule = await Test.createTestingModule({
        imports: [AppModule],
      }).compile();
  
      app = moduleFixture.createNestApplication(new FastifyAdapter());
      await app.init();
      await app.getHttpAdapter().getInstance().ready();
    });
  
    describe('/handler (POST)', () =&gt; {
      describe('should send data to mail server', () =&gt; {
        it('success', () =&gt; {
          const message = ...
  
          return request(app.getHttpServer())
            .post('/handler')
            .send({ message })
            .expect(200);
        });
      });
    });
  });
  </code></pre>
  <p><code>$ npm run test</code></p>
  <pre><code>thrown: &quot;Exceeded timeout of xxx ms for a test.
  Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.&quot;
  </code></pre>
  <p>It seems the test case couldn't access the mailhog server running in docker container. How to set it correctly?</p>