nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

@Body transform issue: The `metatype` property of the 2nd Arg to ValidationPipe.transform(v, metadata) is `undefined` during jest e2e `supertest` run. #13108

Closed m-fire closed 9 months ago

m-fire commented 9 months ago

Is there an existing issue for this?

Current behavior

1. Typical app runs or standalone E2E supertest situations:

image

2-1. Nested request API scenario testing situations:

image

2-2. ValidationPipe.transform Status:

image

Minimum reproduction code

  1. CodeSandbox
  2. GitHub repository

Steps to reproduce

  1. Run npm run test:e2e, check the result: success2, failure3
  2. DTO transform success tests. path: test/module/auth/feature
    • /sign-in-user.api.test.ts
    • /sign-up-user.api.test.ts
  3. Failed tests. path: test/module/board/feature
    • /register-board.api.test.ts
    • /register-comment.api.test.ts
    • /get-comment-list.api.test.ts
  4. debug the request method in src/module/auth/feature/sign-in-user.api.ts for each test
  5. the undefined check in the title should be checked in the ValidationPipe.transform() method.

Expected behavior

ValidationPipe should have no problem converting DTOs through @Body. It's too unreliable.

Package

Other package

No response

NestJS version

10.3.1

Packages versions



### Node.js version

20.11.0

### In which operating systems have you tested?

- [ ] macOS
- [X] Windows
- [ ] Linux

### Other

[System Information]
OS Version     : Windows 10.0.22631
NodeJS Version : v20.11.0
NPM Version    : 9.6.7 

[Nest CLI]
Nest CLI Version : 10.3.0

[Nest Platform Information]
platform-express version : 10.3.1
mapped-types version     : 2.0.4
serve-static version     : 4.0.0
schematics version       : 10.1.0
passport version         : 10.0.3
typeorm version          : 10.0.1
testing version          : 10.3.1
common version           : 10.3.1
config version           : 3.1.1
core version             : 10.3.1
jwt version              : 10.2.0
cli version              : 10.3.0

[Warnings]
The following packages are not in the same minor version
This could lead to runtime errors
* Under version 10.3
- @nestjs/platform-express 10.3.1
- @nestjs/common 10.3.1
- @nestjs/core 10.3.1
* Under version 10.1
- @nestjs/schematics 10.1.0
micalevisk commented 9 months ago

Please provide a minimum reproduction repository. You can start one by running npm init nest in your terminal

why reproductions are required

m-fire commented 9 months ago

@micalevisk May I request a review? I've updated the minimal reproduction comments and reproduction procedure above, and I've posted sample code as you suggested - it's a minimal scenario test, so there's a lot of source code.

m-fire commented 9 months ago

@micalevisk I think this issue is related to the import/export behavior of Typescript. What do you think? I think it's right, it suddenly started working again 😑. All import/export code is working fine during the overhaul. I've had a hard time with this, you've had a hard time, so you can rest now.😌

micalevisk commented 9 months ago

I've no ideia :thinking:

m-fire commented 9 months ago

This is the second time this has happened to me.

in src/module/auth/feature/sign-in-user.api.ts

before

: The Request was placed inside the auth module outside the Api class. import { AuthToken, SignInUserRequest } from '@src-module/auth/feature'

after

: Located in the same folder as the API class import { SignInUserRequest } from './sign-in-user.request'

I thought it was fixed. It was my mistake. It's hard...

m-fire commented 9 months ago

I'll commit the rest of the code to the repository to reflect this, so take a look when you need to.

m-fire commented 9 months ago

Let's reopen it first. because it's impossible to reproduce what happened😑. I don't know how it was fixed. I don't think it's just an import/export issue with Typescript. I feel like I'm getting lost in a maze...

kamilmysliwiec commented 9 months ago

I'm sure it's just an import/export issue, possibly caused by barrel files (index.ts) introducing a cyclic import in your code, leading to TS metadata being undefined in a specific context