-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Allow creating a testing handler for HTTP/RPC/WS controllers #4403
base: 8.0.0
Are you sure you want to change the base?
Allow creating a testing handler for HTTP/RPC/WS controllers #4403
Conversation
Pull Request Test Coverage Report for Build 4e598554-7b3c-4414-85b4-3e7323b07c5f
💛 - Coveralls |
23407ab
to
0f91cf5
Compare
Looks great so far! |
41b9aa9
to
ddfa742
Compare
Thanks @kamilmysliwiec. I did the same work for RPC and WebSocket. Please let me know what could be improved! I thought about adding a single public static method to Test, which would accept the same additional parameter as await Test
.createTestingHandler({ class, methodName, protocol: 'http' })`
.setRequest(fakedRequest)
.run(); The Return type of |
I'll review as soon as possible. Thanks @VinceOPS! |
Hey @kamilmysliwiec, I had a look into Thanks |
This looks great, thanks! |
ddfa742
to
7f6eea8
Compare
I'm sorry to keep you waiting @VinceOPS! The PR looks fantastic 🔥
This is somewhat more complicated though. The Perhaps, we should take this idea even further and consider the following use-case - what if someone wants to test the handler in isolation (without spinning up the HTTP server) BUT wants to have all the enhancers bound with This wouldn't be possible with the current API, so it doesn't really cover all the use-cases. What if changed this: await Test
.createTestingHandler({ class, methodName, protocol: 'http' })`
.setRequest(fakedRequest)
.run(); to this: const moduleRef = await Test.createTestingModule({
imports: [....] // whatever
}).compile() // classic approach
await moduleRef
.createTestingHandler({ class, methodName, protocol: 'http' })`
.setRequest(fakedRequest)
.run(); The TL;DR; We could move the |
Thank you for the explanations @kamilmysliwiec 👍! |
4da0a2f
to
ea10459
Compare
I moved the code to
At this point, I feel like your insights would be pretty helpful 😬 ! Thanks 🙏 |
ea10459
to
4957378
Compare
4957378
to
f1b6567
Compare
Can you create tests for this? I'll fix this issue locally then :)
Please, pull the latest version of |
packages/testing/testing-module.ts
Outdated
const createContext = () => | ||
this.get(ExternalContextCreator).create( | ||
this.get(params.class), | ||
this.get(params.class)[params.methodName] as any, | ||
params.methodName as string, | ||
ROUTE_ARGS_METADATA, | ||
new CustomParamsFactory(), | ||
undefined, | ||
undefined, | ||
undefined, | ||
'http', | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the latest version, the ModuleRef
exposes the introspect()
method to check the class scope. Example:
const { scope } = this.get(ModuleRef).introspect(params.class);
if (scope === Scope.REQUEST) {
// add logic for request-scoped controllers here (I can help with that)
} else {
// use what you have now
}
f1b6567
to
00cc483
Compare
40095a5
to
04e1133
Compare
@kamilmysliwiec Heya Kamil! Guess who's back 👻 😂 I know it's been a long time and you probably forgot everything about this PR 😭 🔫 ... Just in case you would find some time for this possible feature, I pushed the test case I could never pass! There are refactorings that I'm holding for now (e.g. almost duplicated Please let me know if I can help 🙏 |
It seems that |
e7336e2
to
9ca8fab
Compare
Hi @kamilmysliwiec! I had a look into it tonight and it looks like the callback created by @WebSocketGateway()
class EventsGateway {
@SubscribeMessage('identity')
async identity(@MessageBody() data: string): Promise<string> {
return data;
}
} There's one thing bugging me here... As far as I understood, I don't know the "context creator" enough to understand. Hitting a breakpoint on this line ( args[index] = await this.getParamValue(
value,
{ metatype, type, data },
pipes.concat(paramPipes),
); I can see that
Regarding this line (just before):
Does it help? 🤯 Thanks! |
9ca8fab
to
6981ed3
Compare
I've looked into this issue once again and I can see that So in fact, Funny enough, this has changed in 8.0.0 so once you merge One thing to keep in mind - remember to import from (testing-module.ts above^) |
Sounds great 😍 Gonna get back to it ASAP 😄! Thanks! I will keep you posted. |
6981ed3
to
1478d98
Compare
1478d98
to
34ba9d3
Compare
34ba9d3
to
ac32f46
Compare
Thank you @kamilmysliwiec 🙏🏼 ! It's indeed working when rebased on branch 8.0.0 🎉 I have troubles using Pipes (test |
@VinceOPS There might be several different reasons why this is happening. Maybe check if this line: const module = this.getContextModuleName(instance.constructor); in |
Any news on this ? |
@VinceOPS What's the status of this? |
Hi @kamilmysliwiec, I'd like to continue developing this feature, but I'd need some help and clarification to get started Are those changes still relevant for nestjs 10? What are the next steps? |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
See #3938
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information