Closed elton-okawa closed 1 week ago
This is the root of the issue. The function is not called on the instance, but rather on the prototype https://github.com/nestjs/nest/blob/master/packages/microservices/listener-metadata-explorer.ts#L50
Let's track this here https://github.com/nestjs/nest/pull/14112
Is there an existing issue for this?
Current behavior
Controller
instance - handler is called but spy does not track itController
prototype before app is initialized - handler is not calledController
prototype after app is initialized - handler is called but spy does not track itMinimum reproduction code
https://github.com/elton-okawa/nestjs-spy-controller-issue
Steps to reproduce
Expected behavior
From a similar issue comment: https://github.com/nestjs/nest/issues/8790#issuecomment-994480901 (As the comment suggests, I couldn't find any related issue besides this one)
I understood that it's possible to spy/mock annotated methods as long as you do it before the app is initialized, so behavior
(1)
and(3)
is expected to fail, but not(2)
Package
Other package
No response
NestJS version
10.3.8
Packages versions
Node.js version
20.11.1
In which operating systems have you tested?
Other
Spying on
Controller
instance - handler is called but spy does not track itTest Suites: 1 failed, 1 total Tests: 1 failed, 5 skipped, 6 total Snapshots: 0 total Time: 44.32 s, estimated 249 s Ran all test suites with tests matching "should spy controller instance".
[Nest] 48666 - 04/21/2024, 7:31:55 AM DEBUG [E2E Test] Producing message... [Nest] 48666 - 04/21/2024, 7:31:55 AM DEBUG [E2E Test] Message produced successfully! [Nest] 48666 - 04/21/2024, 7:31:55 AM DEBUG [E2E Test] Waiting "message to be consumed" for 10000ms... [Nest] 48666 - 04/21/2024, 7:32:05 AM DEBUG [E2E Test] Finished waiting for "message to be consumed" [Nest] 48666 - 04/21/2024, 7:32:05 AM DEBUG [E2E Test] Message should be consumed at this point ...
● AppController (e2e) › controller › should spy controller prototype before init
Test Suites: 1 failed, 1 total Tests: 1 failed, 5 skipped, 6 total Snapshots: 0 total Time: 51.975 s Ran all test suites with tests matching "spy controller prototype before init".
[Nest] 49480 - 04/21/2024, 7:33:57 AM DEBUG [E2E Test] Producing message... [Nest] 49480 - 04/21/2024, 7:33:57 AM DEBUG [E2E Test] Message produced successfully! [Nest] 49480 - 04/21/2024, 7:33:57 AM DEBUG [E2E Test] Waiting "message to be consumed" for 10000ms... [Nest] 49480 - 04/21/2024, 7:33:57 AM LOG [AppService] Message received: "spy controller prototype after init" [Nest] 49480 - 04/21/2024, 7:34:07 AM DEBUG [E2E Test] Finished waiting for "message to be consumed" [Nest] 49480 - 04/21/2024, 7:34:07 AM DEBUG [E2E Test] Message should be consumed at this point ...
● AppController (e2e) › controller › should spy controller prototype after init
Test Suites: 1 failed, 1 total Tests: 1 failed, 5 skipped, 6 total Snapshots: 0 total Time: 44.39 s, estimated 52 s Ran all test suites with tests matching "spy controller prototype after init".