question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NestJS class-validator is not the same in e2e testing mode

See original GitHub issue

Bug Report

Seems like NestJS Class Validation is not behaving the same in testing environment (with supertest) and in dev/prod environment (axios, Postman).

Screen Shot 2563-08-13 at 02 49 56

With the above setting, every BaseDto’s property is required, and every PartialDto is optional.

This works correctly when requesting to NestJS application in dev/prod mode through tools such as axios, curl or Postman.

However, when testing it through NestJS’s e2e testing guide (eg. https://docs.nestjs.com/fundamentals/testing#end-to-end-testing), it appears to not behave the same as above.

My guess is that PartialType is not working correctly in testing mode. Thus, making PartialDto not partially required as it should be (TypeScript compilation problem?).

Current behavior

When requesting to endpoint with PartialDto, sending only firstName will be fine and returned 200.

However, doing the same with e2e testing will fail with the following error response:

 '{"statusCode":400,"message":["firstName must be a string"],"error":"Bad Request"}'

Input Code

Here is the reproduction Repo: https://github.com/kykungz/nest-test-issue

Try sending a request by running

yarn test:e2e

and compare it with using curl

curl --location --request POST 'http://localhost:5000' \
--header 'Content-Type: application/json' \
--data-raw '{
    "firstName": "test2@test.com"
}'

after starting the app (yarn start:dev)

Expected behavior

The e2e test should pass just like Postman and curl.

PartialDto should accept partial properties as it should be.

Possible Solution

Environment


Nest version: 7.4.1

 
For Tooling issues:
- Node version: v10.20.1 
- Platform:  Mac

Others:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
squid17commented, Oct 28, 2020

Try just pass it manually in your e2e file

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

This works for me.

3reactions
kamilmysliwieccommented, Mar 5, 2021

@jmcdo29 there’s a section about integrating the @nestjs/swagger/plugin too https://docs.nestjs.com/openapi/cli-plugin#integration-with-ts-jest-e2e-tests 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to manually test input validation with NestJS and class ...
To the emulate validation of Nest.js, simply pass the myDtoObject to the validate() function of the class-validator package:
Read more >
Testing | NestJS - A progressive Node.js framework
Nest: automatically scaffolds default unit tests for components and e2e tests for applications; provides default tooling (such as a test runner that builds...
Read more >
5. Extras | Nitro Workshop - GitHub Pages
Nest.js provides a built-in ValidationPipe that enforces validation rules for received data payloads, thanks to annotations provided by the class-validator ...
Read more >
How To Write End-to-End Tests in Node.js Using Puppeteer ...
End-to-end testing (e2e for short) is a process in which the entire ... scripts used in this tutorial are not run in headless...
Read more >
How to write end-to-end tests with Cypress and Node.js
If you're writing production-grade web apps, writing E2E tests is a no-brainer. In this article, we'll take a look at how to write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found