Nest: e2e test returns status 201

Created on 6 Mar 2019  路  8Comments  路  Source: nestjs/nest

e2e test: expected 400 "Bad Request", got 201 "Created"

Current behavior


I have 1 test which returns status 201.

Expected behavior


It should return status 400, am I wrong?

Minimal reproduction of the problem with the instructions


// ------------------------------------------------------- credentials.dto.ts
import { IsNotEmpty, MinLength } from 'class-validator';

export class CredentialsDto {
  @IsNotEmpty()
  username: string;
  @MinLength(5)
  @IsNotEmpty()
  password: string;
}


// ---------------------------------------------------------- auth.e2e-spec.ts
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AuthModule } from '../auth/auth.module';

describe('Auth', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const module = await Test.createTestingModule({
      imports: [AuthModule],
    })
      .compile();

    app = module.createNestApplication();
    await app.init();
  });

  it(`POST /auth`, () => {
    return request(app.getHttpServer())
      .post('/auth')
      .expect(400);
  });

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


// ----------------------------------------------------------------- auth.controller.ts
import { Body, Controller, Post } from '@nestjs/common';
import { CredentialsDto } from '../models/dto/credentials.dto';

@Controller('auth')
export class AuthController {
  @Post()
  login(@Body() body: CredentialsDto): string {
    return 'token';
  }
}


// ----------------------------------------------------------------- auth.module.ts
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './services/auth/auth.service';

@Module({
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

// ------------------------------------------------------------------ main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
}
bootstrap();

Environment


Nest version: X.Y.Z


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Most helpful comment

In your testing module:

app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
await app.init();

All 8 comments

In your testing module:

app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
await app.init();

I know this issue is closed but I can't get auto-validation work on e2e test.
Everytime I try auto-validation feature, process terminated with the error process.exit called with "1" on the below line.

app = moduleFixture.createNestApplication();
app.useGlobalPipes(new ValidationPipe());

since i'm a noob to nest.js, I have no idea I've done anything wrong

@whdckszxxx Please, create a separate issue with repository that reproduces your problem :)

Nevermind. It was my mistake. After installing class-transformer package, the crash fixed.

I know this issue is closed but I can't get auto-validation work on e2e test.
Everytime I try auto-validation feature, process terminated with the error process.exit called with "1" on the below line.

app = moduleFixture.createNestApplication();
app.useGlobalPipes(new ValidationPipe());

since i'm a noob to nest.js, I have no idea I've done anything wrong

Im having this issue. I really dont know how to fix it.

@luqezman you only have to install class-transformer package.

[PackageLoader] The "class-transformer" package is missing. Please, make sure to install this library ($ npm install class-transformer) to take advantage of ValidationPipe.

I am not sure why it is not written in the official document.
https://docs.nestjs.com/techniques/validation

@kamilmysliwiec

In your testing module:

app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
await app.init();

Is there a way how to "include" the main.ts in my e2e tests? Because I don't think it is clever to always copy the code (globally registered pipes, filters, and so on) into my e2e tests. They should interact with the application server like a normal user would do.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cdiaz picture cdiaz  路  3Comments

yanshuf0 picture yanshuf0  路  3Comments

KamGor picture KamGor  路  3Comments

artaommahe picture artaommahe  路  3Comments

mishelashala picture mishelashala  路  3Comments