DEV Community

Cover image for Write e2e tests for NestJS with testcontainers
1

Write e2e tests for NestJS with testcontainers

So I was really annoyed last time I was trying to mock and have some tests for my NestJS app. So I decided to try testcontainers once more.

Last time I was trying to mock BullMQ and it was giving me a hard time, and this time around I managed to get it working.

Stick around and see how I did it.

Support Me

You can support me by giving my GitHub repo a star, so more developers start writing tests. I am a true believer of automated tests and TDD.

Code

So basically I really do not wanna spy one anything or mock for that matter. But rather I am only interested in knowing that my queue received a message and did its job, e.g. called an external API.

Actually here I am not gonna test that but if you need to verify that the external API was called I think you can simply have a Wiremock Testcontainer and check it was called.

I will not cover that case here, at least not now. But what I am interested to show you here is how the e2e test will look like:

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { RedisContainer } from '@testcontainers/redis';
import * as request from 'supertest';
import { App } from 'supertest/types';
import { AppModule } from './../src/app.module';

describe('AppController (e2e)', () => {
  let app: INestApplication<App>;

  beforeAll(async () => {
    const container = await new RedisContainer().start();

    process.env.SERVICE_NAME = 'test';
    process.env.REDIS_URI = container.getHostname();
    process.env.REDIS_PORT = container.getPort().toString();

    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();

    await app.init();
  });

  it('/ (GET)', async () => {
    const res = await request(app.getHttpServer()).get('/');

    expect(res.status).toBe(200);
    expect(res.text).toBe('Hello World!');
  });
});
Enter fullscreen mode Exit fullscreen mode

NOTE

Here I was not trying to use ConfigService, so I just hacked in my way into the process.env. But feel free to check my repo for a more mature way of dealing with env variables.

GitHub Repo

GitHub logo kasir-barati / nestjs-materials

NestJS tips, tricks, Notes, Things which are not in doc and I used to figure them out and use them







Instagram: https://www.instagram.com/node.js.developers.kh/
Facebook: https://www.facebook.com/kasirbarati
X: https://x.com/kasir_barati
YouTube: https://www.youtube.com/@kasir-barati
GitHub: https://github.com/kasir-barati/
Dev.to: https://dev.to/kasir-barati
LinkedIn: https://linkedin.com/in/kasir-barati

Image of Quadratic

AI spreadsheet assistant for easy data analysis

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay