-1

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 { CreateProductDto } from './../src/products/dto/create-product.dto';
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: [
        TypeOrmModule.forRoot(config),
        TypeOrmModule.forFeature([Product]),
        ProductsModule,
      ],
    }).compile();

    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())
      .post(`/api/v1/products`)
      .send(createProductDto);

    // Assert
    expect(response.status).toEqual(201);
  });
});

I expected it to returns a 201 response. I tried more routes and same as before.

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

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.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147