[SOLVED] E2E Testing NestJS


This Content is from Stack Overflow. Question asked by Juanma De la Flor

Im new to NestJS and im trying to setup my end to end testing. It does not throw any errors but the request always returns 404.
The test look like this:

import { ProductsModule } from './../src/products/products.module';
import { Product } from './../src/products/entities/product.entity';
import { Repository } from 'typeorm';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { createTypeOrmConfig } from './utils';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';

describe('ProductsController (e2e)', () => {
  let app: INestApplication;
  let productRepository: Repository<Product>;

  beforeEach(async () => {
    const config = createTypeOrmConfig();

    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [

    app = moduleFixture.createNestApplication();
    productRepository = moduleFixture.get('ProductRepository');

    await app.init();

  afterAll(async () => {
    await app.close();

  it('create', async () => {
    // Arrange
    const createProductDto = new CreateProductDto();
    createProductDto.name = 'testname';
    createProductDto.description = 'testdescription';
    createProductDto.price = 120;
    createProductDto.image = 'testimage';

    // Act
    const response = await request(app.getHttpServer())

    // Assert

I expected it to returns a 201 response. I tried more routes and same as before.
[![enter image description here][1]][1]

Thanks in advanced
[1]: https://i.stack.imgur.com/iUBey.png


Do you have @Controller('api/v1/products') on your ProductsController? If not, you don’t ever configure the test application to be served from /api/v1, you’d need to set the global prefix and enable versioning (assuming you usually do these in your main.ts). For a simple fix, remove the /api/v1 from your .post() method.

This Question was asked in StackOverflow by Juanma De la Flor and Answered by Jay McDoniel It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?