NestJS class-validator is not the same in e2e testing mode
See original GitHub issueBug Report
Seems like NestJS Class Validation is not behaving the same in testing environment (with supertest
) and in dev/prod environment (axios
, Postman).

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:
- Created 3 years ago
- Reactions:5
- Comments:9 (3 by maintainers)
Try just pass it manually in your e2e file
This works for me.
@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 😃