Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationPipe not triggering in unit tests #841

Closed
arnarleifs opened this issue Jul 24, 2023 · 4 comments
Closed

ValidationPipe not triggering in unit tests #841

arnarleifs opened this issue Jul 24, 2023 · 4 comments
Labels
waiting for response Waiting for more information

Comments

@arnarleifs
Copy link

The problem

When running a unit test using Jest the validation pipeline is not triggered. The expected behaviour would be by providing incorrect values to a route which accepts a body that the validation pipe would be triggered and an error returned to the client.

// The input model
import { IsNumber, IsNotEmpty, IsString, IsOptional } from 'class-validator';

export class UpdateAlbumInputModel {
  @IsNotEmpty()
  @IsNumber()
  albumId: number;
  @IsOptional()
  @IsString()
  description?: string;
  @IsNotEmpty()
  @IsString()
  name: string
}

// The route
@Put('/albums')
@HttpCode(204)
public async updateAlbums(
  @Body(ValidationPipe) body: UpdateAlbumInputModel
) {
  return await this.albumService.updateAlbums(body);
}

The unit test can be found here:

it('should fail to update albums, if the body is invalid', async () => {
  await testApiHandler({
    handler: albumHandler,
    url: '/api/albums',
    test: async ({ fetch }) => {
      const res = await fetch({
        method: 'PUT',
        body: JSON.stringify({
          description: 'description',
        }),
        headers: {
          'Content-Type': 'application/json',
        },
      });
      // This returns a 204 status code, but expected to return an invalid status code of 400 (Bad Request)
      console.log(res);
    },
  });
});
Additional context
Response {
        size: 0,
        timeout: 0,
        cookies: [Getter],
        [Symbol(Body internals)]: {
          body: PassThrough {
            _readableState: [ReadableState],
            _events: [Object: null prototype],
            _eventsCount: 2,
            _maxListeners: undefined,
            _writableState: [WritableState],
            allowHalfOpen: true,
            [Symbol(kCapture)]: false,
            [Symbol(kCallback)]: null
          },
          disturbed: false,
          error: null
        },
        [Symbol(Response internals)]: {
          url: 'http://localhost:53325/',
          status: 204,
          statusText: 'No Content',
          headers: Headers { [Symbol(map)]: [Object: null prototype] },
          counter: 0
        }
      }

Package versions:
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"next-api-decorators": "2.0.2",

@arnarleifs arnarleifs added the bug Something isn't working label Jul 24, 2023
@Xunnamius
Copy link
Owner

Hey @arnarleifs. I'm not sure what a ValidationPipe is. Regardless, this sounds like a problem with your decorator or some other underlying code and not NTARH itself. Do you have a minimal reproducible example repo you can link?

@arnarleifs
Copy link
Author

@Xunnamius The ValidationPipe is part of the next-api-decorator package (https://github.com/instantcommerce/next-api-decorators). It is inspired by NestJS using the decorator pattern.

The issue might be related to their package. The example seen above should be suitable for a minimal reproducible example. I will make sure the issue is not on their side.

@Xunnamius
Copy link
Owner

Xunnamius commented Jul 28, 2023

Interesting library, thanks for sharing! Unfortunately, what you posted above isn't sufficient to reliably recreate the issue. Like with the Apollo issue, I can investigate and resolve on my side but only if I get something I can copy and paste into a file and run node --inspect-brk on, or a repo I can clone and run npm install on.

If you can provide a tiny repo that is minimally configured to immediately demonstrate your issue (including a package.json file), I'll see what I can do :)

@Xunnamius Xunnamius added waiting for response Waiting for more information and removed bug Something isn't working labels Jul 30, 2023
@Xunnamius
Copy link
Owner

Closing this for now, but I'll reopen if there are any developments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for response Waiting for more information
Projects
None yet
Development

No branches or pull requests

2 participants